跳到主要内容

IBM watsonx.ai

这将帮助您开始使用 LangChain 的 IBM 文本补全模型 (LLM)。 有关 IBM watsonx.ai 功能和配置选项的详细文档,请参阅 IBM watsonx.ai

概述

集成详情

本地可序列化PY 支持包下载包最新版本
WatsonxLLM@langchain/communityNPM - DownloadsNPM - Version

设置

要访问 IBM WatsonxAI 模型,您需要创建一个 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 包中

提示

有关安装集成包的通用说明,请参阅 此部分

yarn add @langchain/community @langchain/core

实例化

现在我们可以实例化我们的模型对象并生成聊天补全

import { WatsonxLLM } from "@langchain/community/llms/ibm";

const props = {
decoding_method: "sample",
maxNewTokens: 100,
minNewTokens: 1,
temperature: 0.5,
topK: 50,
topP: 1,
};
const instance = new WatsonxLLM({
version: "YYYY-MM-DD",
serviceUrl: process.env.API_URL,
projectId: "<PROJECT_ID>",
// spaceId: "<SPACE_ID>",
// idOrName: "<DEPLOYMENT_ID>",
model: "<MODEL_ID>",
...props,
});

注意

  • 您必须提供 spaceIdprojectIdidOrName(部署 ID),除非您使用轻量级引擎,该引擎无需指定任何一个即可工作(请参阅 watsonx.ai 文档
  • 根据您配置的服务实例的区域,使用正确的 serviceUrl。
  • 您需要通过 model_id 指定要用于推理的模型。

调用和生成

const result = await instance.invoke("Print hello world.");
console.log(result);

const results = await instance.generate([
"Print hello world.",
"Print bye, bye world!",
]);
console.log(results);

print('Hello world.')<|endoftext|>
{
generations: [ [ [Object] ], [ [Object] ] ],
llmOutput: { tokenUsage: { generated_token_count: 28, input_token_count: 10 } }
}

链式调用

我们可以像这样将我们的补全模型与提示模板链接起来

import { PromptTemplate } from "@langchain/core/prompts";

const prompt = PromptTemplate.fromTemplate(
"How to say {input} in {output_language}:\n"
);

const chain = prompt.pipe(instance);
await chain.invoke({
output_language: "German",
input: "I love programming.",
});
Ich liebe Programmieren.

To express that you are passionate about programming in German,

属性覆盖

在初始化时传递的属性将在对象的整个生命周期内持续存在,但是您可以通过传递第二个参数来覆盖单个方法调用的属性,如下所示

const result2 = await instance.invoke("Print hello world.", {
parameters: {
maxNewTokens: 100,
},
});
console.log(result2);

print('Hello world.')<|endoftext|>

分词

此包具有其自定义的 getNumTokens 实现,该实现返回将要使用的确切令牌数量。

const tokens = await instance.getNumTokens("Print hello world.");
console.log(tokens);
4

API 参考

有关所有 IBM watsonx.ai 功能和配置的详细文档,请访问 API 参考:API 文档


此页面是否对您有帮助?


您也可以留下详细的反馈 在 GitHub 上.