import os from dotenv import load_dotenv, find_dotenv load_dotenv() # read local .env file API_KEY = os.getenv('openai_api_key')
1 2 3 4 5 6 7 8 9 10
# 定义实体字段
from langchain.chat_models import ChatOpenAI from dotenv import load_dotenv from langchain.prompts import PromptTemplate from langchain.output_parsers import ResponseSchema, StructuredOutputParser
PromptTemplate(input_variables=['input'], partial_variables={'format_instructions': 'The output should be a markdown code snippet formatted in the following schema, including the leading and trailing "```json" and "```":\n\n```json\n{\n\t"disease": list // 疾病名称实体\n\t"symptom": list // 疾病症状实体\n\t"drug": list // 药物名称实体\n}\n```'}, template='\n1. 从以下用户输入的句子中,提取实体内容\n2. 仅根据用户输入抽取,不要推理\n3. 注意json格式,在json中不要出现//\n4. 如果字段为空,也需要保留字段名称。\n\n{format_instructions}\n\n用户输入:{input}\n\n输出:\n')
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# 调用大模型并解析结果 from langchain.chains import LLMChain
defcheck_and_addition(original_key:dict,af_json:dict)->dict: iflen(af_json)!= len(orignal_key): keys = original_key for t in keys: if t notin af_json: af_json[t] = "null" result = af_json return result check_and_addition(original_key,parse_json(llm_output))