Skip to main content
ChatWatsonx is a wrapper for IBM watsonx.ai foundation models.
The aim of these examples is to show how to communicate with watsonx.ai models using LangChain LLMs API.

概述

集成详情

模型功能

设置

要访问 IBM watsonx.ai 模型,您需要创建一个 IBM watsonx.ai 账户,获取 API 密钥,并安装 langchain-ibm 集成包。

凭证

The cell below defines the credentials required to work with watsonx Foundation Model inferencing. Action: Provide the IBM Cloud user API key. For details, see Managing user API keys.
Additionally you are able to pass additional secrets as an 环境变量。

安装

LangChain 的 IBM 集成位于 langchain-ibm 包中:

实例化

You might need to adjust model parameters for different models or tasks. For details, refer to Available TextChatParameters.
Initialize the WatsonxLLM class with the previously set parameters. Note: In this example, we’ll use the project_id and Dallas URL. You need to specify the model_id that will be used for inferencing. You can find the list of all the available models in Supported 聊天模型.
Alternatively, you can use Cloud Pak for Data credentials. For details, see watsonx.ai software setup.
Instead of model_id, you can also pass the deployment_id of the previously deployed model with reference to a Prompt Template.
For certain requirements, there is an option to pass the IBM’s APIClient object into the ChatWatsonx class.

调用

To obtain completions, you can call the model directly using a string prompt.

链式调用

Create ChatPromptTemplate objects which will be responsible for creating a random question.
Provide a inputs and run the chain.

Streaming the model output

You can stream the model output.

Batch the model output

You can batch the model output.

工具调用

ChatWatsonx.bind_tools()

AIMessage.tool_calls

Notice that the AIMessage has a tool_calls attribute. This contains in a standardized ToolCall format that is model-provider agnostic.

API 参考

有关所有 ChatWatsonx 功能和配置的详细文档,请前往 API reference.