google-genai-haystack 7.0.0__tar.gz → 7.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {google_genai_haystack-7.0.0 → google_genai_haystack-7.2.0}/CHANGELOG.md +22 -0
- {google_genai_haystack-7.0.0 → google_genai_haystack-7.2.0}/PKG-INFO +2 -2
- {google_genai_haystack-7.0.0 → google_genai_haystack-7.2.0}/pydoc/config_docusaurus.yml +1 -0
- {google_genai_haystack-7.0.0 → google_genai_haystack-7.2.0}/pyproject.toml +1 -1
- {google_genai_haystack-7.0.0 → google_genai_haystack-7.2.0}/src/haystack_integrations/components/generators/google_genai/__init__.py +2 -1
- {google_genai_haystack-7.0.0 → google_genai_haystack-7.2.0}/src/haystack_integrations/components/generators/google_genai/chat/chat_generator.py +9 -0
- google_genai_haystack-7.2.0/src/haystack_integrations/components/generators/google_genai/chat/interactions_chat_generator.py +412 -0
- google_genai_haystack-7.2.0/src/haystack_integrations/components/generators/google_genai/chat/interactions_utils.py +593 -0
- {google_genai_haystack-7.0.0 → google_genai_haystack-7.2.0}/src/haystack_integrations/components/generators/google_genai/chat/utils.py +4 -1
- {google_genai_haystack-7.0.0 → google_genai_haystack-7.2.0}/tests/test_chat_generator.py +62 -0
- google_genai_haystack-7.2.0/tests/test_interactions_chat_generator.py +439 -0
- google_genai_haystack-7.2.0/tests/test_interactions_chat_generator_utils.py +594 -0
- {google_genai_haystack-7.0.0 → google_genai_haystack-7.2.0}/.gitignore +0 -0
- {google_genai_haystack-7.0.0 → google_genai_haystack-7.2.0}/LICENSE.txt +0 -0
- {google_genai_haystack-7.0.0 → google_genai_haystack-7.2.0}/README.md +0 -0
- {google_genai_haystack-7.0.0 → google_genai_haystack-7.2.0}/examples/chatgenerator_example.py +0 -0
- {google_genai_haystack-7.0.0 → google_genai_haystack-7.2.0}/src/haystack_integrations/common/google_genai/__init__.py +0 -0
- {google_genai_haystack-7.0.0 → google_genai_haystack-7.2.0}/src/haystack_integrations/common/google_genai/utils.py +0 -0
- {google_genai_haystack-7.0.0 → google_genai_haystack-7.2.0}/src/haystack_integrations/common/py.typed +0 -0
- {google_genai_haystack-7.0.0 → google_genai_haystack-7.2.0}/src/haystack_integrations/components/embedders/google_genai/__init__.py +0 -0
- {google_genai_haystack-7.0.0 → google_genai_haystack-7.2.0}/src/haystack_integrations/components/embedders/google_genai/document_embedder.py +0 -0
- {google_genai_haystack-7.0.0 → google_genai_haystack-7.2.0}/src/haystack_integrations/components/embedders/google_genai/multimodal_document_embedder.py +0 -0
- {google_genai_haystack-7.0.0 → google_genai_haystack-7.2.0}/src/haystack_integrations/components/embedders/google_genai/text_embedder.py +0 -0
- {google_genai_haystack-7.0.0 → google_genai_haystack-7.2.0}/src/haystack_integrations/components/embedders/py.typed +0 -0
- {google_genai_haystack-7.0.0 → google_genai_haystack-7.2.0}/src/haystack_integrations/components/generators/py.typed +0 -0
- {google_genai_haystack-7.0.0 → google_genai_haystack-7.2.0}/src/haystack_integrations/token_counters/google_genai/__init__.py +0 -0
- {google_genai_haystack-7.0.0 → google_genai_haystack-7.2.0}/src/haystack_integrations/token_counters/google_genai/token_counter.py +0 -0
- {google_genai_haystack-7.0.0 → google_genai_haystack-7.2.0}/src/haystack_integrations/token_counters/py.typed +0 -0
- {google_genai_haystack-7.0.0 → google_genai_haystack-7.2.0}/tests/conftest.py +0 -0
- {google_genai_haystack-7.0.0 → google_genai_haystack-7.2.0}/tests/test_chat_generator_utils.py +0 -0
- {google_genai_haystack-7.0.0 → google_genai_haystack-7.2.0}/tests/test_document_embedder.py +0 -0
- {google_genai_haystack-7.0.0 → google_genai_haystack-7.2.0}/tests/test_files/apple.jpg +0 -0
- {google_genai_haystack-7.0.0 → google_genai_haystack-7.2.0}/tests/test_files/banana.png +0 -0
- {google_genai_haystack-7.0.0 → google_genai_haystack-7.2.0}/tests/test_files/sample_docx.docx +0 -0
- {google_genai_haystack-7.0.0 → google_genai_haystack-7.2.0}/tests/test_files/sample_pdf_3.pdf +0 -0
- {google_genai_haystack-7.0.0 → google_genai_haystack-7.2.0}/tests/test_multimodal_document_embedder.py +0 -0
- {google_genai_haystack-7.0.0 → google_genai_haystack-7.2.0}/tests/test_text_embedder.py +0 -0
- {google_genai_haystack-7.0.0 → google_genai_haystack-7.2.0}/tests/test_token_counter.py +0 -0
- {google_genai_haystack-7.0.0 → google_genai_haystack-7.2.0}/tests/test_utils.py +0 -0
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [integrations/google_genai-v7.1.0] - 2026-11-02
|
|
4
|
+
|
|
5
|
+
### 🐛 Bug Fixes
|
|
6
|
+
|
|
7
|
+
- Keep parallel tool calls from separate Gemini stream chunks in GoogleGenAIChatGenerator (#4141)
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
## [integrations/google_genai-v7.0.0] - 2026-10-16
|
|
11
|
+
|
|
12
|
+
### 🚜 Refactor
|
|
13
|
+
|
|
14
|
+
- [**breaking**] Remove from_dict overrides that only call default_from_dict from 14 components (#4059)
|
|
15
|
+
|
|
16
|
+
### 🧪 Testing
|
|
17
|
+
|
|
18
|
+
- Test compatible integrations with Python 3.15; update pyproject (#4104)
|
|
19
|
+
|
|
20
|
+
### 🧹 Chores
|
|
21
|
+
|
|
22
|
+
- [**breaking**] Drop Python 3.10 support (#4105)
|
|
23
|
+
|
|
24
|
+
|
|
3
25
|
## [integrations/google_genai-v6.0.2] - 2026-10-14
|
|
4
26
|
|
|
5
27
|
### 🧹 Chores
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: google-genai-haystack
|
|
3
|
-
Version: 7.
|
|
3
|
+
Version: 7.2.0
|
|
4
4
|
Summary: Use models like Gemini via Google Gen AI SDK
|
|
5
5
|
Project-URL: Documentation, https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/google_genai#readme
|
|
6
6
|
Project-URL: Issues, https://github.com/deepset-ai/haystack-core-integrations/issues
|
|
@@ -19,7 +19,7 @@ Classifier: Programming Language :: Python :: 3.15
|
|
|
19
19
|
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
20
20
|
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
|
21
21
|
Requires-Python: >=3.11
|
|
22
|
-
Requires-Dist: google-genai[aiohttp]>=
|
|
22
|
+
Requires-Dist: google-genai[aiohttp]>=2.17.0
|
|
23
23
|
Requires-Dist: haystack-ai>=3.0.0
|
|
24
24
|
Requires-Dist: jsonref>=1.0.0
|
|
25
25
|
Description-Content-Type: text/markdown
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
loaders:
|
|
2
2
|
- modules:
|
|
3
3
|
- haystack_integrations.components.generators.google_genai.chat.chat_generator
|
|
4
|
+
- haystack_integrations.components.generators.google_genai.chat.interactions_chat_generator
|
|
4
5
|
- haystack_integrations.components.embedders.google_genai.document_embedder
|
|
5
6
|
- haystack_integrations.components.embedders.google_genai.text_embedder
|
|
6
7
|
- haystack_integrations.components.embedders.google_genai.multimodal_document_embedder
|
|
@@ -26,7 +26,7 @@ classifiers = [
|
|
|
26
26
|
"Programming Language :: Python :: Implementation :: CPython",
|
|
27
27
|
"Programming Language :: Python :: Implementation :: PyPy",
|
|
28
28
|
]
|
|
29
|
-
dependencies = ["haystack-ai>=3.0.0", "google-genai[aiohttp]>=
|
|
29
|
+
dependencies = ["haystack-ai>=3.0.0", "google-genai[aiohttp]>=2.17.0", "jsonref>=1.0.0"]
|
|
30
30
|
|
|
31
31
|
[project.urls]
|
|
32
32
|
Documentation = "https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/google_genai#readme"
|
|
@@ -3,5 +3,6 @@
|
|
|
3
3
|
# SPDX-License-Identifier: Apache-2.0
|
|
4
4
|
|
|
5
5
|
from .chat.chat_generator import GoogleGenAIChatGenerator
|
|
6
|
+
from .chat.interactions_chat_generator import GoogleGenAIInteractionsChatGenerator
|
|
6
7
|
|
|
7
|
-
__all__ = ["GoogleGenAIChatGenerator"]
|
|
8
|
+
__all__ = ["GoogleGenAIChatGenerator", "GoogleGenAIInteractionsChatGenerator"]
|
|
@@ -46,6 +46,9 @@ class GoogleGenAIChatGenerator:
|
|
|
46
46
|
Supports models like gemini-3.8-flash and other Gemini variants. For Gemini 2.5 series models,
|
|
47
47
|
enables thinking features via `generation_kwargs={"thinking_budget": value}`.
|
|
48
48
|
|
|
49
|
+
This component uses the `generateContent` API. Some models and quotas, such as the free tier of newer Flash
|
|
50
|
+
models, are only available through the Interactions API: use `GoogleGenAIInteractionsChatGenerator` for those.
|
|
51
|
+
|
|
49
52
|
### Thinking Support (Gemini 2.5 and Gemini 3 Series)
|
|
50
53
|
- **Reasoning transparency**: Models can show their reasoning process
|
|
51
54
|
- **Thought signatures**: Maintains thought context across multi-turn conversations with tools
|
|
@@ -363,6 +366,7 @@ class GoogleGenAIChatGenerator:
|
|
|
363
366
|
try:
|
|
364
367
|
chunks = []
|
|
365
368
|
tool_calls_seen = False
|
|
369
|
+
tool_call_count = 0
|
|
366
370
|
|
|
367
371
|
for i, chunk in enumerate(response_stream):
|
|
368
372
|
streaming_chunk = _convert_google_chunk_to_streaming_chunk(
|
|
@@ -371,8 +375,10 @@ class GoogleGenAIChatGenerator:
|
|
|
371
375
|
component_info=component_info,
|
|
372
376
|
model=self._model,
|
|
373
377
|
tool_calls_seen=tool_calls_seen,
|
|
378
|
+
tool_call_index_offset=tool_call_count,
|
|
374
379
|
)
|
|
375
380
|
tool_calls_seen = tool_calls_seen or bool(streaming_chunk.tool_calls)
|
|
381
|
+
tool_call_count += len(streaming_chunk.tool_calls or [])
|
|
376
382
|
chunks.append(streaming_chunk)
|
|
377
383
|
|
|
378
384
|
# Stream the chunk
|
|
@@ -401,6 +407,7 @@ class GoogleGenAIChatGenerator:
|
|
|
401
407
|
try:
|
|
402
408
|
chunks = []
|
|
403
409
|
tool_calls_seen = False
|
|
410
|
+
tool_call_count = 0
|
|
404
411
|
|
|
405
412
|
i = 0
|
|
406
413
|
chunk = None
|
|
@@ -411,8 +418,10 @@ class GoogleGenAIChatGenerator:
|
|
|
411
418
|
component_info=component_info,
|
|
412
419
|
model=self._model,
|
|
413
420
|
tool_calls_seen=tool_calls_seen,
|
|
421
|
+
tool_call_index_offset=tool_call_count,
|
|
414
422
|
)
|
|
415
423
|
tool_calls_seen = tool_calls_seen or bool(streaming_chunk.tool_calls)
|
|
424
|
+
tool_call_count += len(streaming_chunk.tool_calls or [])
|
|
416
425
|
chunks.append(streaming_chunk)
|
|
417
426
|
|
|
418
427
|
# Stream the chunk
|
|
@@ -0,0 +1,412 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2023-present deepset GmbH <info@deepset.ai>
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
|
|
5
|
+
import inspect
|
|
6
|
+
from typing import Any, ClassVar, Literal
|
|
7
|
+
|
|
8
|
+
from google.genai import Client
|
|
9
|
+
from google.genai.client import AsyncClient
|
|
10
|
+
from haystack import logging
|
|
11
|
+
from haystack.components.generators.utils import _normalize_messages
|
|
12
|
+
from haystack.core.component import component
|
|
13
|
+
from haystack.core.serialization import default_from_dict, default_to_dict
|
|
14
|
+
from haystack.dataclasses import ComponentInfo, StreamingCallbackT, StreamingChunk, select_streaming_callback
|
|
15
|
+
from haystack.dataclasses.chat_message import ChatMessage
|
|
16
|
+
from haystack.tools import (
|
|
17
|
+
ToolsType,
|
|
18
|
+
_check_duplicate_tool_names,
|
|
19
|
+
deserialize_tools_or_toolset_inplace,
|
|
20
|
+
flatten_tools_or_toolsets,
|
|
21
|
+
serialize_tools_or_toolset,
|
|
22
|
+
)
|
|
23
|
+
from haystack.utils import Secret, deserialize_callable, serialize_callable
|
|
24
|
+
from pydantic import BaseModel
|
|
25
|
+
|
|
26
|
+
from haystack_integrations.common.google_genai.utils import _get_client
|
|
27
|
+
from haystack_integrations.components.generators.google_genai.chat.interactions_utils import (
|
|
28
|
+
_aggregate_streaming_chunks,
|
|
29
|
+
_convert_interaction_event_to_streaming_chunk,
|
|
30
|
+
_convert_interaction_to_chat_message,
|
|
31
|
+
_convert_messages_to_interaction_input,
|
|
32
|
+
_convert_tools_to_interactions_format,
|
|
33
|
+
_process_response_format,
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
logger = logging.getLogger(__name__)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
@component
|
|
40
|
+
class GoogleGenAIInteractionsChatGenerator:
|
|
41
|
+
"""
|
|
42
|
+
Generates chat completions with Gemini models through the Gemini Interactions API.
|
|
43
|
+
|
|
44
|
+
The [Interactions API](https://aistudio.google.com/docs/interactions-overview) supports Gemini 2.5 and newer
|
|
45
|
+
models. It also gives access to models and quotas that the `generateContent` API used by
|
|
46
|
+
`GoogleGenAIChatGenerator` doesn't offer, such as the free tier of newer Flash models.
|
|
47
|
+
|
|
48
|
+
The component is stateless like other Haystack Chat Generators: every call sends the full chat history.
|
|
49
|
+
Thought signatures are kept in `ReasoningContent` and sent back automatically, so multi-turn conversations with
|
|
50
|
+
thinking and tools work when you pass previous replies back in the history.
|
|
51
|
+
Server-side conversation state (`previous_interaction_id`) is currently not supported.
|
|
52
|
+
|
|
53
|
+
By default, the Interactions API stores interactions on Google's servers (1 day on the free tier,
|
|
54
|
+
55 days on paid tiers). To opt out, pass `generation_kwargs={"store": False}`.
|
|
55
|
+
|
|
56
|
+
### Authentication
|
|
57
|
+
**Gemini Developer API**: Set the `GOOGLE_API_KEY` or `GEMINI_API_KEY` environment variable.
|
|
58
|
+
**Vertex AI**: Use `api="vertex"` with Application Default Credentials or an API key.
|
|
59
|
+
|
|
60
|
+
### Usage example
|
|
61
|
+
|
|
62
|
+
```python
|
|
63
|
+
from haystack.dataclasses import ChatMessage
|
|
64
|
+
from haystack_integrations.components.generators.google_genai import GoogleGenAIInteractionsChatGenerator
|
|
65
|
+
|
|
66
|
+
chat_generator = GoogleGenAIInteractionsChatGenerator(
|
|
67
|
+
model="gemini-3.8-flash",
|
|
68
|
+
generation_kwargs={"generation_config": {"thinking_level": "low", "thinking_summaries": "auto"}},
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
response = chat_generator.run(messages=[ChatMessage.from_user("Tell me about the future of AI")])
|
|
72
|
+
reply = response["replies"][0]
|
|
73
|
+
print(reply.text)
|
|
74
|
+
if reply.reasoning:
|
|
75
|
+
print("Reasoning:", reply.reasoning.reasoning_text)
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Usage example with tools and an Agent
|
|
79
|
+
|
|
80
|
+
```python
|
|
81
|
+
from haystack.components.agents import Agent
|
|
82
|
+
from haystack.dataclasses import ChatMessage
|
|
83
|
+
from haystack.tools import tool
|
|
84
|
+
from haystack_integrations.components.generators.google_genai import GoogleGenAIInteractionsChatGenerator
|
|
85
|
+
|
|
86
|
+
@tool
|
|
87
|
+
def weather(city: str) -> str:
|
|
88
|
+
\"\"\"Get the weather for a city.\"\"\"
|
|
89
|
+
return f"The weather in {city} is sunny and 25°C"
|
|
90
|
+
|
|
91
|
+
agent = Agent(chat_generator=GoogleGenAIInteractionsChatGenerator(model="gemini-3.8-flash"), tools=[weather])
|
|
92
|
+
result = agent.run(messages=[ChatMessage.from_user("What's the weather in Paris?")])
|
|
93
|
+
print(result["last_message"].text)
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### Usage example with structured output
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
from pydantic import BaseModel
|
|
100
|
+
from haystack.dataclasses import ChatMessage
|
|
101
|
+
from haystack_integrations.components.generators.google_genai import GoogleGenAIInteractionsChatGenerator
|
|
102
|
+
|
|
103
|
+
class City(BaseModel):
|
|
104
|
+
name: str
|
|
105
|
+
country: str
|
|
106
|
+
population: int
|
|
107
|
+
|
|
108
|
+
chat_generator = GoogleGenAIInteractionsChatGenerator(generation_kwargs={"response_format": City})
|
|
109
|
+
response = chat_generator.run(messages=[ChatMessage.from_user("Tell me about Paris")])
|
|
110
|
+
print(response["replies"][0].text) # JSON output matching the City schema
|
|
111
|
+
```
|
|
112
|
+
"""
|
|
113
|
+
|
|
114
|
+
SUPPORTED_MODELS: ClassVar[list[str]] = [
|
|
115
|
+
"gemini-4-argon",
|
|
116
|
+
"gemini-3.8-flash",
|
|
117
|
+
"gemini-3.7-flash",
|
|
118
|
+
"gemini-3.6-flash",
|
|
119
|
+
"gemini-3.5-flash",
|
|
120
|
+
"gemini-3.5-flash-lite",
|
|
121
|
+
"gemini-3.1-pro-preview",
|
|
122
|
+
"gemini-3.1-flash-lite",
|
|
123
|
+
"gemini-3-flash-preview",
|
|
124
|
+
"gemini-2.5-pro",
|
|
125
|
+
"gemini-2.5-flash",
|
|
126
|
+
"gemini-2.5-flash-lite",
|
|
127
|
+
]
|
|
128
|
+
"""A non-exhaustive list of chat models supported by this component.
|
|
129
|
+
|
|
130
|
+
See https://aistudio.google.com/docs/interactions-overview#supported-models-agents for the models available
|
|
131
|
+
through the Interactions API.
|
|
132
|
+
"""
|
|
133
|
+
|
|
134
|
+
def __init__(
|
|
135
|
+
self,
|
|
136
|
+
*,
|
|
137
|
+
api_key: Secret = Secret.from_env_var(["GOOGLE_API_KEY", "GEMINI_API_KEY"], strict=False),
|
|
138
|
+
api: Literal["gemini", "vertex"] = "gemini",
|
|
139
|
+
vertex_ai_project: str | None = None,
|
|
140
|
+
vertex_ai_location: str | None = None,
|
|
141
|
+
model: str = "gemini-3.8-flash",
|
|
142
|
+
generation_kwargs: dict[str, Any] | None = None,
|
|
143
|
+
streaming_callback: StreamingCallbackT | None = None,
|
|
144
|
+
tools: ToolsType | None = None,
|
|
145
|
+
timeout: float | None = None,
|
|
146
|
+
max_retries: int | None = None,
|
|
147
|
+
) -> None:
|
|
148
|
+
"""
|
|
149
|
+
Initialize a GoogleGenAIInteractionsChatGenerator instance.
|
|
150
|
+
|
|
151
|
+
:param api_key: Google API key, defaults to the `GOOGLE_API_KEY` and `GEMINI_API_KEY` environment variables.
|
|
152
|
+
Not needed if using Vertex AI with Application Default Credentials.
|
|
153
|
+
Go to [Google AI Studio](https://aistudio.google.com/app/api-keys) for a Gemini API key.
|
|
154
|
+
:param api: Which API to use. Either "gemini" for the Gemini Developer API or "vertex" for Vertex AI.
|
|
155
|
+
:param vertex_ai_project: Google Cloud project ID for Vertex AI. Required when using Vertex AI with
|
|
156
|
+
Application Default Credentials.
|
|
157
|
+
:param vertex_ai_location: Google Cloud location for Vertex AI (e.g., "us-central1", "europe-west1").
|
|
158
|
+
Required when using Vertex AI with Application Default Credentials.
|
|
159
|
+
:param model: Name of the model to use.
|
|
160
|
+
:param generation_kwargs: Keyword arguments passed as-is to `interactions.create`, for example
|
|
161
|
+
`generation_config` (`temperature`, `max_output_tokens`, `thinking_level`, `thinking_summaries`,
|
|
162
|
+
`tool_choice`, ...), `safety_settings`, `store`, or `service_tier`.
|
|
163
|
+
See the [Interactions API Documentation](https://ai.google.dev/api/interactions-api) for all options.
|
|
164
|
+
Set `generation_config={"thinking_summaries": "auto"}` to receive reasoning summaries.
|
|
165
|
+
`response_format` also accepts a Pydantic model class or a JSON schema dict for structured output.
|
|
166
|
+
`tools` declared here in the Interactions format, such as
|
|
167
|
+
[built-in Google tools](https://aistudio.google.com/docs/tools), are sent along with `tools`. Combining them
|
|
168
|
+
with `tools` requires Gemini 3 or newer. The steps built-in tools produce aren't converted into reply
|
|
169
|
+
content; they're kept in the reply's `meta["raw_steps_for_builtin_tools"]` and sent back on later turns.
|
|
170
|
+
:param streaming_callback: A callback function that is called when a new chunk is received from the stream.
|
|
171
|
+
:param tools: A list of Tool and/or Toolset objects, or a single Toolset for which the model can prepare calls.
|
|
172
|
+
Each tool should have a unique name.
|
|
173
|
+
:param timeout: Timeout for Google GenAI client calls. If not set, the Google GenAI client default is used.
|
|
174
|
+
:param max_retries: Maximum number of retries for failed requests. If not set, the Google GenAI client
|
|
175
|
+
default is used.
|
|
176
|
+
"""
|
|
177
|
+
_check_duplicate_tool_names(flatten_tools_or_toolsets(tools))
|
|
178
|
+
|
|
179
|
+
self._api_key = api_key
|
|
180
|
+
self._api = api
|
|
181
|
+
self._vertex_ai_project = vertex_ai_project
|
|
182
|
+
self._vertex_ai_location = vertex_ai_location
|
|
183
|
+
self._model = model
|
|
184
|
+
self._generation_kwargs = generation_kwargs or {}
|
|
185
|
+
self._streaming_callback = streaming_callback
|
|
186
|
+
self._tools = tools
|
|
187
|
+
self._timeout = timeout
|
|
188
|
+
self._max_retries = max_retries
|
|
189
|
+
self._client: Client | None = None
|
|
190
|
+
self._async_client: AsyncClient | None = None
|
|
191
|
+
|
|
192
|
+
def warm_up(self) -> None:
|
|
193
|
+
"""Create the synchronous Google Gen AI client."""
|
|
194
|
+
if self._client is None:
|
|
195
|
+
self._client = _get_client(
|
|
196
|
+
api_key=self._api_key,
|
|
197
|
+
api=self._api,
|
|
198
|
+
vertex_ai_project=self._vertex_ai_project,
|
|
199
|
+
vertex_ai_location=self._vertex_ai_location,
|
|
200
|
+
timeout=self._timeout,
|
|
201
|
+
max_retries=self._max_retries,
|
|
202
|
+
)
|
|
203
|
+
|
|
204
|
+
async def warm_up_async(self) -> None:
|
|
205
|
+
"""Create the asynchronous Google Gen AI client."""
|
|
206
|
+
if self._async_client is None:
|
|
207
|
+
self._async_client = _get_client(
|
|
208
|
+
api_key=self._api_key,
|
|
209
|
+
api=self._api,
|
|
210
|
+
vertex_ai_project=self._vertex_ai_project,
|
|
211
|
+
vertex_ai_location=self._vertex_ai_location,
|
|
212
|
+
timeout=self._timeout,
|
|
213
|
+
max_retries=self._max_retries,
|
|
214
|
+
async_client=True,
|
|
215
|
+
)
|
|
216
|
+
|
|
217
|
+
def close(self) -> None:
|
|
218
|
+
"""Close the synchronous Google Gen AI client."""
|
|
219
|
+
if self._client is not None:
|
|
220
|
+
self._client.close()
|
|
221
|
+
self._client = None
|
|
222
|
+
|
|
223
|
+
async def close_async(self) -> None:
|
|
224
|
+
"""Close the asynchronous Google Gen AI client."""
|
|
225
|
+
if self._async_client is not None:
|
|
226
|
+
await self._async_client.aclose()
|
|
227
|
+
self._async_client = None
|
|
228
|
+
|
|
229
|
+
def to_dict(self) -> dict[str, Any]:
|
|
230
|
+
"""
|
|
231
|
+
Serializes the component to a dictionary.
|
|
232
|
+
|
|
233
|
+
:returns: Dictionary with serialized data.
|
|
234
|
+
"""
|
|
235
|
+
callback_name = serialize_callable(self._streaming_callback) if self._streaming_callback else None
|
|
236
|
+
serialized_tools = serialize_tools_or_toolset(self._tools) if self._tools else None
|
|
237
|
+
|
|
238
|
+
generation_kwargs = self._generation_kwargs.copy()
|
|
239
|
+
response_format = generation_kwargs.get("response_format")
|
|
240
|
+
if response_format and isinstance(response_format, type) and issubclass(response_format, BaseModel):
|
|
241
|
+
generation_kwargs["response_format"] = response_format.model_json_schema()
|
|
242
|
+
|
|
243
|
+
return default_to_dict(
|
|
244
|
+
self,
|
|
245
|
+
api_key=self._api_key,
|
|
246
|
+
api=self._api,
|
|
247
|
+
vertex_ai_project=self._vertex_ai_project,
|
|
248
|
+
vertex_ai_location=self._vertex_ai_location,
|
|
249
|
+
model=self._model,
|
|
250
|
+
generation_kwargs=generation_kwargs,
|
|
251
|
+
streaming_callback=callback_name,
|
|
252
|
+
tools=serialized_tools,
|
|
253
|
+
timeout=self._timeout,
|
|
254
|
+
max_retries=self._max_retries,
|
|
255
|
+
)
|
|
256
|
+
|
|
257
|
+
@classmethod
|
|
258
|
+
def from_dict(cls, data: dict[str, Any]) -> "GoogleGenAIInteractionsChatGenerator":
|
|
259
|
+
"""
|
|
260
|
+
Deserializes the component from a dictionary.
|
|
261
|
+
|
|
262
|
+
:param data: Dictionary to deserialize from.
|
|
263
|
+
:returns: Deserialized component.
|
|
264
|
+
"""
|
|
265
|
+
deserialize_tools_or_toolset_inplace(data["init_parameters"], key="tools")
|
|
266
|
+
init_params = data.get("init_parameters", {})
|
|
267
|
+
if init_params.get("streaming_callback") is not None:
|
|
268
|
+
init_params["streaming_callback"] = deserialize_callable(init_params["streaming_callback"])
|
|
269
|
+
return default_from_dict(cls, data)
|
|
270
|
+
|
|
271
|
+
def _prepare_request(
|
|
272
|
+
self,
|
|
273
|
+
messages: list[ChatMessage] | str,
|
|
274
|
+
generation_kwargs: dict[str, Any] | None,
|
|
275
|
+
tools: ToolsType | None,
|
|
276
|
+
) -> dict[str, Any]:
|
|
277
|
+
"""
|
|
278
|
+
Build the keyword arguments for `interactions.create`.
|
|
279
|
+
|
|
280
|
+
:param messages: The input messages.
|
|
281
|
+
:param generation_kwargs: Runtime generation kwargs, merged per key over the init ones.
|
|
282
|
+
:param tools: Runtime tools, replacing the init ones if provided.
|
|
283
|
+
:returns: The request keyword arguments, without `stream`.
|
|
284
|
+
"""
|
|
285
|
+
runtime_kwargs = generation_kwargs or {}
|
|
286
|
+
request = {**self._generation_kwargs, **runtime_kwargs}
|
|
287
|
+
init_config = self._generation_kwargs.get("generation_config")
|
|
288
|
+
runtime_config = runtime_kwargs.get("generation_config")
|
|
289
|
+
if isinstance(init_config, dict) and isinstance(runtime_config, dict):
|
|
290
|
+
request["generation_config"] = {**init_config, **runtime_config}
|
|
291
|
+
request = _process_response_format(request)
|
|
292
|
+
|
|
293
|
+
tools = tools if tools is not None else self._tools
|
|
294
|
+
_check_duplicate_tool_names(flatten_tools_or_toolsets(tools))
|
|
295
|
+
if tools:
|
|
296
|
+
request["tools"] = [*request.get("tools", []), *_convert_tools_to_interactions_format(tools)]
|
|
297
|
+
|
|
298
|
+
system_instruction, input_steps = _convert_messages_to_interaction_input(_normalize_messages(messages))
|
|
299
|
+
if system_instruction:
|
|
300
|
+
request["system_instruction"] = system_instruction
|
|
301
|
+
|
|
302
|
+
request["model"] = self._model
|
|
303
|
+
request["input"] = input_steps
|
|
304
|
+
return request
|
|
305
|
+
|
|
306
|
+
@component.output_types(replies=list[ChatMessage])
|
|
307
|
+
def run(
|
|
308
|
+
self,
|
|
309
|
+
messages: list[ChatMessage] | str,
|
|
310
|
+
generation_kwargs: dict[str, Any] | None = None,
|
|
311
|
+
streaming_callback: StreamingCallbackT | None = None,
|
|
312
|
+
tools: ToolsType | None = None,
|
|
313
|
+
) -> dict[str, list[ChatMessage]]:
|
|
314
|
+
"""
|
|
315
|
+
Run the chat generator on the given messages.
|
|
316
|
+
|
|
317
|
+
:param messages: A list of ChatMessage instances representing the input messages.
|
|
318
|
+
If a string is provided, it is converted to a list containing a ChatMessage with user role.
|
|
319
|
+
:param generation_kwargs: Keyword arguments for `interactions.create`. They are merged per key with the
|
|
320
|
+
`generation_kwargs` passed during initialization, and `generation_config` is merged per key as well.
|
|
321
|
+
Keys provided here take precedence.
|
|
322
|
+
:param streaming_callback: A callback function that is called when a new chunk is received from the stream.
|
|
323
|
+
If provided, it overrides the callback set during initialization.
|
|
324
|
+
:param tools: A list of Tool and/or Toolset objects, or a single Toolset for which the model can prepare calls.
|
|
325
|
+
If provided, it overrides the tools set during initialization.
|
|
326
|
+
:returns: A dictionary with the following keys:
|
|
327
|
+
- `replies`: A list containing the generated ChatMessage.
|
|
328
|
+
:raises ValueError: If a message has an unsupported role or content, or tool names are duplicated.
|
|
329
|
+
:raises TypeError: If `response_format` has an unsupported type.
|
|
330
|
+
:raises RuntimeError: If the Interactions API call fails or the interaction ends with status `failed`.
|
|
331
|
+
"""
|
|
332
|
+
self.warm_up()
|
|
333
|
+
assert self._client is not None # noqa: S101
|
|
334
|
+
|
|
335
|
+
request = self._prepare_request(messages, generation_kwargs, tools)
|
|
336
|
+
streaming_callback = select_streaming_callback(
|
|
337
|
+
init_callback=self._streaming_callback, runtime_callback=streaming_callback, requires_async=False
|
|
338
|
+
)
|
|
339
|
+
|
|
340
|
+
try:
|
|
341
|
+
if streaming_callback is None:
|
|
342
|
+
interaction = self._client.interactions.create(**request)
|
|
343
|
+
return {"replies": [_convert_interaction_to_chat_message(interaction, self._model)]}
|
|
344
|
+
|
|
345
|
+
component_info = ComponentInfo.from_component(self)
|
|
346
|
+
chunks: list[StreamingChunk] = []
|
|
347
|
+
for event in self._client.interactions.create(**request, stream=True):
|
|
348
|
+
chunk = _convert_interaction_event_to_streaming_chunk(
|
|
349
|
+
event, component_info, self._model, previous_chunks=chunks
|
|
350
|
+
)
|
|
351
|
+
chunks.append(chunk)
|
|
352
|
+
streaming_callback(chunk)
|
|
353
|
+
return {"replies": [_aggregate_streaming_chunks(chunks, self._model)]}
|
|
354
|
+
except Exception as e:
|
|
355
|
+
msg = f"Error in Google GenAI Interactions chat generation: {e}"
|
|
356
|
+
raise RuntimeError(msg) from e
|
|
357
|
+
|
|
358
|
+
@component.output_types(replies=list[ChatMessage])
|
|
359
|
+
async def run_async(
|
|
360
|
+
self,
|
|
361
|
+
messages: list[ChatMessage] | str,
|
|
362
|
+
generation_kwargs: dict[str, Any] | None = None,
|
|
363
|
+
streaming_callback: StreamingCallbackT | None = None,
|
|
364
|
+
tools: ToolsType | None = None,
|
|
365
|
+
) -> dict[str, list[ChatMessage]]:
|
|
366
|
+
"""
|
|
367
|
+
Asynchronously run the chat generator on the given messages.
|
|
368
|
+
|
|
369
|
+
:param messages: A list of ChatMessage instances representing the input messages.
|
|
370
|
+
If a string is provided, it is converted to a list containing a ChatMessage with user role.
|
|
371
|
+
:param generation_kwargs: Keyword arguments for `interactions.create`. They are merged per key with the
|
|
372
|
+
`generation_kwargs` passed during initialization, and `generation_config` is merged per key as well.
|
|
373
|
+
Keys provided here take precedence.
|
|
374
|
+
:param streaming_callback: A callback function that is called when a new chunk is received from the stream.
|
|
375
|
+
If provided, it overrides the callback set during initialization. Both sync and async callbacks work.
|
|
376
|
+
:param tools: A list of Tool and/or Toolset objects, or a single Toolset for which the model can prepare calls.
|
|
377
|
+
If provided, it overrides the tools set during initialization.
|
|
378
|
+
:returns: A dictionary with the following keys:
|
|
379
|
+
- `replies`: A list containing the generated ChatMessage.
|
|
380
|
+
:raises ValueError: If a message has an unsupported role or content, or tool names are duplicated.
|
|
381
|
+
:raises TypeError: If `response_format` has an unsupported type.
|
|
382
|
+
:raises RuntimeError: If the Interactions API call fails or the interaction ends with status `failed`.
|
|
383
|
+
"""
|
|
384
|
+
await self.warm_up_async()
|
|
385
|
+
assert self._async_client is not None # noqa: S101
|
|
386
|
+
|
|
387
|
+
request = self._prepare_request(messages, generation_kwargs, tools)
|
|
388
|
+
streaming_callback = select_streaming_callback(
|
|
389
|
+
init_callback=self._streaming_callback, runtime_callback=streaming_callback, requires_async=True
|
|
390
|
+
)
|
|
391
|
+
|
|
392
|
+
try:
|
|
393
|
+
if streaming_callback is None:
|
|
394
|
+
interaction = await self._async_client.interactions.create(**request)
|
|
395
|
+
return {"replies": [_convert_interaction_to_chat_message(interaction, self._model)]}
|
|
396
|
+
|
|
397
|
+
component_info = ComponentInfo.from_component(self)
|
|
398
|
+
chunks: list[StreamingChunk] = []
|
|
399
|
+
stream = await self._async_client.interactions.create(**request, stream=True)
|
|
400
|
+
async for event in stream:
|
|
401
|
+
chunk = _convert_interaction_event_to_streaming_chunk(
|
|
402
|
+
event, component_info, self._model, previous_chunks=chunks
|
|
403
|
+
)
|
|
404
|
+
chunks.append(chunk)
|
|
405
|
+
# sync callbacks are allowed in async contexts with Haystack >= 3.0, so only await async ones
|
|
406
|
+
callback_result = streaming_callback(chunk)
|
|
407
|
+
if inspect.isawaitable(callback_result):
|
|
408
|
+
await callback_result
|
|
409
|
+
return {"replies": [_aggregate_streaming_chunks(chunks, self._model)]}
|
|
410
|
+
except Exception as e:
|
|
411
|
+
msg = f"Error in async Google GenAI Interactions chat generation: {e}"
|
|
412
|
+
raise RuntimeError(msg) from e
|