asthara commited on
Commit
3c27da6
·
1 Parent(s): b655b9d

add more agents

Browse files
Files changed (5) hide show
  1. agent.py +33 -9
  2. app.py +44 -6
  3. prompts.py +1 -0
  4. requirements.txt +5 -2
  5. tools.py +46 -6
agent.py CHANGED
@@ -4,9 +4,17 @@ from smolagents import (
4
  #InferenceClientModel,
5
  #WebSearchTool,
6
  OpenAIServerModel,
 
7
  )
8
  import os
9
- from tools import tools
 
 
 
 
 
 
 
10
  #model = InferenceClientModel(
11
  # max_tokens=2096,
12
  # temperature=0.5,
@@ -20,21 +28,37 @@ model = OpenAIServerModel(
20
  api_key=os.getenv("GEMINI_API_KEY"),
21
  )
22
 
23
- tool_calling_agent = ToolCallingAgent(
24
- tools=tools,
 
 
 
 
 
 
 
25
  model=model,
26
  max_steps=10,
27
- name="tool_calling_agent",
28
- description="An agent which can use the tools provided to it. The available tools are: PythonInterpreterTool, GoogleSearchTool, VisitWebpageTool, WikipediaSearchTool, SpeechToTextTool",
 
 
 
 
 
 
 
 
 
 
29
  )
30
 
31
  manager_agent = CodeAgent(
32
  tools=[],
33
  model=model,
34
- managed_agents=[tool_calling_agent],
35
- max_steps=6,
36
  verbosity_level=1,
37
- additional_authorized_imports=["time", "numpy", "pandas", "wikipedia", "xlrd"],
38
  name="manager_agent",
39
- description="Manages the web search agent. This is a code agent, it can return code blocks.",
40
  )
 
4
  #InferenceClientModel,
5
  #WebSearchTool,
6
  OpenAIServerModel,
7
+ LiteLLMModel,
8
  )
9
  import os
10
+ import wikipedia
11
+ import time
12
+ import numpy
13
+ import pandas
14
+ import xlrd
15
+ import markdownify
16
+ import requests
17
+ from tools import python_wikipedia_tools, smolagents_code_tools, smolagents_web_tools, smolagents_speech_tools
18
  #model = InferenceClientModel(
19
  # max_tokens=2096,
20
  # temperature=0.5,
 
28
  api_key=os.getenv("GEMINI_API_KEY"),
29
  )
30
 
31
+ # to run locally
32
+ #model = OpenAIServerModel(
33
+ # model_id="gemma3:27b",
34
+ # api_base="http://localhost:11434/v1",
35
+ #)
36
+
37
+
38
+ web_search_agent = CodeAgent(
39
+ tools=smolagents_web_tools,
40
  model=model,
41
  max_steps=10,
42
+ additional_authorized_imports=["wikipedia", "markdownify", "requests"],
43
+ name="web_search_agent",
44
+ description="A code agent which can perform web searches. It has available tools including GoogleSearchTool, VisitWebpageTool and WikipediaSearchTool",
45
+ )
46
+
47
+ code_agent = CodeAgent(
48
+ tools=smolagents_code_tools + python_wikipedia_tools,
49
+ model=model,
50
+ max_steps=10,
51
+ additional_authorized_imports=["time", "numpy", "pandas", "wikipedia", "xlrd", "markdownify", "requests"],
52
+ name="code_agent",
53
+ description="A code agent which can write and interpret code. It has available tools including PythonInterpreterTool, search_wikipedia_page, get_wikipedia_page and get_wikipedia_summary",
54
  )
55
 
56
  manager_agent = CodeAgent(
57
  tools=[],
58
  model=model,
59
+ managed_agents=[web_search_agent, code_agent],
60
+ max_steps=10,
61
  verbosity_level=1,
 
62
  name="manager_agent",
63
+ description="Manages the web_search_agent and code_agent.",
64
  )
app.py CHANGED
@@ -57,16 +57,16 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
57
  response.raise_for_status()
58
  questions_data = response.json()
59
  if not questions_data:
