Skip to content

Creating An AI Powered Chat Bot

Using AI 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 Ask AI action to send the user's incoming message to an AI and then send the AI response back to the user. We will also use the Embedded Knowledge Store action so that we can give the AI some context based on the user's message. This will enable the AI to answer the question even though it has no training data.

You first need to setup an AI Provider in the ThinkAutomation Server Settings - AI Providers section. You can use OpenAI ChatGPT, Azure OpenAI, xGrok or OptimaGPT (Parker Software's on-premises AI server). To create an OpenAI 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.

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 AI 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
Ask AIAdd 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
Ask AIAdd ContextFrom Knowledge StoreKnowledgeBaseSearch%ChatMessage%Return5Most RelevantTo Conversation%Msg_FromName%
 
// Send to AI to get the response
AIResponse=
AIResponse=Ask AISayQuestion: """ %ChatMessage% """ Answer:Usinggpt-4.1-miniConversation%Msg_FromName%
 
// Return the response to the chat
Return%AIResponse%

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 the AI some general information about us and tells the AI 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 5 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 the AI 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 the AI that it needs to provide an answer.

We then return the response from the AI. 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 AI 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 the AI responses will improve.

All of the AI 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.

To test your bot, right-click the Message Source and select Open Public Web Form or Open Local Web Form.

Creating An AI Powered Email Responder

You can use the same process to create an AI 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 the AI 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 the AI 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 the AI you would then send an email back to the sender, with the message body set to the AI response.

Creating An AI Powered Chat Bot That Can Answer Questions About Local Documents

Suppose you have a local folder(s) on your file system that contain company documents. These may be in PDF, Word, Text or other formats. We can use ThinkAutomation to create an AI powered bot that can answer questions about the content of these documents. If documents are updated or more documents are added to the folder, ThinkAutomation will automatically make the changes available to the AI.

ThinkAutomation includes a 'Vector Database' action. A vector database is a type of database designed to store, index, and search data represented as vectors, typically high-dimensional numerical arrays. When searching, instead of exact matches (like in traditional databases), vector databases find similar items using approximate nearest neighbor (ANN) algorithms.

In this example, we have a folder containing incoming resume's. We want to create a chat bot that can answer questions about job candidates.

In ThinkAutomation, create a Message Source. For the Name enter a name that represents the subject matter (eg: 'Job Applicants'). Select File Pickup as the message source type and click Next. Select the folder containing your documents, and enable the Include Sub-Folders option if the folder contains sub-folders.

Click Next:

Enable the Index Document Contents To Use With AI option and the Also Create Chat Message Source And Automation. The Vector Database Collection will be set to the Message Source name - but can be changed, or you can select an existing one (if you are using multiple message sources to update the same Vector Database).

Click Next.

ThinkAutomation will then automatically create an Automation to read the folder contents and add the document text to the Vector Database:

The File Pickup Message Source scans the selected folder. The Automation for that message source converts supported document types to plain text and adds the text to the Vector Database:

Job Applicants Automation
// Automation Actions Follow
// Extract File Data
Path=Extract FieldFrom%Msg_Body%Json Path"Path"(String)
Name=Extract FieldFrom%Msg_Body%Json Path"Name"(String)
Extension=Extract FieldFrom%Msg_Body%Json Path"Extension"(String)
Size=Extract FieldFrom%Msg_Body%Json Path"Size"(Number)
Created=Extract FieldFrom%Msg_Body%Json Path"Created"(DateTime)
Modified=Extract FieldFrom%Msg_Body%Json Path"Modified"(DateTime)
Description=Extract FieldFrom%Msg_Body%Json Path"Description"(String)
CompanyName=Extract FieldFrom%Msg_Body%Json Path"CompanyName"(String)
Version=Extract FieldFrom%Msg_Body%Json Path"Version"(String)
Product=Extract FieldFrom%Msg_Body%Json Path"Product"(String)
 
// Convert Document To Text
DocumentText=
Extension=Extract File Extension(%Path%)
If%Extension%Contains One Ofpdf,doc,docx,rtf,odt,txt,md,html,htm,xls,xlsxThen
DocumentText=Convert Document To TextFile%Path%
End If
 
If%DocumentText%Is Not BlankThen
DocumentText="Dated: %Modified% Source: %Path% %DocumentText%"
// Store In Vector Database
Embedded Vector DatabaseUpdateIn"jobcandidates"Key%Path%=%DocumentText%
Return"Added %Path%"
Else
Return"No text added for %Path%"
End If

The Message Source will be disabled when it's first created. Right-click it and select Enable to start the process of indexing your documents. This may take several minutes on the first scan if your folder contains many documents.

An additional Web Chat Message Source and Automation will also be created. The Web Chat 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.

Job Applicants AI Chat Automation
// Get the chat message text
ChatMessage=HTML Entity Decode(%Msg_Body%)
ChatMessage=Mask Profanities(%ChatMessage%)
ChatMessage=Trim(%ChatMessage%)
 
// %ChatMessage% contains the chat message received
 
If%ChatMessage%Equal To"[webchatstart]"Then
// Respond to chat starting
Return"Welcome %Msg_FromName%! I can answer questions about Job Applicants."
End If
 
// Add our default context
Ask AIAdd Context"Your name is Job Applicants Bot. You are a very enthusiastic representative answering chat messages for Parker Software. Answer my questions using the provided articles from the Job Applicants documentation. If you are unsure and the answer is not explicitly written in the articles, say "Sorry, I don't know how to help with that." My name is '%Msg_FromName%'."(Required)To Conversation%Msg_ConversationId%
 
// Add context from the local vector database collection jobcandidates
Ask AIAdd ContextFrom Vector Database"jobcandidates"Search%ChatMessage%Return10Most RelevantTo Conversation%Msg_ConversationId%
 
// Send to AI to get the response
AIResponse=
AIResponse=Ask AISay"Question: """ %ChatMessage% """ Answer:"UsingOpenAI [gpt-4.1]Conversation%Msg_ConversationId%
 
// Return the response to the chat
Return%AIResponse%

This Automation is similar to the Chat Bot created earlier, except we are searching for context from the Vector Database instead of the Knowledge Store. The Vector Database is more suitable for large volumes of data.

To test your bot, right-click the AI Chat Message Source and select Open Public Web Form or Open Local Web Form: