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") 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]) rsp = chat( chat_prompt.format_prompt(text=text).to_messages()) print('[修复]--------------------------GPT得到--------------------------') print(rsp.content) try: result = json.loads(rsp.content, object_hook=convert_choice) print( '[修复]--------------------------json.loads得到--------------------------') print(result) return result except: print("修复失败") return None