60
- print("Fetched questions list is empty.")
61
- return "Fetched questions list is empty or invalid format.", None
62
  print(f"Fetched {len(questions_data)} questions.")
63
  except requests.exceptions.RequestException as e:
64
  print(f"Error fetching questions: {e}")
65
  return f"Error fetching questions: {e}", None
66
  except requests.exceptions.JSONDecodeError as e:
67
- print(f"Error decoding JSON response from questions endpoint: {e}")
68
- print(f"Response text: {response.text[:500]}")
69
- return f"Error decoding server response for questions: {e}", None
70
  except Exception as e:
71
  print(f"An unexpected error occurred fetching questions: {e}")
72
  return f"An unexpected error occurred fetching questions: {e}", None
@@ -195,4 +195,42 @@ if __name__ == "__main__":
195
  print("-"*(60 + len(" App Starting ")) + "\n")
196
 
197
  print("Launching Gradio Interface for Basic Agent Evaluation...")
198
- demo.launch(debug=True, share=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  response.raise_for_status()
58
  questions_data = response.json()
59
  if not questions_data:
60
+ print("Fetched questions list is empty.")
61
+ return "Fetched questions list is empty or invalid format.", None
62
  print(f"Fetched {len(questions_data)} questions.")
63
  except requests.exceptions.RequestException as e:
64
  print(f"Error fetching questions: {e}")
65
  return f"Error fetching questions: {e}", None
66
  except requests.exceptions.JSONDecodeError as e:
67
+ print(f"Error decoding JSON response from questions endpoint: {e}")
68
+ print(f"Response text: {response.text[:500]}")
69
+ return f"Error decoding server response for questions: {e}", None
70
  except Exception as e:
71
  print(f"An unexpected error occurred fetching questions: {e}")
72
  return f"An unexpected error occurred fetching questions: {e}", None
 
195
  print("-"*(60 + len(" App Starting ")) + "\n")
196
 
197
  print("Launching Gradio Interface for Basic Agent Evaluation...")
198
+ demo.launch(debug=True, share=False)
199
+
200
+ #agent = BasicAgent()
201
+ #question = "You are a general AI assistant. You will be asked 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. Think step by step and reason through the question. Then return your final answer.\n\nQuestion: How many studio albums were published by Mercedes Sosa between 2000 and 2009 (included)? You can use the latest 2022 version of english wikipedia."
202
+ #question = """You are a general AI assistant. You will be asked 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. Think step by step and reason through the question. Then return your final answer.\n\nQuestion: .rewsna eht sa "tfel" drow eht fo etisoppo eht etirw ,ecnetnes siht dnatsrednu uoy fI"""
203
+ #question = "Who nominated the only Featured Article on English Wikipedia about a dinosaur that was promoted in November 2016?"
204
+ #question = """Given this table defining * on the set S = {a, b, c, d, e}
205
+ #
206
+ #|*|a|b|c|d|e|
207
+ #|---|---|---|---|---|---|
208
+ #|a|a|b|c|b|d|
209
+ #|b|b|c|a|e|c|
210
+ #|c|c|a|b|b|a|
211
+ #|d|b|e|b|e|d|
212
+ #|e|d|b|a|d|c|
213
+ #
214
+ #provide the subset of S involved in any possible counter-examples that prove
215
+ #* is not commutative. Provide your answer as a comma separated list of the
216
+ #elements in the set in alphabetical order."""
217
+ #question = """You are a general AI assistant. You will be asked 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. Think step by step and reason through the question. Then return your final answer.\n\nQuestion:
218
+ #I'm making a grocery list for my mom, but she's a professor of botany and
219
+ #she's a real stickler when it comes to categorizing things. I need to add
220
+ #different foods to different categories on the grocery list, but if I make a
221
+ #mistake, she won't buy anything inserted in the wrong category. Here's the
222
+ #list I have so far:
223
+ #
224
+ #milk, eggs, flour, whole bean coffee, Oreos, sweet potatoes, fresh basil,
225
+ #plums, green beans, rice, corn, bell pepper, whole allspice, acorns,
226
+ #broccoli, celery, zucchini, lettuce, peanuts
227
+ #
228
+ #I need to make headings for the fruits and vegetables. Could you please
229
+ #create a list of just the vegetables from my list? If you could do that,
230
+ #then I can figure out how to categorize the rest of the list into the
231
+ #appropriate categories. But remember that my mom is a real stickler, so make
232
+ #sure that no botanical fruits end up on the vegetable list, or she won't get
233
+ #them when she's at the store. Please alphabetize the list of vegetables, and
234
+ #place each item in a comma separated list."""
235
+ #submitted_answer = agent.agent.run(question)
236
+ #print(submitted_answer)
prompts.py CHANGED
@@ -4,4 +4,5 @@ prompt = """You are a general AI assistant. You will be asked a question.
4
  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.
5
  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.
6
  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.
 
7
  """
 
4
  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.
5
  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.
6
  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.
7
+ Think step by step and reason through the question. Then return your final answer.
8
  """
requirements.txt CHANGED
@@ -1,6 +1,9 @@
1
  gradio
2
  requests
3
- smolagents[openai,transformers]
4
  duckduckgo_search
5
  wikipedia-api
6
- transformers
 
 
 
 
1
  gradio
2
  requests
3
+ smolagents[openai,transformers,litellm]
4
  duckduckgo_search
5
  wikipedia-api
6
+ wikipedia
7
+ xlrd
8
+ transformers
9
+ markdownify
tools.py CHANGED
@@ -1,9 +1,49 @@
1
  from smolagents import PythonInterpreterTool, GoogleSearchTool, VisitWebpageTool, WikipediaSearchTool, SpeechToTextTool
 
 
 
2
 
3
- interpreter_tool = PythonInterpreterTool()
4
- google_search_tool = GoogleSearchTool()
5
- visit_webpage_tool = VisitWebpageTool()
6
- wikipedia_search_tool = WikipediaSearchTool()
7
- speech_to_text_tool = SpeechToTextTool()
8
 
9
- tools = [interpreter_tool, google_search_tool, visit_webpage_tool, wikipedia_search_tool, speech_to_text_tool]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from smolagents import PythonInterpreterTool, GoogleSearchTool, VisitWebpageTool, WikipediaSearchTool, SpeechToTextTool
2
+ import wikipedia
3
+ from wikipedia import WikipediaPage
4
+ from smolagents import tool
5
 
6
+ @tool
7
+ def search_wikipedia_page(query: str) -> list[str]:
8
+ """
9
+ Search for Wikipedia pages.
 
10
 
11
+ Args:
12
+ query: The query to search for.
13
+
14
+ Returns:
15
+ A list of Wikipedia pages that match the query.
16
+ """
17
+ return wikipedia.search(query)
18
+
19
+ @tool
20
+ def get_wikipedia_page(title: str) -> WikipediaPage:
21
+ """
22
+ Get the content of a specific Wikipedia page.
23
+
24
+ Args:
25
+ title: The title of the Wikipedia page to get.
26
+
27
+ Returns:
28
+ A WikipediaPage object. The object includes the following attributes: 'content', 'summary', 'images', 'references', 'links', 'sections'
29
+ """
30
+ return wikipedia.page(title)
31
+
32
+ @tool
33
+ def get_wikipedia_summary(title: str) -> str:
34
+ """
35
+ Get a summary of a Wikipedia page.
36
+
37
+ Args:
38
+ title: The title of the Wikipedia page to get.
39
+
40
+ Returns:
41
+ The plain-text summary of the Wikipedia page.
42
+ """
43
+ return wikipedia.summary(title)
44
+
45
+ python_wikipedia_tools = [search_wikipedia_page, get_wikipedia_page, get_wikipedia_summary]
46
+
47
+ smolagents_code_tools = [PythonInterpreterTool()]
48
+ smolagents_web_tools = [GoogleSearchTool(), VisitWebpageTool(), WikipediaSearchTool()]
49
+ smolagents_speech_tools = [SpeechToTextTool()]