IBM watsonx.ai
这将帮助您开始使用 IBM watsonx.ai
聊天模型。有关所有 IBM watsonx.ai
功能和配置的详细文档,请前往 IBM
watsonx.ai。
概览
集成详情
| 类 | 包 | 本地 | 可序列化 | PY 支持 | 包下载量 | 最新包 |
|---|---|---|---|---|---|---|
ChatWatsonx | @langchain/community | ❌ | ✅ | ✅ | ![]() | ![]() |
模型功能
| 工具调用 | 结构化输出 | JSON 模式 | 图像输入 | 音频输入 | 视频输入 | 逐令牌流 | 令牌使用情况 | Logprobs |
|---|---|---|---|---|---|---|---|---|
| ✅ | ✅ | ✅ | ✅ | ❌ | ❌ | ✅ | ✅ | ❌ |
准备工作
要访问 IBM watsonx.ai 模型,您需要创建一个 IBM watsonx.ai 账户,获取 API
密钥,并安装 @langchain/community 集成包。
凭据
前往 IBM Cloud 注册 IBM watsonx.ai 并生成 API 密钥,或提供以下列出的任何其他认证方式。
IAM 认证
export WATSONX_AI_AUTH_TYPE=iam
export WATSONX_AI_APIKEY=<YOUR-APIKEY>
Bearer 令牌认证
export WATSONX_AI_AUTH_TYPE=bearertoken
export WATSONX_AI_BEARER_TOKEN=<YOUR-BEARER-TOKEN>
IBM watsonx.ai 软件认证
export WATSONX_AI_AUTH_TYPE=cp4d
export WATSONX_AI_USERNAME=<YOUR_USERNAME>
export WATSONX_AI_PASSWORD=<YOUR_PASSWORD>
export WATSONX_AI_URL=<URL>
一旦这些变量放置在您的环境变量中并且对象初始化完成,认证将自动进行。
也可以通过将这些值作为参数传递给新实例来完成认证。
IAM 认证
import { WatsonxLLM } from "@langchain/community/llms/ibm";
const props = {
version: "YYYY-MM-DD",
serviceUrl: "<SERVICE_URL>",
projectId: "<PROJECT_ID>",
watsonxAIAuthType: "iam",
watsonxAIApikey: "<YOUR-APIKEY>",
};
const instance = new WatsonxLLM(props);
Bearer 令牌认证
import { WatsonxLLM } from "@langchain/community/llms/ibm";
const props = {
version: "YYYY-MM-DD",
serviceUrl: "<SERVICE_URL>",
projectId: "<PROJECT_ID>",
watsonxAIAuthType: "bearertoken",
watsonxAIBearerToken: "<YOUR-BEARERTOKEN>",
};
const instance = new WatsonxLLM(props);
IBM watsonx.ai 软件认证
import { WatsonxLLM } from "@langchain/community/llms/ibm";
const props = {
version: "YYYY-MM-DD",
serviceUrl: "<SERVICE_URL>",
projectId: "<PROJECT_ID>",
watsonxAIAuthType: "cp4d",
watsonxAIUsername: "<YOUR-USERNAME>",
watsonxAIPassword: "<YOUR-PASSWORD>",
watsonxAIUrl: "<url>",
};
const instance = new WatsonxLLM(props);
如果您希望自动追踪模型调用,也可以取消以下 LangSmith API 密钥的注释:
# export LANGSMITH_TRACING="true"
# export LANGSMITH_API_KEY="your-api-key"
安装
LangChain IBM watsonx.ai 集成位于 @langchain/community 包中:
:::提示 请参阅安装集成包的一般说明部分。 :::
- npm
- yarn
- pnpm
npm i @langchain/community @langchain/core
yarn add @langchain/community @langchain/core
pnpm add @langchain/community @langchain/core
实例化
现在我们可以实例化我们的模型对象并生成聊天补全:
import { ChatWatsonx } from "@langchain/community/chat_models/ibm";
const props = {
maxTokens: 200,
temperature: 0.5,
};
const instance = new ChatWatsonx({
version: "YYYY-MM-DD",
serviceUrl: process.env.API_URL,
projectId: "<PROJECT_ID>",
// spaceId: "<SPACE_ID>",
// idOrName: "<DEPLOYMENT_ID>",
model: "<MODEL_ID>",
...props,
});
注意:
- 除非使用无需指定任何参数的轻量级引擎(参考 watsonx.ai
文档),否则必须提供
spaceId、projectId或idOrName(部署 ID)。 - 根据您已配置服务实例的区域,使用正确的 serviceUrl。
调用
const aiMsg = await instance.invoke([
{
role: "system",
content:
"You are a helpful assistant that translates English to French. Translate the user sentence.",
},
{
role: "user",
content: "I love programming.",
},
]);
console.log(aiMsg);
AIMessage {
"id": "chat-c5341b2062dc42f091e5ae2558e905e3",
"content": " J'adore la programmation.",
"additional_kwargs": {
"tool_calls": []
},
"response_metadata": {
"tokenUsage": {
"completion_tokens": 10,
"prompt_tokens": 28,
"total_tokens": 38
},
"finish_reason": "stop"
},
"tool_calls": [],
"invalid_tool_calls": [],
"usage_metadata": {
"input_tokens": 28,
"output_tokens": 10,
"total_tokens": 38
}
}
console.log(aiMsg.content);
J'adore la programmation.
链式调用
我们可以像这样将模型与提示模板进行链式调用:
import { ChatPromptTemplate } from "@langchain/core/prompts";
const prompt = ChatPromptTemplate.fromMessages([
[
"system",
"You are a helpful assistant that translates {input_language} to {output_language}.",
],
["human", "{input}"],
]);
const chain = prompt.pipe(instance);
await chain.invoke({
input_language: "English",
output_language: "German",
input: "I love programming.",
});
AIMessage {
"id": "chat-c5c2c08d3c984254acc48225c39c6a08",
"content": " Ich liebe Programmieren.",
"additional_kwargs": {
"tool_calls": []
},
"response_metadata": {
"tokenUsage": {
"completion_tokens": 8,
"prompt_tokens": 22,
"total_tokens": 30
},
"finish_reason": "stop"
},
"tool_calls": [],
"invalid_tool_calls": [],
"usage_metadata": {
"input_tokens": 22,
"output_tokens": 8,
"total_tokens": 30
}
}
流式传输模型输出
import { HumanMessage, SystemMessage } from "@langchain/core/messages";
const messages = [
new SystemMessage(
"You are a helpful assistant which telling short-info about provided topic."
),
new HumanMessage("moon"),
];
const stream = await instance.stream(messages);
for await (const chunk of stream) {
console.log(chunk);
}
The
Moon
is
Earth
'
s
only
natural
satellite
and
工具调用
import { tool } from "@langchain/core/tools";
import { z } from "zod";
const calculatorSchema = z.object({
operation: z
.enum(["add", "subtract", "multiply", "divide"])
.describe("The type of operation to execute."),
number1: z.number().describe("The first number to operate on."),
number2: z.number().describe("The second number to operate on."),
});
const calculatorTool = tool(
async ({ operation, number1, number2 }) => {
if (operation === "add") {
return `${number1 + number2}`;
} else if (operation === "subtract") {
return `${number1 - number2}`;
} else if (operation === "multiply") {
return `${number1 * number2}`;
} else if (operation === "divide") {
return `${number1 / number2}`;
} else {
throw new Error("Invalid operation.");
}
},
{
name: "calculator",
description: "Can perform mathematical operations.",
schema: calculatorSchema,
}
);
const instanceWithTools = instance.bindTools([calculatorTool]);
const res = await instanceWithTools.invoke("What is 3 * 12");
console.log(res);
AIMessage {
"id": "chat-d2214d0bdb794483a213b3211cf0d819",
"content": "",
"additional_kwargs": {
"tool_calls": [
{
"id": "chatcmpl-tool-257f3d39532141b89178c2120f81f0cb",
"type": "function",
"function": "[Object]"
}
]
},
"response_metadata": {
"tokenUsage": {
"completion_tokens": 38,
"prompt_tokens": 177,
"total_tokens": 215
},
"finish_reason": "tool_calls"
},
"tool_calls": [
{
"name": "calculator",
"args": {
"number1": 3,
"number2": 12,
"operation": "multiply"
},
"type": "tool_call",
"id": "chatcmpl-tool-257f3d39532141b89178c2120f81f0cb"
}
],
"invalid_tool_calls": [],
"usage_metadata": {
"input_tokens": 177,
"output_tokens": 38,
"total_tokens": 215
}
}
API 参考文档
有关所有 IBM watsonx.ai 特性和配置的详细文档,请访问 API
参考文档:API
文档
Related
- Chat model conceptual guide
- Chat model how-to guides

