-
Notifications
You must be signed in to change notification settings - Fork 8
feat: add Tavily search provider to legacy web_search skill #3
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,25 +1,48 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import logging | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import asyncio | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import io | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import os | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import re | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from telegram.ext import ContextTypes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import config | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def perform_web_search(query): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def _perform_tavily_search(query): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from tavily import TavilyClient | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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.get('content', '')}\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return summary | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| except Exception as e: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return f"Error performing search: {e}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def _perform_ddgs_search(query): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from ddgs import DDGS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| except Exception as e: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return f"Error performing search: {e}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
27
to
38
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. There are a couple of improvements that can be made here:
The suggestion below addresses both points.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def perform_web_search(query): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| provider = os.environ.get("SEARCH_PROVIDER", "duckduckgo").lower() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if provider == "tavily": | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return _perform_tavily_search(query) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return _perform_ddgs_search(query) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+42
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. The current implementation silently defaults to DuckDuckGo for any if provider == "tavily":
return _perform_tavily_search(query)
elif provider == "duckduckgo":
return _perform_ddgs_search(query)
else:
logging.warning(f"Unknown SEARCH_PROVIDER '{provider}', defaulting to duckduckgo.")
return _perform_ddgs_search(query) |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def get_youtube_video_id(url): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Patterns: youtube.com/watch?v=ID, youtu.be/ID, youtube.com/embed/ID | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| patterns = [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | ||
|
|
||
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
try...exceptblock does not cover theimportstatement. If thetavily-pythonpackage is not installed, anImportErrorwill be raised and crash the application instead of being handled gracefully. The import should be moved inside thetryblock. Additionally, it's good practice to log exceptions before returning an error message to aid in debugging.