value_langchain.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 format_langchain import format_langchain
  9. from global_langchain import global_model
  10. def value_langchain():
  11. print('[数值]--------------------------修改数值中--------------------------')
  12. chat = ChatOpenAI(temperature=0)
  13. model = global_model()
  14. output_type = str(model.value_declare)
  15. intro = model.get_intro_str()
  16. system_template = model.value_system_template
  17. system_message_prompt = SystemMessagePromptTemplate.from_template(
  18. system_template)
  19. human_template = model.value_human_template
  20. human_message_prompt = HumanMessagePromptTemplate.from_template(
  21. human_template)
  22. chat_prompt = ChatPromptTemplate.from_messages(
  23. [system_message_prompt, human_message_prompt])
  24. rsp = chat(
  25. chat_prompt.format_prompt(
  26. output_type=output_type,
  27. intro=intro,
  28. ).to_messages())
  29. print('[数值]--------------------------GPT得到--------------------------')
  30. print(rsp.content)
  31. try:
  32. value_for_now = json.loads(rsp.content)
  33. except:
  34. print('格式错误,修复中')
  35. value_for_now = format_langchain(rsp.content, output_type)
  36. print(
  37. '[数值]--------------------------json.loads得到--------------------------')
  38. print(value_for_now)
  39. model.update_value_for_now(value_for_now)
  40. return value_for_now