Skip to contentSkip to navigationSkip to topbar
On this pageProducts used

Connect your AI Assistant to Twilio Conversations with React


(warning)

Twilio Alpha - Developer Preview

AI Assistants is a Twilio Alpha(link takes you to an external page) project that's in Developer Preview.

View the current limitations for details about feature limits during developer preview.

While you can use any of Twilio's Conversations SDKs(link takes you to an external page) with AI Assistants, there is a dedicated React SDK that provides additional convenience and functionality on top of Twilio Conversations specifically for your AI Assistant use case. The added functionality includes React hooks and UI components powered by Twilio Paste(link takes you to an external page).

This document outlines the steps required to connect your AI Assistant to Twilio Conversations using the React SDK and build a chat interface from the built-in React components.


Setup

setup page anchor

Configure Twilio Conversations

configure-twilio-conversations page anchor

The AI Assistants React SDK is built on top of the Twilio Conversations SDK(link takes you to an external page) for all messaging communication. To use the SDK, you must first configure a Twilio Conversations service for your AI Assistant.

Next, generate client-side access tokens for Twilio Conversations.

When you generate the tokens, the identity used to generate the token will be used for any Customer Memory capabilities that you have enabled.

The command below uses npm to install the Alpha React and Conversations SDKs.

npm install @twilio-alpha/react @twilio/conversations

Use built-in UI components

use-built-in-ui-components page anchor

The React SDK comes with the built-in ability to render a full chat UI by using the <AssistantChat /> component.

To use the component, you will need both a valid Twilio Conversations access token, which you generated in the steps above, and the SID of your Assistant, which you can find in the Assistants section of the Twilio Console(link takes you to an external page). Make sure you replace <token> and <assistantSid> with the respective values.

1
import { AssistantChat } from "@twilio-alpha/assistants-react";
2
3
export function App() {
4
return <AssistantChat token="<token>" assistantSid="<assistantSid>" />;
5
}
(warning)

Warning

The access token is not your Twilio Auth Token. It's instead a Twilio Conversations access token. You should generate these on your backend and have your React code fetch the token from your endpoint.


If you prefer to use your own UI components with an AI Assistant, you can use the useAssistant hook instead. It returns both a messages array that contains all the messages as well as a sendMessage function to send a new message to your Assistant.

Below is an example of using the useAssistant hook to build a chat UI using only native HTML components.

1
import { useAssistant } from "@twilio-alpha/assistants-react";
2
3
export function App() {
4
const { messages, sendMessage } = useAssistant("<token>", {
5
assistantSid: "<assistantSid>",
6
});
7
8
function send(evt) {
9
evt.preventDefault();
10
sendMessage(evt.target.message.value);
11
}
12
13
return (
14
<>
15
<ul>
16
{messages.map((message) => {
17
return (
18
<li key={message.sid}>
19
{message.author}: {message.body}
20
</li>
21
);
22
})}
23
</ul>
24
<form onSubmit={send}>
25
<input type="text" name="message" />
26
<input type="submit" value="Send message" />
27
</form>
28
</>
29
);
30
}

By default, every time the <AssistantChat /> component and useAssistant hook are used, it will create a new Twilio conversation (aka a session). If you want to reuse the existing session between page refreshes, you can use the conversationSid property and the onConversationSetup handler to persist the session (for example, via localStorage(link takes you to an external page)). This gives you full control of how you want to manage the session. If you do not provide a conversationSid, the component will automatically create one.

1
import { useEffect, useState } from "react";
2
3
export function App() {
4
const [conversationSid, setConversationSid] = useState();
5
6
useEffect(() => {
7
// fetches an existing conversation SID from the local storage if it exists
8
setConversationSid(localStorage.getItem("CONVERSATIONS_SID") || undefined);
9
}, []);
10
11
function saveConversationSid(sid: string) {
12
localStorage.setItem("CONVERSATIONS_SID", sid);
13
}
14
15
return (
16
<AssistantChat
17
token={"<token>"}
18
conversationSid={conversationSid}
19
onConversationSetup={saveConversationSid}
20
assistantSid="<assistantSid>"
21
/>
22
);
23
}

Use UI Tools to have your Assistant interact with the UI

use-ui-tools-to-have-your-assistant-interact-with-the-ui page anchor

There are situations where you might want your AI Assistant to not only perform actions in backend systems using APIs, but also interact with the website where the user is chatting with the Assistant. In this case, you can use UI Tools.

You can use this to have the AI Assistant perform these actions, among others:

  • fill out a form on the page
  • show a disclaimer
  • render a richer UI for output from the Assistant
(warning)

Warning

The current solution is limited and only an initial proof of concept. Note that when the AI Assistant triggers a UI Tool, it does not know whether the Tool action succeeded.

To expose UI Tools using the AI Assistants React SDK, pass an object with function handlers into your <AssistantChat /> component or your useAssistant hook using the toolHandlers property, as in the example below.

1
import { AssistantChat } from "@twilio-alpha/assistants-react";
2
3
function fillForm(data) {
4
// data will contain the data that the AI Assistant used to trigger the tool
5
console.log(data);
6
}
7
8
export function App() {
9
return <AssistantChat token="<token>" assistantSid="<assistantSid>" toolHandlers={{fillForm}} />;
10
}

For your AI Assistant to call this Tool, you will have to configure the Tool within your Assistant's configuration in the Console. For the fillForm example above, the configuration might look similar to this:

FieldExample ConfigurationNotes
NameFill sales request formUse a name that's descriptive to you and the UI the same way as for any other tool. This will not have to match the name of your function. That name gets configured in the URL.
DescriptionUse this to prepopulate a talk to sales form on behalf of the customer. After you trigger this tool, ask the customer to cross check the request and submit it.Use this description to tell the AI Assistant when to use the tool.
Input
{
// anything you want to send to the UI tool
}
The Assistant will pass any data described here to the UI.
MethodPOST
URLhttps://<your-functions-domain>.twil.io/tools/ui-tools?toolName=<yourUiToolFunction>This URL assumes you've deployed the AI Assistants Samples code(link takes you to an external page) into your Twilio account using the instructions in the project.

The toolName value is the name of the function you passed into toolHandlers. In this example, it would be fillForm

You've now connected your AI Assistant to Twilio Conversations and explored the built-in UI from the React SDK, as well as seen how to bring your own UI and have your Assistant interact with the UI. View the links below to learn more about working with your AI Assistant:

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.