Google PaLM(旧版)
危险
Google PaLM API 已弃用,将在 0.3.0 中删除。请改用Google GenAI 或VertexAI 集成。
注意
此集成不支持 gemini-*
模型。请检查Google GenAI 或VertexAI。
可以通过首先安装所需的软件包来集成Google PaLM API
提示
- npm
- Yarn
- pnpm
npm install google-auth-library @google-ai/generativelanguage @langchain/community
yarn add google-auth-library @google-ai/generativelanguage @langchain/community
pnpm add google-auth-library @google-ai/generativelanguage @langchain/community
从Google MakerSuite 创建一个API 密钥。然后,您可以将密钥设置为 GOOGLE_PALM_API_KEY
环境变量,或者在实例化模型时将其作为 apiKey
参数传递。
import { GooglePaLM } from "@langchain/community/llms/googlepalm";
export const run = async () => {
const model = new GooglePaLM({
apiKey: "<YOUR API KEY>", // or set it in environment variable as `GOOGLE_PALM_API_KEY`
// other params
temperature: 1, // OPTIONAL
model: "models/text-bison-001", // OPTIONAL
maxOutputTokens: 1024, // OPTIONAL
topK: 40, // OPTIONAL
topP: 3, // OPTIONAL
safetySettings: [
// OPTIONAL
{
category: "HARM_CATEGORY_DANGEROUS",
threshold: "BLOCK_MEDIUM_AND_ABOVE",
},
],
stopSequences: ["stop"], // OPTIONAL
});
const res = await model.invoke(
"What would be a good company name for a company that makes colorful socks?"
);
console.log({ res });
};
API 参考
- GooglePaLM 来自
@langchain/community/llms/googlepalm
GooglePaLM
Langchain.js 支持两种不同的身份验证方法,具体取决于您是在 Node.js 环境中运行还是在 Web 环境中运行。
设置
Node.js
要在 Node 中调用 Vertex AI 模型,您需要安装Google 的官方身份验证客户端 作为对等依赖项。
您应确保 Vertex AI API 已为相关项目启用,并且您已使用以下方法之一对 Google Cloud 进行了身份验证
- 您已登录到允许使用该项目的帐户(使用
gcloud auth application-default login
)。 - 您正在使用允许使用该项目的 service account 的机器上运行。
- 您已下载允许使用该项目的 service account 的凭据,并将
GOOGLE_APPLICATION_CREDENTIALS
环境变量设置为该文件的路径。
- npm
- Yarn
- pnpm
npm install google-auth-library
yarn add google-auth-library
pnpm add google-auth-library
Web
要在 Web 环境(如 Edge 函数)中调用 Vertex AI 模型,您需要安装web-auth-library
软件包作为对等依赖项
- npm
- Yarn
- pnpm
npm install web-auth-library
yarn add web-auth-library
pnpm add web-auth-library
然后,您需要将您的 service account 凭据直接作为 GOOGLE_VERTEX_AI_WEB_CREDENTIALS
环境变量添加
GOOGLE_VERTEX_AI_WEB_CREDENTIALS={"type":"service_account","project_id":"YOUR_PROJECT-12345",...}
您也可以像这样在代码中直接传递您的凭据
提示
- npm
- Yarn
- pnpm
npm install @langchain/community
yarn add @langchain/community
pnpm add @langchain/community
import { GoogleVertexAI } from "@langchain/community/llms/googlevertexai";
const model = new GoogleVertexAI({
authOptions: {
credentials: {"type":"service_account","project_id":"YOUR_PROJECT-12345",...},
},
});
用法
有几种模型可用,可以通过构造函数中的 model
属性指定。这些包括
- text-bison(默认)
- text-bison-32k
- code-gecko
- code-bison
import { GoogleVertexAI } from "@langchain/community/llms/googlevertexai";
// Or, if using the web entrypoint:
// import { GoogleVertexAI } from "@langchain/community/llms/googlevertexai/web";
/*
* Before running this, you should make sure you have created a
* Google Cloud Project that is permitted to the Vertex AI API.
*
* You will also need permission to access this project / API.
* Typically, this is done in one of three ways:
* - You are logged into an account permitted to that project.
* - You are running this on a machine using a service account permitted to
* the project.
* - The `GOOGLE_APPLICATION_CREDENTIALS` environment variable is set to the
* path of a credentials file for a service account permitted to the project.
*/
const model = new GoogleVertexAI({
temperature: 0.7,
});
const res = await model.invoke(
"What would be a good company name for a company that makes colorful socks?"
);
console.log({ res });
API 参考
- GoogleVertexAI 来自
@langchain/community/llms/googlevertexai
Google 还为其“Codey”代码生成模型提供了单独的模型。
“code-gecko” 模型对于代码补全很有用
import { GoogleVertexAI } from "@langchain/community/llms/googlevertexai";
/*
* Before running this, you should make sure you have created a
* Google Cloud Project that is permitted to the Vertex AI API.
*
* You will also need permission to access this project / API.
* Typically, this is done in one of three ways:
* - You are logged into an account permitted to that project.
* - You are running this on a machine using a service account permitted to
* the project.
* - The `GOOGLE_APPLICATION_CREDENTIALS` environment variable is set to the
* path of a credentials file for a service account permitted to the project.
*/
const model = new GoogleVertexAI({
model: "code-gecko",
});
const res = await model.invoke("for (let co=0;");
console.log({ res });
API 参考
- GoogleVertexAI 来自
@langchain/community/llms/googlevertexai
而“code-bison” 模型在基于文本提示进行更大代码生成方面更出色
import { GoogleVertexAI } from "@langchain/community/llms/googlevertexai";
/*
* Before running this, you should make sure you have created a
* Google Cloud Project that is permitted to the Vertex AI API.
*
* You will also need permission to access this project / API.
* Typically, this is done in one of three ways:
* - You are logged into an account permitted to that project.
* - You are running this on a machine using a service account permitted to
* the project.
* - The `GOOGLE_APPLICATION_CREDENTIALS` environment variable is set to the
* path of a credentials file for a service account permitted to the project.
*/
const model = new GoogleVertexAI({
model: "code-bison",
maxOutputTokens: 2048,
});
const res = await model.invoke(
"A Javascript function that counts from 1 to 10."
);
console.log({ res });
API 参考
- GoogleVertexAI 来自
@langchain/community/llms/googlevertexai
流式传输
支持以多个块的形式进行流式传输,以获得更快的响应速度
import { GoogleVertexAI } from "@langchain/community/llms/googlevertexai";
const model = new GoogleVertexAI({
temperature: 0.7,
});
const stream = await model.stream(
"What would be a good company name for a company that makes colorful socks?"
);
for await (const chunk of stream) {
console.log("\n---------\nChunk:\n---------\n", chunk);
}
/*
---------
Chunk:
---------
1. Toe-tally Awesome Socks
2. The Sock Drawer
3. Happy Feet
4.
---------
Chunk:
---------
Sock It to Me
5. Crazy Color Socks
6. Wild and Wacky Socks
7. Fu
---------
Chunk:
---------
nky Feet
8. Mismatched Socks
9. Rainbow Socks
10. Sole Mates
---------
Chunk:
---------
*/
API 参考
- GoogleVertexAI 来自
@langchain/community/llms/googlevertexai