elomid commited on
Commit
dd910f0
·
1 Parent(s): ae7a494

first agent

Browse files
Files changed (3) hide show
  1. .gitignore +54 -0
  2. app.py +57 -14
  3. tools/web_search.py +9 -3
.gitignore ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Python
2
+ *.py[cod]
3
+ *.so
4
+ .Python
5
+ build/
6
+ develop-eggs/
7
+ dist/
8
+ downloads/
9
+ eggs/
10
+ .eggs/
11
+ lib/
12
+ lib64/
13
+ parts/
14
+ sdist/
15
+ var/
16
+ wheels/
17
+ *.egg-info/
18
+ .installed.cfg
19
+ *.egg
20
+ .gradio/
21
+ __pycache__/
22
+
23
+ # Virtual Environment
24
+ venv/
25
+ .venv/
26
+ ENV/
27
+ env/
28
+
29
+ # IDE specific files
30
+ .idea/
31
+ .vscode/
32
+ *.swp
33
+ *.swo
34
+ .DS_Store
35
+
36
+ # Jupyter Notebook
37
+ .ipynb_checkpoints
38
+
39
+ # Local development settings
40
+ *.log
41
+ .env.local
42
+ .env.*.local
43
+ .env
44
+
45
+ # Gradio specific
46
+ gradio_cached_examples/
47
+ flagged/
48
+
49
+ # Model files & cache
50
+ models/
51
+ *.pt
52
+ *.pth
53
+ *.ckpt
54
+ .cache/
app.py CHANGED
@@ -1,23 +1,55 @@
1
- from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
  import datetime
3
  import requests
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
 
 
 
 
 
 
 
 
7
 
8
  from Gradio_UI import GradioUI
9
 
 
10
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
11
  @tool
12
- def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
13
- #Keep this format for the description / args / args description but feel free to modify the tool
14
- """A tool that does nothing yet
 
 
15
  Args:
16
  arg1: the first argument
17
  arg2: the second argument
18
  """
19
  return "What magic will you build ?"
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  @tool
22
  def get_current_time_in_timezone(timezone: str) -> str:
23
  """A tool that fetches the current local time in a specified timezone.
@@ -34,36 +66,47 @@ def get_current_time_in_timezone(timezone: str) -> str:
34
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
35
 
36
 
 
 
 
37
  final_answer = FinalAnswerTool()
38
 
39
  # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
40
- # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
41
 
42
  model = HfApiModel(
43
- max_tokens=2096,
44
- temperature=0.5,
45
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
46
- custom_role_conversions=None,
47
  )
48
 
49
 
50
  # Import tool from Hub
51
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
52
 
53
- with open("prompts.yaml", 'r') as stream:
54
  prompt_templates = yaml.safe_load(stream)
55
-
56
  agent = CodeAgent(
57
  model=model,
58
- tools=[final_answer], ## add your tools here (don't remove final answer)
 
 
 
 
 
 
 
59
  max_steps=6,
60
  verbosity_level=1,
61
  grammar=None,
62
  planning_interval=None,
63
  name=None,
64
  description=None,
65
- prompt_templates=prompt_templates
 
66
  )
67
 
68
 
69
- GradioUI(agent).launch()
 
1
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
2
  import datetime
3
  import requests
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
7
+ from tools.visit_webpage import VisitWebpageTool
8
+ from tools.web_search import DuckDuckGoSearchTool
9
+ import os
10
+
11
+ from dotenv import load_dotenv
12
+
13
+
14
+ load_dotenv(override=True)
15
 
16
  from Gradio_UI import GradioUI
17
 
18
+
19
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
20
  @tool
21
+ def my_custom_tool(
22
+ arg1: str, arg2: int
23
+ ) -> str: # it's import to specify the return type
24
+ # Keep this format for the description / args / args description but feel free to modify the tool
25
+ """A tool that does nothing yet
26
  Args:
27
  arg1: the first argument
28
  arg2: the second argument
