You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

misc_utils.py 3.0 kB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import re
  2. import copy
  3. from typing import List, Dict
  4. import autogen
  5. def response_preparer(
  6. inner_messages: List[Dict[str, str]],
  7. PROMPT: str,
  8. client: autogen.OpenAIWrapper,
  9. ) -> str:
  10. messages = [
  11. {
  12. "role": "user",
  13. "content": f"""Earlier you were asked the following:
  14. {PROMPT}
  15. Your team then worked diligently to address that request. Here is a transcript of that conversation:""",
  16. }
  17. ]
  18. for message in inner_messages:
  19. if not message.get("content"):
  20. continue
  21. message = copy.deepcopy(message)
  22. message["role"] = "user"
  23. messages.append(message)
  24. # ask for the final answer
  25. messages.append(
  26. {
  27. "role": "user",
  28. "content": f"""
  29. Read the above conversation and output a FINAL ANSWER to the question. The question is repeated here for convenience:
  30. {PROMPT}
  31. """,
  32. }
  33. )
  34. response = client.create(context=None, messages=messages)
  35. if "finish_reason='content_filter'" in str(response):
  36. raise Exception(str(response))
  37. extracted_response = client.extract_text_or_completion_object(response)[0]
  38. # No answer
  39. if "unable to determine" in extracted_response.lower():
  40. print("\n>>>Making an educated guess.\n")
  41. messages.append({"role": "assistant", "content": extracted_response})
  42. messages.append(
  43. {
  44. "role": "user",
  45. "content": """
  46. I understand that a definitive answer could not be determined. Please make a well-informed EDUCATED GUESS based on the conversation.
  47. To output the educated guess, use the following template: EDUCATED GUESS: [YOUR EDUCATED GUESS]
  48. Your EDUCATED GUESS should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. DO NOT OUTPUT 'I don't know', 'Unable to determine', etc.
  49. ADDITIONALLY, your EDUCATED GUESS MUST adhere to any formatting instructions specified in the original question (e.g., alphabetization, sequencing, units, rounding, decimal places, etc.)
  50. If you are asked for a number, express it numerically (i.e., with digits rather than words), don't use commas, and don't include units such as $ or percent signs unless specified otherwise.
  51. If you are asked for a string, don't use articles or abbreviations (e.g. for cities), unless specified otherwise. Don't output any final sentence punctuation such as '.', '!', or '?'.
  52. If you are asked for a comma separated list, apply the above rules depending on whether the elements are numbers or strings.
  53. """.strip(),
  54. }
  55. )
  56. response = client.create(context=None, messages=messages)
  57. if "finish_reason='content_filter'" in str(response):
  58. raise Exception(str(response))
  59. extracted_response = client.extract_text_or_completion_object(response)[0]
  60. return re.sub(r"EDUCATED GUESS:", "FINAL ANSWER:", extracted_response)
  61. else:
  62. return extracted_response