c56bef871b
CodeQL / Analyze (python) (push) Has been cancelled
Update Platform Components Table / update (push) Has been cancelled
Docker image release / Build base image (push) Has been cancelled
Sync docs with Docusaurus / sync (push) Has been cancelled
Tests / Check if changed (push) Has been cancelled
Tests / format (push) Has been cancelled
Tests / check-imports (push) Has been cancelled
Tests / Unit / macos-latest (push) Has been cancelled
Tests / Unit / ubuntu-latest (push) Has been cancelled
Tests / Unit / windows-latest (push) Has been cancelled
Tests / mypy (push) Has been cancelled
Tests / Integration / ubuntu-latest (push) Has been cancelled
Tests / Integration / macos-latest (push) Has been cancelled
Tests / Integration / windows-latest (push) Has been cancelled
Tests / notify-slack-on-failure (push) Has been cancelled
Tests / Mark tests as completed (push) Has been cancelled
146 lines
5.7 KiB
Plaintext
146 lines
5.7 KiB
Plaintext
---
|
||
title: "VertexAIGeminiGenerator"
|
||
id: vertexaigeminigenerator
|
||
slug: "/vertexaigeminigenerator"
|
||
description: "`VertexAIGeminiGenerator` enables text generation using Google Gemini models."
|
||
---
|
||
|
||
# VertexAIGeminiGenerator
|
||
|
||
`VertexAIGeminiGenerator` enables text generation using Google Gemini models.
|
||
|
||
:::warning
|
||
Deprecation Notice
|
||
|
||
This integration uses the deprecated google-generativeai SDK, which will lose support after August 2025.
|
||
|
||
We recommend switching to the new [GoogleGenAIChatGenerator](googlegenaichatgenerator.mdx) integration instead.
|
||
:::
|
||
|
||
| | |
|
||
| :------------------------------------- | :---------------------------------------------------------------------------------------------- |
|
||
| **Most common position in a pipeline** | After a [`PromptBuilder`](../builders/promptbuilder.mdx) |
|
||
| **Mandatory run variables** | “parts”: A variadic list containing a mix of images, audio, video, and text to prompt Gemini |
|
||
| **Output variables** | “replies”: A list of strings or dictionaries with all the replies generated by the model |
|
||
| **API reference** | [Google Vertex](/reference/integrations-google-vertex) |
|
||
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/google_vertex |
|
||
|
||
`VertexAIGeminiGenerator` supports `gemini-1.5-pro` and `gemini-1.5-flash`/ `gemini-2.0-flash` models. Note that [Google recommends upgrading](https://cloud.google.com/vertex-ai/generative-ai/docs/learn/model-versions) from `gemini-1.5-pro` to `gemini-2.0-flash`.
|
||
|
||
For details on available models, see https://cloud.google.com/vertex-ai/generative-ai/docs/learn/models.
|
||
|
||
:::note
|
||
To explore the full capabilities of Gemini check out this [article](https://haystack.deepset.ai/blog/gemini-models-with-google-vertex-for-haystack) and the related [Colab notebook](https://colab.research.google.com/drive/10SdXvH2ATSzqzA3OOmTM8KzD5ZdH_Q6Z?usp=sharing).
|
||
|
||
:::
|
||
|
||
### Parameters Overview
|
||
|
||
`VertexAIGeminiGenerator` uses Google Cloud Application Default Credentials (ADCs) for authentication. For more information on how to set up ADCs, see the [official documentation](https://cloud.google.com/docs/authentication/provide-credentials-adc).
|
||
|
||
Keep in mind that it’s essential to use an account that has access to a project authorized to use Google Vertex AI endpoints.
|
||
|
||
You can find your project ID in the [GCP resource manager](https://console.cloud.google.com/cloud-resource-manager) or locally by running `gcloud projects list` in your terminal. For more info on the gcloud CLI, see its [official documentation](https://cloud.google.com/cli).
|
||
|
||
### Streaming
|
||
|
||
This Generator supports [streaming](guides-to-generators/choosing-the-right-generator.mdx#streaming-support) the tokens from the LLM directly in output. To do so, pass a function to the `streaming_callback` init parameter.
|
||
|
||
## Usage
|
||
|
||
You should install `google-vertex-haystack` package to use the `VertexAIGeminiGenerator`:
|
||
|
||
```shell
|
||
pip install google-vertex-haystack
|
||
```
|
||
|
||
### On its own
|
||
|
||
Basic usage:
|
||
|
||
```python
|
||
from haystack_integrations.components.generators.google_vertex import (
|
||
VertexAIGeminiGenerator,
|
||
)
|
||
|
||
gemini = VertexAIGeminiGenerator()
|
||
result = gemini.run(parts=["What is the most interesting thing you know?"])
|
||
for answer in result["replies"]:
|
||
print(answer)
|
||
```
|
||
|
||
Advanced usage, multi-modal prompting:
|
||
|
||
```python
|
||
import requests
|
||
from haystack.dataclasses.byte_stream import ByteStream
|
||
from haystack_integrations.components.generators.google_vertex import (
|
||
VertexAIGeminiGenerator,
|
||
)
|
||
|
||
URLS = [
|
||
"https://raw.githubusercontent.com/silvanocerza/robots/main/robot1.jpg",
|
||
"https://raw.githubusercontent.com/silvanocerza/robots/main/robot2.jpg",
|
||
"https://raw.githubusercontent.com/silvanocerza/robots/main/robot3.jpg",
|
||
"https://raw.githubusercontent.com/silvanocerza/robots/main/robot4.jpg",
|
||
]
|
||
images = [
|
||
ByteStream(data=requests.get(url).content, mime_type="image/jpeg") for url in URLS
|
||
]
|
||
|
||
gemini = VertexAIGeminiGenerator()
|
||
result = gemini.run(parts=["What can you tell me about this robots?", *images])
|
||
for answer in result["replies"]:
|
||
print(answer)
|
||
```
|
||
|
||
### In a pipeline
|
||
|
||
In a RAG pipeline:
|
||
|
||
```python
|
||
from haystack.components.retrievers.in_memory import InMemoryBM25Retriever
|
||
from haystack.components.builders import PromptBuilder
|
||
from haystack import Pipeline
|
||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||
from haystack_integrations.components.generators.google_vertex import (
|
||
VertexAIGeminiGenerator,
|
||
)
|
||
|
||
docstore = InMemoryDocumentStore()
|
||
docstore.write_documents(
|
||
[
|
||
Document(content="Rome is the capital of Italy"),
|
||
Document(content="Paris is the capital of France"),
|
||
],
|
||
)
|
||
|
||
query = "What is the capital of France?"
|
||
|
||
template = """
|
||
Given the following information, answer the question.
|
||
|
||
Context:
|
||
{% for document in documents %}
|
||
{{ document.content }}
|
||
{% endfor %}
|
||
|
||
Question: {{ query }}?
|
||
"""
|
||
pipe = Pipeline()
|
||
|
||
pipe.add_component("retriever", InMemoryBM25Retriever(document_store=docstore))
|
||
pipe.add_component("prompt_builder", PromptBuilder(template=template))
|
||
pipe.add_component("gemini", VertexAIGeminiGenerator())
|
||
pipe.connect("retriever", "prompt_builder.documents")
|
||
pipe.connect("prompt_builder", "gemini")
|
||
|
||
res = pipe.run({"prompt_builder": {"query": query}, "retriever": {"query": query}})
|
||
|
||
print(res)
|
||
```
|
||
|
||
## Additional References
|
||
|
||
🧑🍳 Cookbook: [Function Calling and Multimodal QA with Gemini](https://haystack.deepset.ai/cookbook/vertexai-gemini-examples)
|