-
Notifications
You must be signed in to change notification settings - Fork 298
feat(google_genai): add google_server_tools to GoogleGenAIChatGenerator #3571
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
Aftabbs
wants to merge
2
commits into
deepset-ai:main
Choose a base branch
from
Aftabbs:feat/google-genai-server-tools
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
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 |
|---|---|---|
|
|
@@ -253,6 +253,33 @@ def test_serde_with_mixed_tools_and_toolsets(self, monkeypatch): | |
| assert restored._tools[0].name == "tool1" | ||
| assert len(restored._tools[1]) == 1 | ||
|
|
||
| def test_init_with_google_server_tools(self, monkeypatch): | ||
| monkeypatch.setenv("GOOGLE_API_KEY", "test-api-key") | ||
| server_tool = {"google_search": {}} | ||
| component = GoogleGenAIChatGenerator(google_server_tools=[server_tool]) | ||
| assert component._google_server_tools == [server_tool] | ||
|
|
||
| def test_to_dict_with_google_server_tools(self, monkeypatch): | ||
| monkeypatch.setenv("GOOGLE_API_KEY", "test-api-key") | ||
| server_tool = {"google_search": {}} | ||
| component = GoogleGenAIChatGenerator(google_server_tools=[server_tool]) | ||
| data = component.to_dict() | ||
| assert data["init_parameters"]["google_server_tools"] == [server_tool] | ||
|
|
||
| def test_from_dict_with_google_server_tools(self, monkeypatch): | ||
| monkeypatch.setenv("GOOGLE_API_KEY", "test-api-key") | ||
| server_tool = {"google_search": {}} | ||
| component = GoogleGenAIChatGenerator(google_server_tools=[server_tool]) | ||
| data = component.to_dict() | ||
| restored = GoogleGenAIChatGenerator.from_dict(data) | ||
| assert restored._google_server_tools == [server_tool] | ||
|
|
||
| def test_to_dict_default_google_server_tools_is_none(self, monkeypatch): | ||
| monkeypatch.setenv("GOOGLE_API_KEY", "test-api-key") | ||
| component = GoogleGenAIChatGenerator() | ||
| data = component.to_dict() | ||
| assert data["init_parameters"]["google_server_tools"] is None | ||
|
|
||
| def test_to_dict_with_response_format_pydantic(self, monkeypatch): | ||
| """Test that to_dict serializes a Pydantic response_format to a JSON schema dict.""" | ||
| monkeypatch.setenv("GOOGLE_API_KEY", "test-api-key") | ||
|
|
@@ -365,6 +392,34 @@ def test_run_with_tools_passes_tools_to_config(self, monkeypatch, mock_response, | |
| config = call_kwargs.kwargs.get("config") or call_kwargs[1].get("config") | ||
| assert config.tools is not None | ||
|
|
||
| def test_run_with_google_server_tools(self, monkeypatch, mock_response): | ||
| monkeypatch.setenv("GOOGLE_API_KEY", "test-api-key") | ||
| server_tool = {"google_search": {}} | ||
| component = GoogleGenAIChatGenerator(google_server_tools=[server_tool]) | ||
| component._client.models.generate_content = Mock(return_value=mock_response) | ||
|
|
||
| component.run([ChatMessage.from_user("Search for AI news")]) | ||
|
|
||
| call_kwargs = component._client.models.generate_content.call_args | ||
| config = call_kwargs.kwargs.get("config") or call_kwargs[1].get("config") | ||
| assert config.tools is not None | ||
| assert len(config.tools) == 1 | ||
|
|
||
| def test_run_merges_haystack_and_google_server_tools(self, monkeypatch, mock_response, tools): | ||
| monkeypatch.setenv("GOOGLE_API_KEY", "test-api-key") | ||
| server_tool = {"google_search": {}} | ||
| component = GoogleGenAIChatGenerator(google_server_tools=[server_tool]) | ||
| component._client.models.generate_content = Mock(return_value=mock_response) | ||
|
|
||
| component.run([ChatMessage.from_user("Search for AI news")], tools=tools) | ||
|
|
||
| call_kwargs = component._client.models.generate_content.call_args | ||
| config = call_kwargs.kwargs.get("config") or call_kwargs[1].get("config") | ||
| assert config.tools is not None | ||
| # _convert_tools_to_google_genai_format bundles all function declarations into one Tool object, | ||
| # plus one Tool object for google_search | ||
| assert len(config.tools) == 2 | ||
|
|
||
|
Member
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. Let's add a live integration test in the appropriate section. |
||
| def test_run_with_safety_settings(self, monkeypatch, mock_response): | ||
| monkeypatch.setenv("GOOGLE_API_KEY", "test-api-key") | ||
| safety = [{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"}] | ||
|
|
@@ -726,6 +781,19 @@ def test_live_run_with_parallel_tools(self, tools): | |
| # Check that the response mentions both temperature readings | ||
| assert "22" in final_message.text and "15" in final_message.text | ||
|
|
||
| def test_live_run_with_google_server_tools(self): | ||
| """ | ||
| Integration test that google_server_tools (Google Search) is accepted by the API and produces a response. | ||
| """ | ||
| component = GoogleGenAIChatGenerator(google_server_tools=[{"google_search": {}}]) | ||
| results = component.run([ChatMessage.from_user("What is the capital of France?")]) | ||
|
|
||
| assert len(results["replies"]) == 1 | ||
| message = results["replies"][0] | ||
| assert message.text | ||
| assert message.meta["model"] | ||
| assert message.meta["finish_reason"] == "stop" | ||
|
|
||
| def test_live_run_with_thinking(self): | ||
| """ | ||
| Integration test for the thinking feature with a model that supports it. | ||
|
|
||
Oops, something went wrong.
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.
Could you explain how this syntax works?
We should support all Google built-in tools if possible, also allowing to pass potential parameters they accept.
So let's verify that this syntax is functional for this goal or choose another one; also provide users with an example in docstring and a link to Google tools.