ChatVertexAI
本页面概述了如何开始使用 VertexAI 聊天模型。如需了解所有 ChatVertexAI 功能和配置的详细文档,请访问 API 参考。
ChatVertexAI 暴露了 Google Cloud 中提供的所有基础模型,例如 gemini-1.5-pro、gemini-1.5-flash 等 。如需可用模型列表的完整且最新更新,请访问 VertexAI 文档。
Google Cloud VertexAI 集成独立于 Google PaLM 集成。Google 选择通过 GCP 提供 PaLM 的企业版,并且此版本支持通过此处提供的模型。
概览
集成详情
| 类 | 包 | 本地 | 可序列化 | JS 支持 | 包下载量 | 包最新版本 |
|---|---|---|---|---|---|---|
| ChatVertexAI | langchain-google-vertexai | ❌ | beta | ✅ |
模型功能
| 工具调用 | 结构化输出 | JSON 模式 | 图像输入 | 音频输入 | 视频输入 | Token 级流式输出 | 原生异步 | Token 使用量跟踪 | Logprobs |
|---|---|---|---|---|---|---|---|---|---|
| ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ |
设置
要访问 VertexAI 模型,您需要创建一个 Google Cloud Platform 账户,设置凭据,并安装 langchain-google-vertexai 集成包。
凭据
要使用该集成,您必须:
- 为您的环境配置凭据(gcloud、工作负载身份等)
- 将指向服务账户 JSON 文件的路径存储在 GOOGLE_APPLICATION_CREDENTIALS 环境变量中
此代码库使用 google.auth 库,该库首先查找上述应用程序凭据变量,然后查找系统级别的身份验证。
更多信息,请参阅:
- https://cloud.google.com/docs/authentication/application-default-credentials#GAC
- https://googleapis.dev/python/google-auth/latest/reference/google.auth.html#module-google.auth
如需启用模型调用的自动化跟踪,请设置您的 LangSmith API 密钥:
# os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
# os.environ["LANGSMITH_TRACING"] = "true"
安装
LangChain VertexAI 集成位于 langchain-google-vertexai 包中:
%pip install -qU langchain-google-vertexai
Note: you may need to restart the kernel to use updated packages.
实例化
现 在我们可以实例化我们的模型对象并生成聊天补全:
from langchain_google_vertexai import ChatVertexAI
llm = ChatVertexAI(
model="gemini-1.5-flash-001",
temperature=0,
max_tokens=None,
max_retries=6,
stop=None,
# other params...
)
调用
messages = [
(
"system",
"You are a helpful assistant that translates English to French. Translate the user sentence.",
),
("human", "I love programming."),
]
ai_msg = llm.invoke(messages)
ai_msg
AIMessage(content="J'adore programmer. \n", response_metadata={'is_blocked': False, 'safety_ratings': [{'category': 'HARM_CATEGORY_HATE_SPEECH', 'probability_label': 'NEGLIGIBLE', 'blocked': False}, {'category': 'HARM_CATEGORY_DANGEROUS_CONTENT', 'probability_label': 'NEGLIGIBLE', 'blocked': False}, {'category': 'HARM_CATEGORY_HARASSMENT', 'probability_label': 'NEGLIGIBLE', 'blocked': False}, {'category': 'HARM_CATEGORY_SEXUALLY_EXPLICIT', 'probability_label': 'NEGLIGIBLE', 'blocked': False}], 'usage_metadata': {'prompt_token_count': 20, 'candidates_token_count': 7, 'total_token_count': 27}}, id='run-7032733c-d05c-4f0c-a17a-6c575fdd1ae0-0', usage_metadata={'input_tokens': 20, 'output_tokens': 7, 'total_tokens': 27})
print(ai_msg.content)
J'adore programmer.
内置工具
Gemini 支持一系列在服务器端执行的工具。
Google 搜索
langchain-google-vertexai>=2.0.11Gemini 可以执行 Google 搜索,并使用搜索结果来巩固其响应:
from langchain_google_vertexai import ChatVertexAI
llm = ChatVertexAI(model="gemini-2.0-flash-001").bind_tools([{"google_search": {}}])
response = llm.invoke("What is today's news?")
代码执行
langchain-google-vertexai>=2.0.25Gemini 可以生成和执行 Python 代码:
from langchain_google_vertexai import ChatVertexAI
llm = ChatVertexAI(model="gemini-2.0-flash-001").bind_tools([{"code_execution": {}}])
response = llm.invoke("What is 3^3?")
链式调用
我们可以像这样将模型与提示模板进行链式调用:
from langchain_core.prompts import ChatPromptTemplate
prompt = ChatPromptTemplate.from_messages(
[
(
"system",
"You are a helpful assistant that translates {input_language} to {output_language}.",
),
("human", "{input}"),
]
)
chain = prompt | llm
chain.invoke(
{
"input_language": "English",
"output_language": "German",
"input": "I love programming.",
}
)
AIMessage(content='Ich liebe Programmieren. \n', response_metadata={'is_blocked': False, 'safety_ratings': [{'category': 'HARM_CATEGORY_HATE_SPEECH', 'probability_label': 'NEGLIGIBLE', 'blocked': False}, {'category': 'HARM_CATEGORY_DANGEROUS_CONTENT', 'probability_label': 'NEGLIGIBLE', 'blocked': False}, {'category': 'HARM_CATEGORY_HARASSMENT', 'probability_label': 'NEGLIGIBLE', 'blocked': False}, {'category': 'HARM_CATEGORY_SEXUALLY_EXPLICIT', 'probability_label': 'NEGLIGIBLE', 'blocked': False}], 'usage_metadata': {'prompt_token_count': 15, 'candidates_token_count': 8, 'total_token_count': 23}}, id='run-c71955fd-8dc1-422b-88a7-853accf4811b-0', usage_metadata={'input_tokens': 15, 'output_tokens': 8, 'total_tokens': 23})
API 参考
如需了解 ChatVertexAI 所有功能和配置的详细文档,例如如何发送多模态输入和配置安全设置,请前往 API 参考:https://python.langchain.com/api_reference/google_vertexai/chat_models/langchain_google_vertexai.chat_models.ChatVertexAI.html
Related
- Chat model conceptual guide
- Chat model how-to guides