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 from utils.utils import convert_choice from value_langchain import value_langchain def event_langchain(): print('--------------------------生成事件中--------------------------') chat = ChatOpenAI(temperature=0) model = global_model() output_type = str(model.event_declare) intro = model.get_intro_str() style = model.style story = model.story system_template = model.event_system_template system_message_prompt = SystemMessagePromptTemplate.from_template( system_template) human_template = model.event_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, style=style, story=story, intro=intro).to_messages()) print(rsp.content) try: game_event = json.loads(rsp.content, object_hook=convert_choice) except: print('格式错误,修复中') game_event = format_langchain(rsp.content, output_type) print(game_event) model.add_event({ "事件": game_event["事件"], "是否需要玩家选择下一步操作": game_event["是否需要玩家选择下一步操作"], "玩家可以做出的选择": game_event["玩家可以做出的选择"], "当前场景角色": game_event["当前场景角色"], "当前场景数值系统": game_event["当前场景数值系统"], "游戏是否结束": game_event["游戏是否结束"], }) model.update_if_end(game_event["游戏是否结束"]) value = value_langchain() return game_event