Build a chatbot connected to a Knowledge Graph
Agent Builder contains several UI and blueprint blocks that you can use to build a chatbot.
This tutorial demonstrates how to build a general chatbot that is connected to the Palmyra X5 model with the following steps:
- Add a Chatbot block to the UI.
- Start a new chat conversation when the agent first loads
- Add messages to the chat whenever a user types in the Chatbot component
- Generate a response from the Palmyra X5 model to each message
After you build the initial chatbot, you can also connect a Knowledge Graph to the chatbot to answer questions about data related to your business or a specific dataset.
If you are unfamiliar with Agent Builder interface, check out the Agent Builder Quickstart.
Build the UI
The UI for the agent contains a single Chatbot block.
Add a Chatbot block to the UI
Drag and drop a Chatbot block onto the Interface canvas.
In the block’s configuration panel, update the following fields:
- Conversation:
@{chat}
. This is the state variable that stores the chat conversation. - Assistant initials:
AI
. This is the text that appears next to the assistant’s messages in the chat. - User initials:
YOU
. This is the text that appears next to the user’s messages in the chat. - Use markdown:
no
. You can change this toyes
if you want to parse Markdown formatting in messages. - Enable file upload:
no
. This disables the file uploads in the chatbot. - Placeholder:
Type your message here...
. This is the text that appears in the input field when it is empty.
Build the blueprint
The blueprint for the agent contains the following blocks:
- UI trigger block to run the blueprint when a user enters a chat message
- Start chat conversation block to start a new chat conversation if it doesn’t already exist
- Add chat message block to add the user’s message to the chat conversation
- Generate chat response block to generate a response from the Palmyra X5 model to each message
Add a UI trigger for the chat message event
To run the blueprint when a user enters a chat message, add a UI trigger block to the canvas. In the block’s configuration panel, update the following fields:
- Component Id: select the
Chatbot
component from the dropdown of UI blocks - Event type:
wf-chatbot-message
Add a Start chat conversation block
Add a Start chat conversation block to the canvas, which runs after the UI trigger block and creates a new chat conversation if it doesn’t already exist. In the block’s configuration panel, update the following fields:
- Conversation state element:
chat
- Model:
Palmyra X5
You can also update the Temperature and Max output tokens fields to control the output of the chat conversation.
Add chat message to the conversation
Add an Add chat message block to the canvas after the Start chat conversation block. In the block’s configuration panel, update the following fields:
- Conversation state element:
chat
- Message:
@{payload}
. This references thepayload
variable set by the previous UI trigger. Thepayload
variable created by the UI trigger contains the message from the user in the following format:{"message": "<user's message>", "role": "user"}
.
Generate a chat reply
Add a Generate chat response block to the canvas after the chat message event triggers. This passes the chat conversation to the Palmyra X5 model to generate a response. In the block’s configuration panel, update the following fields:
- Conversation state element:
chat
- Use streaming:
yes
. This configures the block to stream the response from the model to the chat in real time, rather than waiting for the entire response to be generated before displaying it.
Once the response is generated, the block adds a new message to the chat conversation with the following format: {"message": "<assistant's response>", "role": "assistant"}
.
This block is where you would add additional tools to the chatbot, under the Add tool section of the configuration panel. See Connect a Knowledge Graph to the chatbot for an example.
Preview the agent
To preview the agent, click the Preview button in the top right corner of the page.
You can then type a message in the chatbot and see the response from the model.
Connect a Knowledge Graph to the chatbot
You can connect one or more Knowledge Graphs to the chatbot to answer questions about data related to your business or a specific dataset.
To connect a Knowledge Graph to the chatbot, update the Generate chat reply block.
Open the Generate chat reply block configuration panel
Click the Generate chat reply block to open its configuration panel.
Add a Knowledge Graph tool
Click the Add tool button and select Knowledge graph under Tool type.
Under Graph id(s), select one or more Knowledge Graphs you want to connect to the chatbot. The example below connects the chatbot to a Knowledge Graph that contains developer docs at https://dev.writer.com
.
Now when the user asks questions related to data in the connected Knowledge Graphs, the chatbot can reference the data in the Knowledge Graphs to answer the question.
If you encounter any issues, refer to the Troubleshooting guide for debugging information.