2MoOn24mOoN4 commited on
Commit
f27e78c
·
verified ·
1 Parent(s): 137a035

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +30 -23
agent.py CHANGED
@@ -64,34 +64,41 @@ def process_tool_output(tool_output):
64
  return tool_output.get('title', str(tool_output))
65
  return str(tool_output)
66
 
67
- class CustomDuckDuckGoSearchTool(Tool):
68
- name = "CustomDuckDuckGoSearchTool"
69
- description = "Perform a DuckDuckGo search and return a string result."
70
- def __call__(self, query: str) -> str:
71
-
72
- try:
73
- with DDGS() as ddgs:
74
- results = list(ddgs.text(query, max_results=3)) # Convert generator to list
 
 
 
75
  return process_tool_output(results)
76
- except Exception as e:
77
  print(f"Error in DuckDuckGoSearchTool: {e}")
78
  return f"Search error: {e}"
79
  custome_duck_search = CustomDuckDuckGoSearchTool()
80
 
81
- class CustomVisitWebpageTool(Tool):
82
- name = "CustomVisitWebpageTool"
83
- description = "Visit a webpage and extract content as a string."
84
- def __call__(self, url: str) -> str:
85
- try:
86
- response = requests.get(url, timeout=10)
87
- response.raise_for_status()
88
- soup = BeautifulSoup(response.text, 'html.parser')
89
- # Extract text from the webpage
90
- text = soup.get_text(separator=' ', strip=True)
91
- return text[:1000] # Limit output length to avoid overload
92
- except Exception as e:
93
- print(f"Error in VisitWebpageTool: {e}")
94
- return f"Webpage access error: {e}"
 
 
 
 
95
  custome_web_search = CustomVisitWebpageTool()
96
 
97
  #=======Agent========#
 
64
  return tool_output.get('title', str(tool_output))
65
  return str(tool_output)
66
 
67
+ @tool
68
+ def CustomDuckDuckGoSearchTool(query:str): -> str
69
+ """
70
+ Perform a DuckDuckGo search and return a string result.
71
+
72
+ Args:
73
+ query: the question you input
74
+ """
75
+ try:
76
+ with DDGS() as ddgs:
77
+ results = list(ddgs.text(query, max_results=3)) # Convert generator to list
78
  return process_tool_output(results)
79
+ except Exception as e:
80
  print(f"Error in DuckDuckGoSearchTool: {e}")
81
  return f"Search error: {e}"
82
  custome_duck_search = CustomDuckDuckGoSearchTool()
83
 
84
+ @tool
85
+ def CustomVisitWebpageTool(query:str):->str
86
+ """
87
+ Visit a webpage and extract content as a string.
88
+
89
+ Args:
90
+ query: the question you input
91
+ """
92
+ try:
93
+ response = requests.get(url, timeout=10)
94
+ response.raise_for_status()
95
+ soup = BeautifulSoup(response.text, 'html.parser')
96
+ # Extract text from the webpage
97
+ text = soup.get_text(separator=' ', strip=True)
98
+ return text[:1000] # Limit output length to avoid overload
99
+ except Exception as e:
100
+ print(f"Error in VisitWebpageTool: {e}")
101
+ return f"Webpage access error: {e}"
102
  custome_web_search = CustomVisitWebpageTool()
103
 
104
  #=======Agent========#