123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- import json
- from langchain.chat_models import ChatOpenAI
- from langchain.prompts.chat import (
- ChatPromptTemplate,
- SystemMessagePromptTemplate,
- HumanMessagePromptTemplate,
- )
- from langchain.agents import load_tools
- from langchain.agents import initialize_agent
- from langchain.agents import AgentType
- from langchain.llms import OpenAI
- from format_langchain import format_langchain
- from global_langchain import global_model
- from utils.utils import convert_choice
- def evaluate_langchain():
- print('--------------------------评估优化中--------------------------')
- chat = ChatOpenAI(temperature=0)
- model = global_model()
- # llm = OpenAI(temperature=0)
- # tools = load_tools(["serpapi", "llm-math"], llm=llm)
- # agent = initialize_agent(tools,
- # chat,
- # agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION,
- # verbose=True)
- output_type = str(model.evaluate_declare)
- events = model.get_events()
- event_for_now = events[-1]
- system_template = model.evaluate_system_template
- system_message_prompt = SystemMessagePromptTemplate.from_template(
- system_template)
- human_template = model.evaluate_human_template
- human_message_prompt = HumanMessagePromptTemplate.from_template(
- human_template)
- chat_prompt = ChatPromptTemplate.from_messages(
- [system_message_prompt, human_message_prompt])
- # rsp = agent.run(
- # chat_prompt.format_prompt(output_type=output_type,
- # event_for_now=event_for_now).to_messages())
- rsp = chat(
- chat_prompt.format_prompt(output_type=output_type,
- event_for_now=event_for_now).to_messages())
- print('[优化]--------------------------GPT得到--------------------------')
- print(rsp.content)
- try:
- game_event = json.loads(rsp.content, object_hook=convert_choice)
- except:
- print('格式错误,修复中')
- game_event = format_langchain(rsp.content, output_type)
- events[-1] = {
- "事件": game_event["事件"],
- "是否需要玩家选择下一步操作": game_event["是否需要玩家选择下一步操作"],
- "玩家可以做出的选择": game_event["玩家可以做出的选择"],
- "当前场景角色": game_event["当前场景角色"],
- "当前场景数值系统": game_event["当前场景数值系统"],
- }
- model.update_events(events)
- print(
- '[优化]--------------------------json.loads得到--------------------------')
- print(game_event)
- return game_event
|