Update agent.py
Browse files
agent.py
CHANGED
@@ -221,29 +221,28 @@ def create_rag_tool(documents: List[Document]) -> QueryEngineTool:
|
|
221 |
|
222 |
# 1. Create the base DuckDuckGo search tool from the official spec.
|
223 |
# This tool returns text summaries of search results, not just URLs.
|
224 |
-
base_duckduckgo_tool = DuckDuckGoSearchToolSpec().to_tool_list()[
|
225 |
|
226 |
# 2. Define a wrapper function to post-process the output.
|
227 |
def search_and_extract_top_url(query: str) -> str:
|
228 |
"""
|
229 |
Takes a search query, uses the base DuckDuckGo search tool to get results,
|
230 |
and then parses the output to extract and return only the first URL.
|
231 |
-
|
232 |
Args:
|
233 |
query: The natural language search query.
|
234 |
-
|
235 |
Returns:
|
236 |
A string containing the first URL found, or an error message if none is found.
|
237 |
"""
|
238 |
# Call the base tool to get the search results as text
|
239 |
-
search_results = base_duckduckgo_tool(query)
|
|
|
240 |
|
241 |
# Use a regular expression to find the first URL in the text output
|
242 |
# The \S+ pattern matches any sequence of non-whitespace characters
|
243 |
url_match = re.search(r"https?://\S+", str(search_results))
|
244 |
|
245 |
if url_match:
|
246 |
-
return url_match.group(0)
|
247 |
else:
|
248 |
return "No URL could be extracted from the search results."
|
249 |
|
|
|
221 |
|
222 |
# 1. Create the base DuckDuckGo search tool from the official spec.
|
223 |
# This tool returns text summaries of search results, not just URLs.
|
224 |
+
base_duckduckgo_tool = DuckDuckGoSearchToolSpec().to_tool_list()[1]
|
225 |
|
226 |
# 2. Define a wrapper function to post-process the output.
|
227 |
def search_and_extract_top_url(query: str) -> str:
|
228 |
"""
|
229 |
Takes a search query, uses the base DuckDuckGo search tool to get results,
|
230 |
and then parses the output to extract and return only the first URL.
|
|
|
231 |
Args:
|
232 |
query: The natural language search query.
|
|
|
233 |
Returns:
|
234 |
A string containing the first URL found, or an error message if none is found.
|
235 |
"""
|
236 |
# Call the base tool to get the search results as text
|
237 |
+
search_results = base_duckduckgo_tool(query, max_results = 1)
|
238 |
+
print(search_results)
|
239 |
|
240 |
# Use a regular expression to find the first URL in the text output
|
241 |
# The \S+ pattern matches any sequence of non-whitespace characters
|
242 |
url_match = re.search(r"https?://\S+", str(search_results))
|
243 |
|
244 |
if url_match:
|
245 |
+
return url_match.group(0)[:-2]
|
246 |
else:
|
247 |
return "No URL could be extracted from the search results."
|
248 |
|