format_error_data.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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 utils.configuration import Configuration
  9. from utils.utils import convert_choice
  10. def format_error_data(text):
  11. print('[修复]--------------------------修复格式中--------------------------')
  12. chat = ChatOpenAI(temperature=0)
  13. configuration=Configuration()
  14. data=configuration.read_json("format_config.json")
  15. json_declare="(示例:{\"key1\":\"value1\",\"key2\":\"value2\"})"
  16. system_template = data["system_template"]
  17. system_message_prompt = SystemMessagePromptTemplate.from_template(
  18. system_template)
  19. human_template = data["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. input_prompt=chat_prompt.format_prompt(text=text,json_declare=json_declare).to_messages()
  25. rsp = chat(input_prompt)
  26. print('[修复]--------------------------Chat得到--------------------------')
  27. print(rsp.content)
  28. try:
  29. result = json.loads(rsp.content, object_hook=convert_choice)
  30. print(
  31. '[修复]--------------------------json.loads得到--------------------------')
  32. print(result)
  33. return result
  34. except:
  35. print("修复失败")
  36. return None