Google Vertex AI
Google Vertex 是一项服务,它公开了 Google Cloud 中提供的所有基础模型,例如 gemini-1.5-pro
、gemini-1.5-flash
等。
这将帮助您开始使用 LangChain 的 VertexAI 补全模型 (LLM)。 有关 VertexAI
功能和配置选项的详细文档,请参阅API 参考。
概述
集成详细信息
类 | 包 | 本地 | 可序列化 | PY 支持 | 包下载 | 包最新 |
---|---|---|---|---|---|---|
VertexAI | @langchain/google-vertexai | ❌ | ✅ | ✅ | ![]() | ![]() |
设置
LangChain.js 支持两种不同的身份验证方法,具体取决于您是在 Node.js 环境还是 Web 环境中运行。
要访问 VertexAI 模型,您需要创建一个 Google Cloud Platform (GCP) 帐户,获取 API 密钥,并安装 @langchain/google-vertexai
集成包。
凭据
Node.js
您应该确保为相关项目启用了 Vertex AI API,并且您已使用以下方法之一通过 Google Cloud 身份验证
- 您已登录到允许访问该项目的帐户(使用
gcloud auth application-default login
)。 - 您正在使用允许访问该项目的服务帐户的计算机上运行。
- 您已下载允许访问该项目的服务帐户的凭据,并将
GOOGLE_APPLICATION_CREDENTIALS
环境变量设置为此文件的路径。或者 - 您将
GOOGLE_API_KEY
环境变量设置为项目的 API 密钥。
Web
要在 Web 环境(如 Edge 函数)中调用 Vertex AI 模型,您需要安装 @langchain/google-vertexai-web
包。
然后,您需要将您的服务帐户凭据直接添加为 GOOGLE_VERTEX_AI_WEB_CREDENTIALS
环境变量
GOOGLE_VERTEX_AI_WEB_CREDENTIALS={"type":"service_account","project_id":"YOUR_PROJECT-12345",...}
您也可以像这样直接在代码中传递您的凭据
import { VertexAI } from "@langchain/google-vertexai";
// Or uncomment this line if you're using the web version:
// import { VertexAI } from "@langchain/google-vertexai-web";
const model = new VertexAI({
authOptions: {
credentials: {"type":"service_account","project_id":"YOUR_PROJECT-12345",...},
},
});
如果您想获得模型调用的自动跟踪,您还可以通过取消注释下方内容来设置您的 LangSmith API 密钥
# export LANGSMITH_TRACING="true"
# export LANGSMITH_API_KEY="your-api-key"
安装
LangChain VertexAI 集成位于 @langchain/google-vertexai
包中
有关安装集成包的一般说明,请参阅此部分。
- npm
- yarn
- pnpm
npm i @langchain/google-vertexai @langchain/core
yarn add @langchain/google-vertexai @langchain/core
pnpm add @langchain/google-vertexai @langchain/core
或对于 Web 环境
- npm
- yarn
- pnpm
npm i @langchain/google-vertexai-web @langchain/core
yarn add @langchain/google-vertexai-web @langchain/core
pnpm add @langchain/google-vertexai-web @langchain/core
实例化
现在我们可以实例化我们的模型对象并生成聊天补全
import { VertexAI } from "@langchain/google-vertexai-web";
const llm = new VertexAI({
model: "gemini-pro",
temperature: 0,
maxRetries: 2,
// other params...
});
调用
const inputText = "VertexAI is an AI company that ";
const completion = await llm.invoke(inputText);
completion;
offers a wide range of cloud computing services and artificial intelligence solutions to businesses and developers worldwide.
链接
我们可以像这样使用提示模板链接我们的补全模型
import { PromptTemplate } from "@langchain/core/prompts";
const prompt = PromptTemplate.fromTemplate(
"How to say {input} in {output_language}:\n"
);
const chain = prompt.pipe(llm);
await chain.invoke({
output_language: "German",
input: "I love programming.",
});
"Ich liebe Programmieren."
Pronunciation guide:
Ich: [ɪç] (similar to "ikh" with a soft "ch" sound)
liebe: [ˈliːbə] (LEE-buh)
Programmieren: [pʁoɡʁaˈmiːʁən] (pro-gra-MEE-ren)
You could also say:
"Ich liebe es zu programmieren."
Which translates more literally to "I love to program." This version is a bit more formal or precise.
Pronunciation:
es: [ɛs] (like the letter "S")
zu: [tsuː] (tsoo)
Both versions are correct and commonly used.
API 参考
有关所有 VertexAI 功能和配置的详细文档,请访问 API 参考:https://api.js.langchain.com/classes/langchain_google_vertexai.VertexAI.html