i-dhilip commited on
Commit
76df2dd
·
verified ·
1 Parent(s): cdaefb9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -9
app.py CHANGED
@@ -34,8 +34,7 @@ llm = HuggingFacePipeline(pipeline=pipe)
34
  system_prompt = """You are a helpful assistant tasked with answering questions using a set of tools.
35
  Now, I will ask you a question. Report your thoughts, and finish your answer with the following template:
36
  FINAL ANSWER: [YOUR FINAL ANSWER].
37
- YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. 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.
38
- Your answer should only start with "FINAL ANSWER: ", then follows with the answer."""
39
  system_message_prompt = SystemMessagePromptTemplate.from_template(system_prompt)
40
 
41
  # --- Tools ---
@@ -103,6 +102,12 @@ def create_agent(llm, tools):
103
  llm_chain = LLMChain(llm=llm, prompt=prompt)
104
  return llm_chain
105
 
 
 
 
 
 
 
106
  def run_and_submit_all(profile: gr.OAuthProfile | None):
107
  """
108
  Fetches all questions, runs the EnhancedAgent on them, submits all answers,
@@ -160,16 +165,15 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
160
  print(f"Skipping item with missing task_id or question: {item}")
161
  continue
162
  try:
163
- # Get direct answer from the agent (no prefixes or explanations)
164
- submitted_answer = agent.run(question_text)
165
- # Extract the final answer part
166
- if "FINAL ANSWER:" in submitted_answer:
167
- final_answer = submitted_answer.split("FINAL ANSWER:")[1].strip()
168
- else:
169
- final_answer = submitted_answer.strip()
170
 
 
171
  answers_payload.append({"task_id": task_id, "submitted_answer": final_answer})
172
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": final_answer})
 
173
  except Exception as e:
174
  print(f"Error running agent on task {task_id}: {e}")
175
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
 
34
  system_prompt = """You are a helpful assistant tasked with answering questions using a set of tools.
35
  Now, I will ask you a question. Report your thoughts, and finish your answer with the following template:
36
  FINAL ANSWER: [YOUR FINAL ANSWER].
37
+ YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. 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."""
 
38
  system_message_prompt = SystemMessagePromptTemplate.from_template(system_prompt)
39
 
40
  # --- Tools ---
 
102
  llm_chain = LLMChain(llm=llm, prompt=prompt)
103
  return llm_chain
104
 
105
+ def extract_final_answer(full_response):
106
+ """Extract only the final answer from the agent's response."""
107
+ if "FINAL ANSWER:" in full_response:
108
+ return full_response.split("FINAL ANSWER:")[1].strip()
109
+ return full_response.strip()
110
+
111
  def run_and_submit_all(profile: gr.OAuthProfile | None):
112
  """
113
  Fetches all questions, runs the EnhancedAgent on them, submits all answers,
 
165
  print(f"Skipping item with missing task_id or question: {item}")
166
  continue
167
  try:
168
+ # Get the response from the agent
169
+ agent_response = agent.run(question_text)
170
+ # Extract just the final answer part
171
+ final_answer = extract_final_answer(agent_response)
 
 
 
172
 
173
+ # Add to payload for submission
174
  answers_payload.append({"task_id": task_id, "submitted_answer": final_answer})
175
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": final_answer})
176
+ print(f"Task {task_id}: Processed answer: {final_answer}")
177
  except Exception as e:
178
  print(f"Error running agent on task {task_id}: {e}")
179
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})