Evaluate_langchain.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import json
  2. from langchain.chat_models import ChatOpenAI
  3. from langchain.prompts.chat import (
  4. ChatPromptTemplate,
  5. SystemMessagePromptTemplate,
  6. HumanMessagePromptTemplate,
  7. )
  8. from langchain.agents import load_tools
  9. from langchain.agents import initialize_agent
  10. from langchain.agents import AgentType
  11. from langchain.llms import OpenAI
  12. from format_langchain import format_langchain
  13. from global_langchain import global_model
  14. from utils.utils import convert_choice
  15. def evaluate_langchain():
  16. print('--------------------------评估优化中--------------------------')
  17. chat = ChatOpenAI(temperature=0)
  18. model = global_model()
  19. # llm = OpenAI(temperature=0)
  20. # tools = load_tools(["serpapi", "llm-math"], llm=llm)
  21. # agent = initialize_agent(tools,
  22. # chat,
  23. # agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION,
  24. # verbose=True)
  25. output_type = str(model.evaluate_declare)
  26. events = model.get_events()
  27. event_for_now = events[-1]
  28. system_template = model.evaluate_system_template
  29. system_message_prompt = SystemMessagePromptTemplate.from_template(
  30. system_template)
  31. human_template = model.evaluate_human_template
  32. human_message_prompt = HumanMessagePromptTemplate.from_template(
  33. human_template)
  34. chat_prompt = ChatPromptTemplate.from_messages(
  35. [system_message_prompt, human_message_prompt])
  36. # rsp = agent.run(
  37. # chat_prompt.format_prompt(output_type=output_type,
  38. # event_for_now=event_for_now).to_messages())
  39. rsp = chat(
  40. chat_prompt.format_prompt(output_type=output_type,
  41. event_for_now=event_for_now).to_messages())
  42. print('[优化]--------------------------GPT得到--------------------------')
  43. print(rsp.content)
  44. try:
  45. game_event = json.loads(rsp.content, object_hook=convert_choice)
  46. except:
  47. print('格式错误,修复中')
  48. game_event = format_langchain(rsp.content, output_type)
  49. events[-1] = {
  50. "事件": game_event["事件"],
  51. "是否需要玩家选择下一步操作": game_event["是否需要玩家选择下一步操作"],
  52. "玩家可以做出的选择": game_event["玩家可以做出的选择"],
  53. "当前场景角色": game_event["当前场景角色"],
  54. "当前场景数值系统": game_event["当前场景数值系统"],
  55. }
  56. model.update_events(events)
  57. print(
  58. '[优化]--------------------------json.loads得到--------------------------')
  59. print(game_event)
  60. return game_event