Spaces:
Sleeping
Sleeping
Update GeneralAgent.py
Browse files- GeneralAgent.py +64 -37
GeneralAgent.py
CHANGED
@@ -47,8 +47,11 @@ WELCOME_MSG = "Welcome to my general-purpose AI agent. Type `q` to quit. How sha
|
|
47 |
@tool
|
48 |
def wikipedia_search_tool(title: str) -> str:
|
49 |
"""Provides an excerpt from a Wikipedia article with the given title."""
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
52 |
|
53 |
@tool
|
54 |
def media_tool(file_path: str) -> str:
|
@@ -58,26 +61,35 @@ def media_tool(file_path: str) -> str:
|
|
58 |
@tool
|
59 |
def internet_search_tool(search_query: str) -> str:
|
60 |
"""Does a google search with using the input as the search query. Returns a long batch of textual information related to the query."""
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
64 |
|
65 |
@tool
|
66 |
def webscraper_tool(url: str) -> str:
|
67 |
"""Returns the page's html content from the input url."""
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
|
|
|
|
|
|
75 |
|
76 |
@tool
|
77 |
def read_excel_tool(file_path: str) -> str:
|
78 |
"""Returns the contents of an Excel file as a Pandas dataframe."""
|
79 |
-
|
80 |
-
|
|
|
|
|
|
|
81 |
|
82 |
class AgenticAI:
|
83 |
def __init__(self):
|
@@ -151,35 +163,50 @@ class AgenticAI:
|
|
151 |
tool_args = tool_call["args"]
|
152 |
|
153 |
if tool_name == "wikipedia_search_tool":
|
154 |
-
|
155 |
-
|
156 |
-
|
|
|
|
|
|
|
157 |
elif tool_name == "media_tool":
|
158 |
-
|
159 |
-
|
|
|
|
|
|
|
160 |
elif tool_name == "internet_search_tool":
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
|
|
|
|
|
|
165 |
elif tool_name == "webscraper_tool":
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
|
|
|
|
|
|
175 |
elif tool_name == "read_excel_tool":
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
|
|
|
|
|
|
180 |
|
181 |
else:
|
182 |
-
|
183 |
|
184 |
outbound_msgs.append(
|
185 |
ToolMessage(
|
|
|
47 |
@tool
|
48 |
def wikipedia_search_tool(title: str) -> str:
|
49 |
"""Provides an excerpt from a Wikipedia article with the given title."""
|
50 |
+
try:
|
51 |
+
page = wikipedia.page(title, auto_suggest=False)
|
52 |
+
return page.content[:3000]
|
53 |
+
except Exception as e:
|
54 |
+
return f"Error during processing: {e}"
|
55 |
|
56 |
@tool
|
57 |
def media_tool(file_path: str) -> str:
|
|
|
61 |
@tool
|
62 |
def internet_search_tool(search_query: str) -> str:
|
63 |
"""Does a google search with using the input as the search query. Returns a long batch of textual information related to the query."""
|
64 |
+
try:
|
65 |
+
search_tool = DuckDuckGoSearchTool()
|
66 |
+
result = search_tool(search_query)
|
67 |
+
return result
|
68 |
+
except Exception as e:
|
69 |
+
return f"Error during processing: {e}"
|
70 |
|
71 |
@tool
|
72 |
def webscraper_tool(url: str) -> str:
|
73 |
"""Returns the page's html content from the input url."""
|
74 |
+
try:
|
75 |
+
response = requests.get(url, stream=True)
|
76 |
+
if response.status_code == 200:
|
77 |
+
soup = BeautifulSoup(response.content, 'html.parser')
|
78 |
+
html_text = soup.get_text()
|
79 |
+
return html_text
|
80 |
+
else:
|
81 |
+
return f"Failed to retrieve the webpage. Status code: {response.status_code}"
|
82 |
+
except Exception as e:
|
83 |
+
return f"Error during processing: {e}"
|
84 |
|
85 |
@tool
|
86 |
def read_excel_tool(file_path: str) -> str:
|
87 |
"""Returns the contents of an Excel file as a Pandas dataframe."""
|
88 |
+
try:
|
89 |
+
df = pd.read_excel(file_path, engine = "openpyxl")
|
90 |
+
return df.to_string(index=False)
|
91 |
+
except Exception as e:
|
92 |
+
return f"Error during processing: {e}"
|
93 |
|
94 |
class AgenticAI:
|
95 |
def __init__(self):
|
|
|
163 |
tool_args = tool_call["args"]
|
164 |
|
165 |
if tool_name == "wikipedia_search_tool":
|
166 |
+
try:
|
167 |
+
print(f"called wikipedia with {str(tool_args)}")
|
168 |
+
page = wikipedia.page(tool_args.get("title"), auto_suggest=False)
|
169 |
+
response = page.content[:3000]
|
170 |
+
except Exception as e:
|
171 |
+
response = e
|
172 |
elif tool_name == "media_tool":
|
173 |
+
try:
|
174 |
+
print(f"called media with {str(tool_args)}")
|
175 |
+
response = "This tool hasn't been implemented yet. Please return 0 if the task cannot be solved without knowing the contents of this file."
|
176 |
+
except Exception as e:
|
177 |
+
response = e
|
178 |
elif tool_name == "internet_search_tool":
|
179 |
+
try:
|
180 |
+
print(f"called internet with {str(tool_args)}")
|
181 |
+
question = tool_args.get("search_query")
|
182 |
+
search_tool = DuckDuckGoSearchTool()
|
183 |
+
response = search_tool(question)[:3000]
|
184 |
+
except Exception as e:
|
185 |
+
response = e
|
186 |
elif tool_name == "webscraper_tool":
|
187 |
+
try:
|
188 |
+
print(f"called webscraper with {str(tool_args)}")
|
189 |
+
url = tool_args.get("url")
|
190 |
+
response = requests.get(url, stream=True)
|
191 |
+
if response.status_code == 200:
|
192 |
+
soup = BeautifulSoup(response.content, 'html.parser')
|
193 |
+
html_text = soup.get_text()
|
194 |
+
response = html_text
|
195 |
+
else:
|
196 |
+
response = f"Failed to retrieve the webpage. Status code: {response.status_code}"
|
197 |
+
except Exception as e:
|
198 |
+
response = e
|
199 |
elif tool_name == "read_excel_tool":
|
200 |
+
try:
|
201 |
+
print(f"called excel with {str(tool_args)}")
|
202 |
+
path = tool_args.get("file_path")
|
203 |
+
df = pd.read_excel(path, engine = "openpyxl")
|
204 |
+
response = df
|
205 |
+
except Exception as e:
|
206 |
+
response = e
|
207 |
|
208 |
else:
|
209 |
+
response = f'Unknown tool call: {tool_name}'
|
210 |
|
211 |
outbound_msgs.append(
|
212 |
ToolMessage(
|