APRG commited on
Commit
45d767e
·
verified ·
1 Parent(s): b2df3b1

Update GeneralAgent.py

Browse files
Files changed (1) hide show
  1. 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
- page = wikipedia.page(title, auto_suggest=False)
51
- return page.content[:3000]
 
 
 
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
- search_tool = DuckDuckGoSearchTool()
62
- result = search_tool(search_query)
63
- return result
 
 
 
64
 
65
  @tool
66
  def webscraper_tool(url: str) -> str:
67
  """Returns the page's html content from the input url."""
68
- response = requests.get(url, stream=True)
69
- if response.status_code == 200:
70
- soup = BeautifulSoup(response.content, 'html.parser')
71
- html_text = soup.get_text()
72
- return html_text
73
- else:
74
- raise Exception(f"Failed to retrieve the webpage. Status code: {response.status_code}")
 
 
 
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
- df = pd.read_excel(file_path, engine = "openpyxl")
80
- return df.to_string(index=False)
 
 
 
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
- print(f"called wikipedia with {str(tool_args)}")
155
- page = wikipedia.page(tool_args.get("title"), auto_suggest=False)
156
- response = page.content[:3000]
 
 
 
157
  elif tool_name == "media_tool":
158
- print(f"called media with {str(tool_args)}")
159
- response = "This tool hasn't been implemented yet. Please return 0 if the task cannot be solved without knowing the contents of this file."
 
 
 
160
  elif tool_name == "internet_search_tool":
161
- print(f"called internet with {str(tool_args)}")
162
- question = tool_args.get("search_query")
163
- search_tool = DuckDuckGoSearchTool()
164
- response = search_tool(question)[:3000]
 
 
 
165
  elif tool_name == "webscraper_tool":
166
- print(f"called webscraper with {str(tool_args)}")
167
- url = tool_args.get("url")
168
- response = requests.get(url, stream=True)
169
- if response.status_code == 200:
170
- soup = BeautifulSoup(response.content, 'html.parser')
171
- html_text = soup.get_text()
172
- response = html_text
173
- else:
174
- response = Exception(f"Failed to retrieve the webpage. Status code: {response.status_code}")
 
 
 
175
  elif tool_name == "read_excel_tool":
176
- print(f"called excel with {str(tool_args)}")
177
- path = tool_args.get("file_path")
178
- df = pd.read_excel(path, engine = "openpyxl")
179
- response = df
 
 
 
180
 
181
  else:
182
- raise NotImplementedError(f'Unknown tool call: {tool_name}')
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(