29
  """
30
  return "What magic will you build ?"
31
 
32
+
33
+ @tool
34
+ def get_crypto_price(coin_name: str) -> str:
35
+ """Get latest crypto price in usd for a given coin name
36
+
37
+ Args:
38
+ coin_name: The unique name of the coin in lowercase. e.g. bitcoin.
39
+ """
40
+ api_key = os.getenv("COINGECKO_API_KEY")
41
+ headers = {"accept": "application/json", "x-cg-demo-api-key": api_key}
42
+ url = f"https://api.coingecko.com/api/v3/simple/price?ids={coin_name}&vs_currencies=usd"
43
+
44
+ response = requests.get(url, headers=headers)
45
+ data = response.json()
46
+ if coin_name in data and "usd" in data[coin_name]:
47
+ price = data[coin_name]["usd"]
48
+ return price
49
+ else:
50
+ return None
51
+
52
+
53
  @tool
54
  def get_current_time_in_timezone(timezone: str) -> str:
55
  """A tool that fetches the current local time in a specified timezone.
 
66
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
67
 
68
 
69
+ search_tool = DuckDuckGoSearchTool(max_results=10)
70
+ visit_webpage_tool = VisitWebpageTool()
71
+
72
  final_answer = FinalAnswerTool()
73
 
74
  # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
75
+ # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
76
 
77
  model = HfApiModel(
78
+ max_tokens=2096,
79
+ temperature=0.5,
80
+ model_id="Qwen/Qwen2.5-Coder-32B-Instruct", # it is possible that this model may be overloaded
81
+ custom_role_conversions=None,
82
  )
83
 
84
 
85
  # Import tool from Hub
86
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
87
 
88
+ with open("prompts.yaml", "r") as stream:
89
  prompt_templates = yaml.safe_load(stream)
90
+
91
  agent = CodeAgent(
92
  model=model,
93
+ tools=[
94
+ final_answer,
95
+ visit_webpage_tool,
96
+ search_tool,
97
+ get_current_time_in_timezone,
98
+ get_crypto_price,
99
+ image_generation_tool,
100
+ ],
101
  max_steps=6,
102
  verbosity_level=1,
103
  grammar=None,
104
  planning_interval=None,
105
  name=None,
106
  description=None,
107
+ prompt_templates=prompt_templates,
108
+ # authorized_imports=["requests", "random", "numpy", "pandas"],
109
  )
110
 
111
 
112
+ GradioUI(agent).launch()
tools/web_search.py CHANGED
@@ -1,11 +1,14 @@
1
  from typing import Any, Optional
2
- from smolagents.tools import Tool
3
  import duckduckgo_search
4
 
 
5
  class DuckDuckGoSearchTool(Tool):
6
  name = "web_search"
7
  description = "Performs a duckduckgo web search based on your query (think a Google search) then returns the top search results."
8
- inputs = {'query': {'type': 'string', 'description': 'The search query to perform.'}}
 
 
9
  output_type = "string"
10
 
11
  def __init__(self, max_results=10, **kwargs):
@@ -23,5 +26,8 @@ class DuckDuckGoSearchTool(Tool):
23
  results = self.ddgs.text(query, max_results=self.max_results)
24
  if len(results) == 0:
25
  raise Exception("No results found! Try a less restrictive/shorter query.")
26
- postprocessed_results = [f"[{result['title']}]({result['href']})\n{result['body']}" for result in results]
 
 
 
27
  return "## Search Results\n\n" + "\n\n".join(postprocessed_results)
 
1
  from typing import Any, Optional
2
+ from smolagents import Tool
3
  import duckduckgo_search
4
 
5
+
6
  class DuckDuckGoSearchTool(Tool):
7
  name = "web_search"
8
  description = "Performs a duckduckgo web search based on your query (think a Google search) then returns the top search results."
9
+ inputs = {
10
+ "query": {"type": "string", "description": "The search query to perform."}
11
+ }
12
  output_type = "string"
13
 
14
  def __init__(self, max_results=10, **kwargs):
 
26
  results = self.ddgs.text(query, max_results=self.max_results)
27
  if len(results) == 0:
28
  raise Exception("No results found! Try a less restrictive/shorter query.")
29
+ postprocessed_results = [
30
+ f"[{result['title']}]({result['href']})\n{result['body']}"
31
+ for result in results
32
+ ]
33
  return "## Search Results\n\n" + "\n\n".join(postprocessed_results)