VertexAIGeminiChatGenerator
VertexAIGeminiChatGenerator 可使用 Google Gemini 模型进行聊天补全。
弃用通知
此集成使用了已弃用的 google-generativeai SDK,该 SDK 将于 2025 年 8 月后停止支持。
我们建议改用新的 GoogleGenAIChatGenerator 集成。
| pipeline 中的最常见位置 | 使用 ChatPromptBuilder 后 |
| 强制运行变量 | “messages”:一个 ChatMessage 对象列表,代表聊天记录 |
| 输出变量 | “replies”: 模型对输入聊天的备选回复列表 |
| API 参考 | Google Vertex |
| GitHub 链接 | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/google_vertex |
VertexAIGeminiGenerator 支持gemini-1.5-pro 和gemini-1.5-flash/ gemini-2.0-flash 模型。请注意,Google 建议将gemini-1.5-pro 升级到gemini-2.0-flash.
有关可用模型,请参阅 https://cloud.google.com/vertex-ai/generative-ai/docs/learn/models。
参数概述
VertexAIGeminiChatGenerator 使用 Google Cloud 应用程序默认凭据 (ADC) 进行身份验证。有关如何设置 ADC 的更多信息,请参阅 官方文档。
请记住,使用有权访问已授权使用 Google Vertex AI 端点的项目的帐户至关重要。
您可以在GCP 资源管理器中找到您的项目 ID,或者通过在终端中运行gcloud projects list 来在本地找到。有关 gcloud CLI 的更多信息,请参阅其官方文档。
流式传输
此 Generator 支持将 LLM 的 token直接流式传输到输出中。要做到这一点,请将一个函数传递给streaming_callback 初始化参数。
用法
您需要安装google-vertex-haystack 包以使用VertexAIGeminiChatGenerator:
pip install google-vertex-haystack
单独使用
基本用法
from haystack.dataclasses import ChatMessage
from haystack_integrations.components.generators.google_vertex import VertexAIGeminiChatGenerator
gemini_chat = VertexAIGeminiChatGenerator()
messages = [ChatMessage.from_user("Tell me the name of a movie")]
res = gemini_chat.run(messages)
print(res["replies"][0].text)
>>> The Shawshank Redemption
messages += [res["replies"][0], ChatMessage.from_user("Who's the main actor?")]
res = gemini_chat.run(messages)
print(res["replies"][0].text)
>>> Tim Robbins
与 Gemini Pro 聊天时,您还可以轻松使用函数调用。首先,在本地定义函数并将其转换为 Tool
from typing import Annotated
from haystack.tools import create_tool_from_function
# example function to get the current weather
def get_current_weather(
location: Annotated[str, "The city for which to get the weather, e.g. 'San Francisco'"] = "Munich",
unit: Annotated[str, "The unit for the temperature, e.g. 'celsius'"] = "celsius",
) -> str:
return f"The weather in {location} is sunny. The temperature is 20 {unit}."
tool = create_tool_from_function(get_current_weather)
创建新实例VertexAIGeminiChatGenerator 以设置工具和 ToolInvoker 来调用工具。
from haystack_integrations.components.generators.google_vertex import VertexAIGeminiChatGenerator
from haystack.components.tools import ToolInvoker
gemini_chat = VertexAIGeminiChatGenerator(model="gemini-2.0-flash-exp", tools=[tool])
tool_invoker = ToolInvoker(tools=[tool])
然后问我们的问题
from haystack.dataclasses import ChatMessage
messages = [ChatMessage.from_user("What is the temperature in celsius in Berlin?")]
res = gemini_chat.run(messages=messages)
print(res["replies"][0].tool_calls)
>>> [ToolCall(tool_name='get_current_weather',
>>> arguments={'unit': 'celsius', 'location': 'Berlin'}, id=None)]
tool_messages = tool_invoker.run(messages=replies)["tool_messages"]
messages = user_message + replies + tool_messages
messages += res["replies"][0] + [ChatMessage.from_function(content=weather, name="get_current_weather")]
final_replies = gemini_chat.run(messages=messages)["replies"]
print(final_replies[0].text)
>>> The temperature in Berlin is 20 degrees Celsius.
在 pipeline 中
from haystack.components.builders import ChatPromptBuilder
from haystack.dataclasses import ChatMessage
from haystack import Pipeline
from haystack_integrations.components.generators.google_vertex import VertexAIGeminiChatGenerator
# no parameter init, we don't use any runtime template variables
prompt_builder = ChatPromptBuilder()
gemini_chat = VertexAIGeminiChatGenerator()
pipe = Pipeline()
pipe.add_component("prompt_builder", prompt_builder)
pipe.add_component("gemini", gemini)
pipe.connect("prompt_builder.prompt", "gemini.messages")
location = "Rome"
messages = [ChatMessage.from_user("Tell me briefly about {{location}} history")]
res = pipe.run(data={"prompt_builder": {"template_variables":{"location": location}, "template": messages}})
print(res)
>>> - **753 B.C.:** Traditional date of the founding of Rome by Romulus and Remus.
>>> - **509 B.C.:** Establishment of the Roman Republic, replacing the Etruscan monarchy.
>>> - **492-264 B.C.:** Series of wars against neighboring tribes, resulting in the expansion of the Roman Republic's territory.
>>> - **264-146 B.C.:** Three Punic Wars against Carthage, resulting in the destruction of Carthage and the Roman Republic becoming the dominant power in the Mediterranean.
>>> - **133-73 B.C.:** Series of civil wars and slave revolts, leading to the rise of Julius Caesar.
>>> - **49 B.C.:** Julius Caesar crosses the Rubicon River, starting the Roman Civil War.
>>> - **44 B.C.:** Julius Caesar is assassinated, leading to the Second Triumvirate of Octavian, Mark Antony, and Lepidus.
>>> - **31 B.C.:** Battle of Actium, where Octavian defeats Mark Antony and Cleopatra, becoming the sole ruler of Rome.
>>> - **27 B.C.:** The Roman Republic is transformed into the Roman Empire, with Octavian becoming the first Roman emperor, known as Augustus.
>>> - **1st century A.D.:** The Roman Empire reaches its greatest extent, stretching from Britain to Egypt.
>>> - **3rd century A.D.:** The Roman Empire begins to decline, facing internal instability, invasions by Germanic tribes, and the rise of Christianity.
>>> - **476 A.D.:** The last Western Roman emperor, Romulus Augustulus, is overthrown by the Germanic leader Odoacer, marking the end of the Roman Empire in the West.
其他参考资料
🧑🍳 食谱:Gemini 的函数调用和多模态问答
更新于 3 个月前
