Path: blob/master/cloud/notebooks/rest_api/deployments/foundation_models/chat/Use watsonx to make simple chat conversation and tool calls.ipynb
5214 views

Use watsonx to make simple chat conversation and tool calls
Disclaimers
Use only Projects and Spaces that are available in watsonx context.
Notebook content
This notebook provides a detailed demonstration of the steps and code required to showcase support for Chat models, including the integration of tools and watsonx.ai models.
Some familiarity with Python is helpful. This notebook uses Python 3.12.
Learning goal
The purpose of this notebook is to demonstrate how to use Chat models, by using tools.
Table of Contents
This notebook contains the following parts:
Set up the environment
Before you use the sample code in this notebook, you must perform the following setup tasks:
Create a watsonx.ai Runtime Service instance (a free plan is offered and information about how to create the instance can be found here).
Install and import the datasets and dependencies
Inferencing class
This cell defines a class that makes a REST API call to the watsonx Foundation Model inferencing API that we will use to generate output from the provided input. The class takes the access token created in the previous step, and uses it to make a REST API call with input, model id and model parameters. The response from the API call is returned as the cell output.
Action: Provide watsonx.ai Runtime url to work with watsonx.ai.
Define a ModelInferanceChat class for chat generation.
watsonx API connection
Use the code cell below to define the watsonx.ai credentials that are required to work with watsonx Foundation Model inferencing.
Action: Provide the IBM Cloud user API key. For details, see Managing user API keys.
Defining the project id
You need to provide the project ID to give the Foundation Model the context for the call. If you have a default project ID set in Watson Studio, the notebook obtains that project ID. Otherwise, you need to provide the project ID in the code cell below.
List available chat models
Specify the model_id of the model you will use for the chat with tools.
Initialize the ModelInferanceChat class.
Hint: Your authentication token might expire, if so please regenerate the access_token reinitialize the ModelInferanceChat class.
Working with simple chat message
Working with simple chat message with params
Work with an advanced chat message
Work with chat messages using tools and tool_choice
Work with chat messages using tools and tool_choice_option
Summary and next steps
You successfully completed this notebook!
You learned how to work with chat models using tools and watsonx.ai.
Check out our Online Documentation for more samples, tutorials, documentation, how-tos, and blog posts.
Author
Mateusz Szewczyk, Software Engineer at watsonx.ai.
Copyright © 2026 IBM. This notebook and its source code are released under the terms of the MIT License.