-
Notifications
You must be signed in to change notification settings - Fork 3.1k
feat: add Tavily as parallel search provider option in backend #208
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
Open
tavily-integrations
wants to merge
1
commit into
google-gemini:main
Choose a base branch
from
Tavily-FDE:feat/tavily-migration/backend-google-search
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| # GEMINI_API_KEY= | ||
| # GEMINI_API_KEY= | ||
| # TAVILY_API_KEY= # Required when search_provider=tavily |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ dependencies = [ | |
| "langgraph-api", | ||
| "fastapi", | ||
| "google-genai", | ||
| "tavily-python", | ||
| ] | ||
|
|
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |||||||||||||||||||||||||||
| from langgraph.graph import START, END | ||||||||||||||||||||||||||||
| from langchain_core.runnables import RunnableConfig | ||||||||||||||||||||||||||||
| from google.genai import Client | ||||||||||||||||||||||||||||
| from tavily import TavilyClient | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| from agent.state import ( | ||||||||||||||||||||||||||||
| OverallState, | ||||||||||||||||||||||||||||
|
|
@@ -93,9 +94,10 @@ def continue_to_web_research(state: QueryGenerationState): | |||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def web_research(state: WebSearchState, config: RunnableConfig) -> OverallState: | ||||||||||||||||||||||||||||
| """LangGraph node that performs web research using the native Google Search API tool. | ||||||||||||||||||||||||||||
| """LangGraph node that performs web research using Google Search or Tavily. | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| Executes a web search using the native Google Search API tool in combination with Gemini 2.0 Flash. | ||||||||||||||||||||||||||||
| Executes a web search using the configured search provider. When search_provider | ||||||||||||||||||||||||||||
| is 'tavily', uses the Tavily API; otherwise defaults to Google Search grounding. | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| Args: | ||||||||||||||||||||||||||||
| state: Current graph state containing the search query and research loop count | ||||||||||||||||||||||||||||
|
|
@@ -104,8 +106,16 @@ def web_research(state: WebSearchState, config: RunnableConfig) -> OverallState: | |||||||||||||||||||||||||||
| Returns: | ||||||||||||||||||||||||||||
| Dictionary with state update, including sources_gathered, research_loop_count, and web_research_results | ||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||
| # Configure | ||||||||||||||||||||||||||||
| configurable = Configuration.from_runnable_config(config) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if configurable.search_provider == "tavily": | ||||||||||||||||||||||||||||
| return _web_research_tavily(state, configurable) | ||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||
| return _web_research_google(state, configurable) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def _web_research_google(state: WebSearchState, configurable: Configuration) -> OverallState: | ||||||||||||||||||||||||||||
| """Perform web research using Google Search grounding via Gemini.""" | ||||||||||||||||||||||||||||
| formatted_prompt = web_searcher_instructions.format( | ||||||||||||||||||||||||||||
| current_date=get_current_date(), | ||||||||||||||||||||||||||||
| research_topic=state["search_query"], | ||||||||||||||||||||||||||||
|
|
@@ -136,6 +146,42 @@ def web_research(state: WebSearchState, config: RunnableConfig) -> OverallState: | |||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def _web_research_tavily(state: WebSearchState, configurable: Configuration) -> OverallState: | ||||||||||||||||||||||||||||
| """Perform web research using the Tavily search API.""" | ||||||||||||||||||||||||||||
| tavily_client = TavilyClient() | ||||||||||||||||||||||||||||
| response = tavily_client.search( | ||||||||||||||||||||||||||||
| query=state["search_query"], | ||||||||||||||||||||||||||||
| max_results=5, | ||||||||||||||||||||||||||||
| search_depth="advanced", | ||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||
|
Comment on lines
+151
to
+156
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:
This suggestion addresses both points and assumes you add
Suggested change
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Build sources and research text matching the existing schema | ||||||||||||||||||||||||||||
| sources_gathered = [] | ||||||||||||||||||||||||||||
| research_parts = [] | ||||||||||||||||||||||||||||
| for idx, result in enumerate(response.get("results", [])): | ||||||||||||||||||||||||||||
| url = result.get("url", "") | ||||||||||||||||||||||||||||
| title = result.get("title", "") | ||||||||||||||||||||||||||||
| content = result.get("content", "") | ||||||||||||||||||||||||||||
| short_url = f"https://tavily.com/id/{state['id']}-{idx}" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # label: use the domain or title | ||||||||||||||||||||||||||||
| label = title.split(" - ")[0] if title else url | ||||||||||||||||||||||||||||
| sources_gathered.append({ | ||||||||||||||||||||||||||||
| "label": label, | ||||||||||||||||||||||||||||
| "short_url": short_url, | ||||||||||||||||||||||||||||
| "value": url, | ||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||
| research_parts.append(f"{content} [{label}]({short_url})") | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| modified_text = "\n\n".join(research_parts) if research_parts else "" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||
| "sources_gathered": sources_gathered, | ||||||||||||||||||||||||||||
| "search_query": [state["search_query"]], | ||||||||||||||||||||||||||||
| "web_research_result": [modified_text], | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def reflection(state: OverallState, config: RunnableConfig) -> ReflectionState: | ||||||||||||||||||||||||||||
| """LangGraph node that identifies knowledge gaps and generates potential follow-up queries. | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
To make the Tavily integration more flexible, consider making
max_resultsandsearch_depthconfigurable instead of hardcoding them ingraph.py. This allows adjusting the search behavior without changing the code.