import json from langchain.chat_models import ChatOpenAI from langchain.prompts.chat import ( ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate, ) from format_langchain import format_langchain from global_langchain import global_model def value_langchain(): print('[数值]--------------------------修改数值中--------------------------') chat = ChatOpenAI(temperature=0) model = global_model() output_type = str(model.value_declare) intro = model.get_intro_str() system_template = model.value_system_template system_message_prompt = SystemMessagePromptTemplate.from_template( system_template) human_template = model.value_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( output_type=output_type, intro=intro, ).to_messages()) print('[数值]--------------------------GPT得到--------------------------') print(rsp.content) try: value_for_now = json.loads(rsp.content) except: print('格式错误,修复中') value_for_now = format_langchain(rsp.content, output_type) print( '[数值]--------------------------json.loads得到--------------------------') print(value_for_now) model.update_value_for_now(value_for_now) return value_for_now