-
Notifications
You must be signed in to change notification settings - Fork 8
feat: migrate web search to support Tavily alongside DuckDuckGo #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| TELEGRAM_BOT_TOKEN= | ||
| TELEGRAM_CHAT_ID= | ||
| GBYTE_ERP_URL="" | ||
| API_KEY=secret-agent-key | ||
| API_KEY=secret-agent-key | ||
| TAVILY_API_KEY= |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,7 @@ pyyaml | |
| dateparser | ||
| pytz | ||
| ddgs | ||
| tavily-python | ||
| youtube-transcript-api | ||
| python-dateutil | ||
| playwright | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,23 +2,44 @@ | |||||||||||
| Web Search Tool — Search the web and summarize content. | ||||||||||||
| """ | ||||||||||||
| import logging | ||||||||||||
| import os | ||||||||||||
| import re | ||||||||||||
| from langchain_core.tools import tool | ||||||||||||
| import config as app_config | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| def perform_web_search(query): | ||||||||||||
| """Performs a DuckDuckGo search.""" | ||||||||||||
| def _tavily_search(query): | ||||||||||||
| """Performs a web search using Tavily.""" | ||||||||||||
| from tavily import TavilyClient | ||||||||||||
| client = TavilyClient() | ||||||||||||
| response = client.search(query=query, max_results=5, search_depth="basic") | ||||||||||||
| results = response.get("results", []) | ||||||||||||
| if not results: | ||||||||||||
| return "No results found." | ||||||||||||
| summary = "" | ||||||||||||
| for r in results: | ||||||||||||
| summary += f"- [{r['title']}]({r['url']}): {r['content']}\n" | ||||||||||||
| return summary | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| def _ddgs_search(query): | ||||||||||||
| """Performs a web search using DuckDuckGo.""" | ||||||||||||
| from ddgs import DDGS | ||||||||||||
| results = DDGS().text(query, max_results=5) | ||||||||||||
| if not results: | ||||||||||||
| return "No results found." | ||||||||||||
| summary = "" | ||||||||||||
| for r in results: | ||||||||||||
| summary += f"- [{r['title']}]({r['href']}): {r['body']}\n" | ||||||||||||
| return summary | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| def perform_web_search(query): | ||||||||||||
| """Performs a web search using Tavily (if TAVILY_API_KEY is set) or DuckDuckGo.""" | ||||||||||||
| try: | ||||||||||||
| results = DDGS().text(query, max_results=5) | ||||||||||||
| if not results: | ||||||||||||
| return "No results found." | ||||||||||||
|
|
||||||||||||
| summary = "" | ||||||||||||
| for r in results: | ||||||||||||
| summary += f"- [{r['title']}]({r['href']}): {r['body']}\n" | ||||||||||||
| return summary | ||||||||||||
| if os.environ.get("TAVILY_API_KEY"): | ||||||||||||
| return _tavily_search(query) | ||||||||||||
| return _ddgs_search(query) | ||||||||||||
| except Exception as e: | ||||||||||||
| return f"Error performing search: {e}" | ||||||||||||
|
Comment on lines
43
to
44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This
Suggested change
|
||||||||||||
|
|
||||||||||||
|
|
||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
DDGS().text()method in theduckduckgo_searchlibrary (and similar wrappers) often returns a generator. In Python, a generator object is always truthy, so theif not resultscheck will fail to detect empty results. Converting the results to a list ensures the check works correctly and allows for safe iteration.