politeles commited on
Commit
661e53b
·
verified ·
1 Parent(s): 25154f1

Update app.py

Browse files

multiagents with websearch and final answer.

Files changed (1) hide show
  1. app.py +50 -5
app.py CHANGED
@@ -4,6 +4,7 @@ import requests
4
  import inspect
5
  import pandas as pd
6
  from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel, tool
 
7
  # (Keep Constants as is)
8
  # --- Constants ---
9
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
@@ -12,22 +13,66 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
12
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
13
  class BasicAgent:
14
  agent = None
15
- def __init__(self):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  prompt = "You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. 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."
17
  model = InferenceClientModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct", provider="together")
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  agent = CodeAgent(
19
- model=model,
20
  tools=[GoogleSearchTool(), VisitWebpageTool(), calculate_cargo_travel_time],
21
- additional_authorized_imports=["pandas"],
 
 
22
  max_steps=20,
 
23
  )
24
  print("BasicAgent initialized.")
 
25
  def __call__(self, question: str) -> str:
26
  answer = agent.run(question)
27
  print(f"Agent received question (first 50 chars): {question[:50]}...")
28
  #fixed_answer = "This is a default answer."
29
- print(f"Agent returning fixed answer: {answer}")
30
- return answer
 
 
31
 
32
  def run_and_submit_all( profile: gr.OAuthProfile | None):
33
  """
 
4
  import inspect
5
  import pandas as pd
6
  from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel, tool
7
+ from smolagents import OpenAIServerModel
8
  # (Keep Constants as is)
9
  # --- Constants ---
10
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
 
13
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
14
  class BasicAgent:
15
  agent = None
16
+ web_agent = None
17
+ def check_reasoning(final_answer,agent_memory):
18
+ model = OpenAIServerModel("gpt-4o", max_tokens=8096)
19
+ prompt = (
20
+ f"Here is a user-given task and the agent steps: {agent_memory.get_succinct_steps()}."
21
+ "Please check that the reasoning process is correct: do it correctly answer the given task?"
22
+ "First list reasons why yes/no, then write your final decision: PASS in caps lock if it is satisfactory, FAIL if it is not."
23
+
24
+ )
25
+ messages = [
26
+ {
27
+ "role": "user",
28
+ "content": [
29
+ {
30
+ "type": "text",
31
+ "text": prompt,
32
+ }
33
+ ],
34
+ }
35
+ ]
36
+ output = model(messages).content
37
+ print("Feedback: ", output)
38
+ if "FAIL" in output:
39
+ raise Exception(output)
40
+ return True
41
+ def __init__(self):
42
  prompt = "You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. 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."
43
  model = InferenceClientModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct", provider="together")
44
+
45
+ web_agent = CodeAgent(
46
+ model=model,
47
+ tools=[
48
+ GoogleSearchTool(provider="serper"),
49
+ VisitWebpageTool()
50
+ ],
51
+ name="web_agent",
52
+ description="Browses the web to find information",
53
+ verbosity_level=0,
54
+ max_steps=10,
55
+ )
56
+
57
  agent = CodeAgent(
58
+ model=InferenceClientModel("deepseek-ai/DeepSeek-R1", provider="together", max_tokens=8096),
59
  tools=[GoogleSearchTool(), VisitWebpageTool(), calculate_cargo_travel_time],
60
+ managed_agents=[web_agent],
61
+ additional_authorized_imports=["pandas","json","numpy"],
62
+ planning_interval=5,
63
  max_steps=20,
64
+ final_answer_checks=[check_reasoning],
65
  )
66
  print("BasicAgent initialized.")
67
+
68
  def __call__(self, question: str) -> str:
69
  answer = agent.run(question)
70
  print(f"Agent received question (first 50 chars): {question[:50]}...")
71
  #fixed_answer = "This is a default answer."
72
+ final_answer = answer.replace("FINAL ANSWER:","").strip()
73
+
74
+ print(f"Agent returning fixed answer: {final_answer}")
75
+ return final_answer
76
 
77
  def run_and_submit_all( profile: gr.OAuthProfile | None):
78
  """