-
Notifications
You must be signed in to change notification settings - Fork 12
feat: add Tavily as configurable search backend alongside Serper #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 | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -150,6 +150,42 @@ def contains_chinese_basic(text: str) -> bool: | |||||||||||||||||||||||||||||||
| content = f"A Google search for '{query}' found {len(web_snippets)} results:\n\n## Web Results\n" + "\n\n".join(web_snippets) | ||||||||||||||||||||||||||||||||
| return content | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| def google_search_with_tavily(self, query: str): | ||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||
| from tavily import TavilyClient | ||||||||||||||||||||||||||||||||
| except ImportError: | ||||||||||||||||||||||||||||||||
| return "[WebSearch] tavily-python is not installed." | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| api_key = os.getenv("TAVILY_API_KEY", "").strip() | ||||||||||||||||||||||||||||||||
| if not api_key: | ||||||||||||||||||||||||||||||||
| return "[WebSearch] TAVILY_API_KEY is not set." | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||
| client = TavilyClient(api_key=api_key) | ||||||||||||||||||||||||||||||||
| response = client.search(query=query, max_results=10, search_depth="basic") | ||||||||||||||||||||||||||||||||
| except Exception as exc: | ||||||||||||||||||||||||||||||||
| if search_debug_enabled(): | ||||||||||||||||||||||||||||||||
| print(exc) | ||||||||||||||||||||||||||||||||
| return f"[WebSearch] Tavily request failed for '{query}': {exc}" | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| results = response.get("results", []) | ||||||||||||||||||||||||||||||||
| if not results: | ||||||||||||||||||||||||||||||||
| return f"No results found for '{query}'. Try with a more general query." | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| web_snippets = [] | ||||||||||||||||||||||||||||||||
| for idx, page in enumerate(results, start=1): | ||||||||||||||||||||||||||||||||
| title = str(page.get("title", "Untitled result")) | ||||||||||||||||||||||||||||||||
| link = str(page.get("url", "")) | ||||||||||||||||||||||||||||||||
| snippet = f"\n{page['content']}" if page.get("content") else "" | ||||||||||||||||||||||||||||||||
| redacted_version = f"{idx}. [{title}]({link}){snippet}" | ||||||||||||||||||||||||||||||||
| web_snippets.append(redacted_version) | ||||||||||||||||||||||||||||||||
|
Comment on lines
+176
to
+181
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. Add a type check to ensure each
Suggested change
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if not web_snippets: | ||||||||||||||||||||||||||||||||
| return f"No results found for '{query}'. Try with a more general query." | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| content = f"A Google search for '{query}' found {len(web_snippets)} results:\n\n## Web Results\n" + "\n\n".join(web_snippets) | ||||||||||||||||||||||||||||||||
| return content | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| def call(self, params: Union[str, dict], **kwargs) -> str: | ||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||
| params = self.parse_json_args(params) | ||||||||||||||||||||||||||||||||
|
|
@@ -160,6 +196,9 @@ def call(self, params: Union[str, dict], **kwargs) -> str: | |||||||||||||||||||||||||||||||
| if not isinstance(query, str) or not query.strip(): | ||||||||||||||||||||||||||||||||
| return "[WebSearch] 'query' must be a non-empty string." | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| provider = os.getenv("SEARCH_PROVIDER", "serper").strip().lower() | ||||||||||||||||||||||||||||||||
| if provider == "tavily": | ||||||||||||||||||||||||||||||||
| return self.google_search_with_tavily(query.strip()) | ||||||||||||||||||||||||||||||||
| return self.google_search_with_serp(query.strip()) | ||||||||||||||||||||||||||||||||
|
Comment on lines
+199
to
202
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. Validate the
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.
Defensively check if the
responsereturned by Tavily is a dictionary before calling.get()on it to avoid potentialAttributeErrorif the API returns an unexpected response type.