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.安装
LangChain 的 IBM 集成位于langchain-ibm 包中:
实例化
You might need to adjust modelparameters for different models or tasks. For details, refer to Available TextChatParameters.
WatsonxLLM class with the previously set parameters.
Note:
- To provide context for the API call, you must pass the
project_idorspace_id. To get your project or space ID, open your project or space, go to the Manage tab, and click General. For more information see: Project documentation or Deployment space documentation. - Depending on the region of your provisioned service instance, use one of the urls listed in watsonx.ai API Authentication.
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 聊天模型.
model_id, you can also pass the deployment_id of the previously deployed model with reference to a Prompt Template.
APIClient object into the ChatWatsonx class.
调用
To obtain completions, you can call the model directly using a string prompt.链式调用
CreateChatPromptTemplate objects which will be responsible for creating a random question.
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 theAIMessage has a tool_calls attribute. This contains in a standardized ToolCall format that is model-provider agnostic.
API 参考
有关所有ChatWatsonx 功能和配置的详细文档,请前往 API reference.
Connect these docs to Claude, VSCode, and more via MCP for real-time answers.

