Skip to content

Creating A Web Chat Bot

Using ChatGPT With A Local Knowledge Store

You can use ThinkAutomation to create web chat forms. A web chat form is similar to the Web Form message source type, except that the users message and the automation response is shown in a conversation-style UI.

Your Automation will receive each chat message as a new incoming message. The return value from your Automation is then sent back to the chat form and displayed to the user.

In this example we will use the ChatGPT action to send the user's incoming message to ChatGPT and then send the ChatGPT response back to the user. We will also use the Embedded Knowledge Store action so that we can give ChatGPT some context based on the user's message. This will enable ChatGPT to answer the question even though it has no training data.

You first need to setup an Open AI account. Go to https://openai.com and click the Sign Up button to create an account. Then go to https://platform.openai.com/overview, click your account and then select View API Keys. Click the Create Secret Key button to create a new API key. Make a note of this key as it is only displayed once.

Go into ThinkAutomation Server Settings - Integrations tab. In the ChatGPT Section - paste your API key.

You then need to create a Knowledge Store collection. A Knowledge Store collection is a database of articles relating to the knowledge that you want your chat bot to be able to answer questions on. You can create a Knowledge Store collection using the Embedded Knowledge Store Browser on the Studio File menu. Here you can import files & documents. You can also use a separate Automation to update your knowledge store using the Embedded Knowledge Store action. For example, you could have an Automation that takes incoming emails and updates the knowledge store - users can then simply send an email to a specific address when they want to add or update articles.

Once your Knowledge Store is setup you are ready to use ChatGPT with ThinkAutomation.

In ThinkAutomation, create a Message Source. Select Web Chat as the message source type and click Next.

Leave the Web Chat properties as they are for now (you can always tweak these later). Click Next and Next again to save the Message Source.

When you save a new Web Chat Message Source, ThinkAutomation will create a default Automation to receive the chat messages. The default Automation is shown below:

MyBot Automation
// Automation Actions Follow
// Get the chat message text
ChatMessage=HTML Entity Decode(%Msg_Body%)
ChatMessage=Trim(%ChatMessage%)
 
// %ChatMessage% contains the chat message received
 
If%ChatMessage%Equal To[webchatstart]Then
// Respond to chat starting
ReturnWelcome %Msg_FromName%! I can answer questions about KnowledgeBase
End If
 
// Add our default context
ChatGPTAdd ContextYou are a very enthusiastic representative working at Parker Software. Given the provided sections from our KnowledgeBase documentation, answer the user's question using only that information, outputted in markdown format.To Conversation%Msg_FromName%
 
// Add context from the local knowledge store collection KnowledgeBase
ChatGPTAdd ContextFrom Knowledge StoreKnowledgeBaseSearch%ChatMessage%Return4Most RelevantTo Conversation%Msg_FromName%
 
// Send to ChatGPT to get the response
ChatGPTResponse=
ChatGPTResponse=ChatGPTSayQuestion: """ %ChatMessage% """ Answer:Usinggpt-3.5-turbo-16kConversation%Msg_FromName%
 
// Return the response to the chat
Return%ChatGPTResponse%

The Web Chat Message source receives user chat messages in the %Msg_Body% built-in variable. Any HTML characters will be decoded, so the first two Actions use the Set Variable action to HTML Entity Decode the %Msg_Body% and trim. The variable %ChatMessage% will then contain the incoming chat message.

The Web Chat form will automatically send the message [webchatstart] after the user has completed the Start Form form and is ready to chat. We check for this and send back a generic welcome message:

Welcome %Msg_FromName%! I can answer questions about {your knowledge store collection name}.

You can change this - or set it to blank to not to show a welcome message.

We then add some default context to the conversation. This gives ChatGPT some general information about us and tells ChatGPT how to behave. The default will be set to:

You are a very enthusiastic representative working at {your company name}. Given the provided sections from our {your knowledge store collection name} documentation, answer the user's question using only that information, outputted in markdown format.

You can change this if required.

The following action adds more context to the conversation. This time we search the Knowledge Store for the top 4 most relevant articles relating to the %ChatMessage% - IE: The last message received from the chat form.

Note: You could also add context from a database or any other source. For example, you could do a database lookup of recent orders for a customer based on their email address, and add these as context allowing the user to ask questions about recent orders.

After adding context we then call ChatGPT itself with the user's question. The prompt is formatted as follows:

Question: """
%ChatMessage%
"""

Answer:

This format is not strictly required. You could just send %ChatMessage% - however clearly separating the question tells ChatGPT that it needs to provide an answer.

We then return the response from ChatGPT. This response will be shown in the web chat form. The user can then send another message and the process repeats. Each time further context is added. The old context remains until it reaches the ChatGPT token limit. When the token limit is reached ThinkAutomation automatically removes the oldest context. When ThinkAutomation adds context to the conversation - it only adds items that do not already exist in the conversation, so its safe to re-add context that may already be there.

Any changes you make to your Knowledge Store will take effect immediately - so as you add more articles the accuracy of ChatGPT responses will improve.

All of the ChatGPT and context actions require a Conversation Id. This is simply some text that uniquely identifies the current web chat user. By default the Web Chat form asks for the users Name and Email Address when the chat starts. These will be set to the incoming message From address. This means you can use the built-in message variables %Msg_FromName% or %Msg_FromEmail% as the conversation id. You can also use the built-in variable %Msg_ConversationId% which is a hash of the from/to email addresses and subject. All incoming messages and responses from the same conversation id will be grouped as a 'conversation' - even over multiple automation executions. If you do not ask for a name or email at the start of the chat you could use %Msg_FromIP% - which is the built-in variable containing the web chat user's IP Address.

Each incoming chat message causes your Automation to execute. So each chat message & Automation return value is stored in your Message Store database, just like regular messages.

Creating A ChatGPT Powered Email Responder

You can use the same process to create a ChatGPT powered email responder. You could create a first-line support email address that responds using the same Knowledge Store as the chat form.

The incoming message doesn't have to be a Web Chat form. It could be email, SMS, Teams etc - in fact any of the ThinkAutomation Message Source types.

Create an Email Message Source in ThinkAutomation - that reads the mailbox you want to use for your email 'bot'. The Automation that executes for new email messages will be similar to the Web Chat Automation, with some minor differences.

First - we cant use the %Msg_Body% built-in variable as the prompt text. The reason is that if a user replies to a bot email, then the new incoming email will contain the whole thread. We cant send the whole thread to ChatGPT each time - since the knowledge base search wont be as targeted and the text may go above the token limit. The conversation context will already have any previous email related context anyway.

Instead we can use the %Msg_LastReplyBody% built-in variable. This field is automatically set to the email body WITHOUT all previous quoted replies.

The other main difference is the default context. This needs to be something like:

Your name is '{botname}' and you are a very enthusiastic representative working at {your company name} answering emails about {your knowledge store collection name}. Given the provided sections from the  knowledge base, answer the question using only that information, outputted in markdown format.

If an answer cannot be found in the information provided, respond with 'I cannot help with that' only. Do not try and answer the question if the information is not provided.

Add a friendly greeting and sign off message to your response. Your email address is '{bot email}'.

My email address is %Msg_FromEmail%

We tell ChatGPT that it is answering emails - and we tell it to add a friendly greeting and sign off message. We also tell it what its name is.

Responses will then be more like:

Hi {Name},

{response}

Best regards
{bot name}

Alternatively you could use the same default context as with Web Chat forms and add the greeting and sign off/footer in the Send Email Action.

After receiving the response from ChatGPT you would then send an email back to the sender, with the message body set to the ChatGPT response.