import json from langchain.chat_models import ChatOpenAI from langchain.prompts.chat import ( ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate, ) from utils.configuration import Configuration from utils.utils import convert_choice def format_error_data(text): print('[修复]--------------------------修复格式中--------------------------') chat = ChatOpenAI(temperature=0) configuration=Configuration() data=configuration.read_json("format_config.json") json_declare="(示例:{\"key1\":\"value1\",\"key2\":\"value2\"})" system_template = data["system_template"] system_message_prompt = SystemMessagePromptTemplate.from_template( system_template) human_template = data["human_template"] human_message_prompt = HumanMessagePromptTemplate.from_template( human_template) chat_prompt = ChatPromptTemplate.from_messages( [system_message_prompt, human_message_prompt]) input_prompt=chat_prompt.format_prompt(text=text,json_declare=json_declare).to_messages() rsp = chat(input_prompt) print('[修复]--------------------------Chat得到--------------------------') print(rsp.content) try: result = json.loads(rsp.content, object_hook=convert_choice) print( '[修复]--------------------------json.loads得到--------------------------') print(result) return result except: print("修复失败") return None