format_error_data.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. system_template = data["system_template"]
  16. system_message_prompt = SystemMessagePromptTemplate.from_template(
  17. system_template)
  18. human_template = data["human_template"]
  19. human_message_prompt = HumanMessagePromptTemplate.from_template(
  20. human_template)
  21. chat_prompt = ChatPromptTemplate.from_messages(
  22. [system_message_prompt, human_message_prompt])
  23. rsp = chat(
  24. chat_prompt.format_prompt(text=text).to_messages())
  25. print('[修复]--------------------------GPT得到--------------------------')
  26. print(rsp.content)
  27. try:
  28. result = json.loads(rsp.content, object_hook=convert_choice)
  29. print(
  30. '[修复]--------------------------json.loads得到--------------------------')
  31. print(result)
  32. return result
  33. except:
  34. print("修复失败")
  35. return None