2MoOn24mOoN4 commited on
Commit
20d36d3
·
verified ·
1 Parent(s): ade294c

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +42 -2
agent.py CHANGED
@@ -1,6 +1,9 @@
1
  import json
2
  import os
3
- from smolagents import CodeAgent, DuckDuckGoSearchTool, VisitWebpageTool, tool, InferenceClientModel
 
 
 
4
 
5
  #========Tools========#
6
  @tool
@@ -52,6 +55,41 @@ def modulus(a: int, b: int) -> int:
52
 
53
  search_tool = DuckDuckGoSearchTool()
54
  web_search = VisitWebpageTool()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  #=======Agent========#
56
  hf_token = os.environ.get("HF_TOKEN")
57
  model = InferenceClientModel(token = hf_token, model_id = 'meta-llama/Llama-2-7b-chat-hf', provider = "auto")
@@ -63,7 +101,9 @@ agent = CodeAgent(
63
  divide,
64
  modulus,
65
  search_tool,
66
- web_search],
 
 
67
  model = model,
68
  add_base_tools=True,
69
  planning_interval=3
 
1
  import json
2
  import os
3
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, VisitWebpageTool, tool, InferenceClientModel, Tool
4
+ from duckduckgo_search import DDGS
5
+ import requests
6
+ from bs4 import BeautifulSoup
7
 
8
  #========Tools========#
9
  @tool
 
55
 
56
  search_tool = DuckDuckGoSearchTool()
57
  web_search = VisitWebpageTool()
58
+
59
+ def process_tool_output(tool_output):
60
+ """Convert tool output to a string."""
61
+ if isinstance(tool_output, list):
62
+ return '\n'.join([item.get('title', str(item)) for item in tool_output if item]) or 'No results found'
63
+ elif isinstance(tool_output, dict):
64
+ return tool_output.get('title', str(tool_output))
65
+ return str(tool_output)
66
+
67
+ class CustomDuckDuckGoSearchTool(Tool):
68
+ def __call__(self, query: str) -> str:
69
+ """Perform a DuckDuckGo search and return a string result."""
70
+ try:
71
+ with DDGS() as ddgs:
72
+ results = list(ddgs.text(query, max_results=3)) # Convert generator to list
73
+ return process_tool_output(results)
74
+ except Exception as e:
75
+ print(f"Error in DuckDuckGoSearchTool: {e}")
76
+ return f"Search error: {e}"
77
+
78
+ class CustomVisitWebpageTool(Tool):
79
+ def __call__(self, url: str) -> str:
80
+ """Visit a webpage and extract content as a string."""
81
+ try:
82
+ response = requests.get(url, timeout=10)
83
+ response.raise_for_status()
84
+ soup = BeautifulSoup(response.text, 'html.parser')
85
+ # Extract text from the webpage
86
+ text = soup.get_text(separator=' ', strip=True)
87
+ return text[:1000] # Limit output length to avoid overload
88
+ except Exception as e:
89
+ print(f"Error in VisitWebpageTool: {e}")
90
+ return f"Webpage access error: {e}"
91
+
92
+
93
  #=======Agent========#
94
  hf_token = os.environ.get("HF_TOKEN")
95
  model = InferenceClientModel(token = hf_token, model_id = 'meta-llama/Llama-2-7b-chat-hf', provider = "auto")
 
101
  divide,
102
  modulus,
103
  search_tool,
104
+ web_search,
105
+ CustomDuckDuckGoSearchTool,
106
+ CustomVisitWebpageTool],
107
  model = model,
108
  add_base_tools=True,
109
  planning_interval=3