Tutorial
Extend your watsonx Assistant chatbot by using an IBM Code Engine function extension
watsonx Assistant: Connect with Code Engine functionsSuppose you have a watsonx Assistant, and you need to call an IBM Cloud Code Engine function to perform some important task. These tasks can include the assistant needing to pull some data, the assistant needing to send an email, or the assistant needing to save some key details.
This tutorial explains how you can connect watsonx Assistant with IBM Cloud Code Engine functions by using an extension.
Prerequisites
To follow this tutorial, you need:
- An IBM watsonx Assistant instance
- An IBM Cloud Code Engine project
Estimated time
It should take you approximately 30 minutes to complete the tutorial.
Steps
Use the following steps to connect watsonx Assistant with IBM Cloud Code Engine. If you already have an IBM Cloud Code Engine function, you can skip Step 1.
Step 1. Create a sample Code Engine function
- Navigate to Functions under your IBM Cloud Code Engine project.
- Click Create, and name the project.
- Choose Python 3.11 for the Runtime image.
- Choose Code bundle options as Edit inline code.
- Use the default values for the rest of the fields.
Click Create, and wait for the function to be
Ready. This might take a couple of minutes.Get the function's public URL by clicking the Domain mappings tab. The URL is beside Public.
Enter the public URL in a browser with the following query parameters.
<replace-with-function-public-url>/?first_name=Jhon&last_name=Doe
Step 2. Connect the watsonx Assistant with the IBM Cloud Code Engine function
Create an OpenAPI specification JSON file.
- Download the sample spec file ext-code-engine-openapi.json.
- In the file, update the
serverssectionurlwith your IBM Cloud Code Engine function's public URL.
- Check the
pathssection in the file, and update the query parameters as needed. This is optional if you created the sample IBM Cloud Code Engine function as shown in the previous step.
- Save the changes to the file.
Create a custom extension in watsonx Assistant. (You can see this step in this video.)
- Go to your assistant by using the browser, and navigate to the Integrations menu from the panel on the left.
- Click Build custom extension, then click Next.
- Enter
ext-connect-code-engineas the Extension name, then click Next. - Upload the
ext-code-engine-openapi.jsonfile from the previous step, then click Next. - Review the server URL, operations, methods, paths, and request/response properties, then click Finish.
- You should see the ext-connect-code-engine tile in the catalog. Click Add, then click Add again.
- Add the extension to the draft environment by navigating through each tab, then clicking Finish.
- The extension is ready to use when creating actions now that it is in the draft environment.
Create a step to invoke the custom extension from the action. (You can see this step in this video.)
- Navigate to the Actions menu from the panel on the left.
- Click the Global settings
icon and scroll to the right to the last tab, Upload/Download
- Upload this sample-connect-code-engine-fun-action.json file in the Upload section. Click Upload and replace, then close the settings after a successful upload.
You see an action named invoke code engine function and its Status in red. Open this action to fix the missing extension error.
- Go to Step 3. This step uses an extension to invoke the IBM Cloud Code Engine function.
- Click Edit extension, choose the ext-connect-code-engine extension and the Invoke my Code Engine function operation.
Set the parameter values to:
first_name: Choose Action step variables, then 1. Enter first name:.
last_name: Choose Action step variables, then 2. Enter last name:, and click Apply.
Go to Step 4 and see how various response objects like
__ce_method,first_name, andlast_nameare printed by the assistant.
Test the integration. (You can see this step in this video.)
- Navigate to the Preview menu from the panel on the left.
- In the web chat, type
call my function, and send. - Provide any sample input when asked by the web chat for the first name (John) and last name (Doe).
- Notice the invocation of the IBM Cloud Code Engine function by watsonx Assistant with the message
Invoking extension to Code engine function... - In the response, look at the First name and Last name that is returned by the IBM Cloud Code Engine function as a response.
Summary
That's it! Using the previous steps, you can connect a watsonx Assistant web chat with IBM Cloud Code Engine. Because IBM is deprecating the Cloud Functions, you can use this as an alternative. There are many use cases to connect functions into watsonx Assistant, such as sending emails, request authentications, and other functions that work like connectors to push or pull data from your system.
Next steps
Generally, what you learned in this tutorial is applicable for creating any custom extension in a watsonx Assistant webchat. Explore the in-depth documentation for building the custom extensions, and try out more options on your own.