以编程方式使用文档

这将帮助您开始使用 IBM watsonx.ai 聊天模型。有关所有 IBM watsonx.ai 功能和配置的详细文档,请前往 IBM watsonx.ai.

概述

集成详情

可序列化PY 支持下载量版本
ChatWatsonx@langchain/ibm!NPM - 下载量!NPM - 版本

模型功能

工具调用结构化输出图像输入音频输入视频输入令牌级流式处理令牌使用量对数概率

设置

To access IBM watsonx.ai models you'll need to create a/an IBM watsonx.ai account, get an API key, and install the @langchain/ibm 集成包。

凭据

前往 IBM Cloud 注册 IBM watsonx.ai 并生成 API 密钥,或按如下所示提供任何其他身份验证方式。

IAM 身份验证

Bearer 令牌身份验证

IBM watsonx.ai 软件身份验证

将这些凭据放入您的环境变量中并初始化对象后,身份验证将自动进行。

身份验证也可以通过将这些值作为参数传递给新实例来完成。

IAM 身份验证

const props = {
  version: "YYYY-MM-DD",
  serviceUrl: "",
  projectId: "",
  watsonxAIAuthType: "iam",
  watsonxAIApikey: "",
};
const instance = new ChatWatsonx(props);

Bearer 令牌身份验证

const props = {
  version: "YYYY-MM-DD",
  serviceUrl: "",
  projectId: "",
  watsonxAIAuthType: "bearertoken",
  watsonxAIBearerToken: "",
};
const instance = new ChatWatsonx(props);

IBM watsonx.ai 软件身份验证

const props = {
  version: "YYYY-MM-DD",
  serviceUrl: "",
  projectId: "",
  watsonxAIAuthType: "cp4d",
  watsonxAIUsername: "",
  watsonxAIPassword: "",
  watsonxAIUrl: "<url>",
};
const instance = new ChatWatsonx(props);

如果您想获取模型调用的自动追踪,还可以设置您的 LangSmith API 密钥,只需取消下方注释:

# export LANGSMITH_TRACING="true"
# export LANGSMITH_API_KEY="your-api-key"

安装

LangChain IBM watsonx.ai 集成位于 @langchain/ibm package:

npm install @langchain/ibm @langchain/core
yarn add @langchain/ibm @langchain/core
pnpm add @langchain/ibm @langchain/core

实例化

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

const props = {
  maxTokens: 200,
  temperature: 0.5
};

const instance = new ChatWatsonx({
  version: "YYYY-MM-DD",
  serviceUrl: process.env.API_URL,
  projectId: "",
  // spaceId: "",
  // idOrName: "",
  model: "",
  ...props
});

Note:

  • - 您必须提供 spaceId, projectId or idOrName(部署 ID),除非您使用轻量级引擎,该引擎无需指定任一参数(请参阅 watsonx.ai 文档)
  • - 根据您配置的服务实例所在的区域,使用正确的 serviceUrl。

使用模型网关

const props = {
  maxTokens: 200,
  temperature: 0.5
};

const instance = new ChatWatsonx({
  version: "YYYY-MM-DD",
  serviceUrl: process.env.API_URL,
  model: "",
  modelGateway: true,
  ...props
});

要使用 LangChain 的模型网关,您需要先通过 @ibm-cloud/watsonx-ai SDK 或 watsonx.ai API 创建提供商并添加模型。请遵循此文档: - API. - SDK.

调用

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.

流式传输模型输出

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

工具调用

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 文档