Connor Adams commited on
Commit
466697e
·
1 Parent(s): 76d4d08

Add final answer

Browse files
Files changed (1) hide show
  1. agent.py +35 -2
agent.py CHANGED
@@ -61,15 +61,48 @@ def youtube_analysis_tool(question: str, url: str) -> str | None:
61
  raise RuntimeError(f"Processing failed: {str(e)}") from e
62
 
63
  class BasicAgent:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  def __init__(self):
65
  self.agent = Agent(
66
  "gemini-2.5-flash-preview-05-20",
67
  tools=[web_search_tool, youtube_analysis_tool],
68
  system_prompt="You are a helpful assistant that can answer questions about the world.",
69
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
  def __call__(self, question: str) -> str:
72
  print(f"Agent received question (first 50 chars): {question[:50]}...")
73
  result = self.agent.run_sync(question).output
74
- print(f"Agent returning fixed answer: {result}")
75
- return result
 
 
 
61
  raise RuntimeError(f"Processing failed: {str(e)}") from e
62
 
63
  class BasicAgent:
64
+ def _generate_final_answer(self, question: str, answer: str) -> str:
65
+ prompt = f"""
66
+ **Question:** {question}
67
+
68
+ **Initial answer:** {answer}
69
+
70
+ **Example 1:** What is the biggest city in California? Los Angeles
71
+ **Example 2:** How many 'r's are in strawberry? 3
72
+ **Example 3:** What is the opposite of black? White
73
+ **Example 4:** What are the first 5 numbers in the Fibonacci sequence? 0, 1, 1, 2, 3
74
+ **Example 5:** What is the opposite of bad, worse, worst? good, better, best
75
+
76
+ **Final answer:**
77
+ """
78
+ return self.final_answer_agent.run_sync(prompt).output
79
+
80
  def __init__(self):
81
  self.agent = Agent(
82
  "gemini-2.5-flash-preview-05-20",
83
  tools=[web_search_tool, youtube_analysis_tool],
84
  system_prompt="You are a helpful assistant that can answer questions about the world.",
85
  )
86
+ self.final_answer_agent = Agent(
87
+ "gemini-2.5-flash-preview-05-20",
88
+ system_prompt="""
89
+ You are an expert question answering assistant. Given a question and an initial answer, your task is to provide the final answer.
90
+ Your final answer must be a number and/or string OR as few words as possible OR a comma-separated list of numbers and/or strings.
91
+ If you are asked for a number, don't use comma to write your number neither use units such as USD, $, percent, or % unless specified otherwise.
92
+ If you are asked for a string, don't use articles, neither abbreviations (for example cities), and write the digits in plain text unless specified otherwise.
93
+ If you are asked for a comma-separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
94
+ If the final answer is a number, use a number not a word.
95
+ If the final answer is a string, start with an uppercase character.
96
+ If the final answer is a comma-separated list of numbers, use a space character after each comma.
97
+ If the final answer is a comma-separated list of strings, use a space character after each comma and start with a lowercase character.
98
+ Do not add any content to the final answer that is not in the initial answer.
99
+ """,
100
+ )
101
 
102
  def __call__(self, question: str) -> str:
103
  print(f"Agent received question (first 50 chars): {question[:50]}...")
104
  result = self.agent.run_sync(question).output
105
+ print(f"Agent returning initial answer: {result}")
106
+ final_answer = self._generate_final_answer(question, result)
107
+ print(f"Agent returning final answer: {final_answer}")
108
+ return final_answer