Creating An AI Powered Chat Bot
AI Providers
Before using any of the AI features in ThinkAutomation, you first need to setup an AI Provider in the ThinkAutomation Server Settings - AI Providers section.
ThinkAutomation supports 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.
Multiple AI Providers can be setup, and you can have a different AI Provider that uses the same provider type, but using a different model.
OptimaGPT
OptimaGPT is Parker Software's on-premises or private-cloud hosted AI Server. OptimaGPT supports a variety of freely available public AI models. It enables you to use AI locally, without sending private or sensitive data to a 3rd party. This makes it ideal for organizations needing to avoid external data transfer due to privacy regulations. See: OptimaGPT for more information.
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 automation action to send the user's incoming message to an AI and then send the AI response back to the user. Before that, we use the Ask AI action to search the Embedded Knowledge Store using the user's message as the search text 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 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.
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:
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 {knowledge store 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 {knowledge store 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 {knowledge store 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 using the Convert Document To Text action, and then adds the text to the Vector Database using the Embedded Vector Database automation action.
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.
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:
You can now ask questions relating to the documents. Any new or updated documents will be automatically added to the Vector Database - and be available for answers.
The benefit of this approach is that you do not need to upload documents to a 3rd party AI provider. This saves time, reduces costs and helps with privacy/regulatory concerns. You could also update the same Vector Database from other sources (email, CRM data etc.).
As with the Creating An AI Powered Email Responder sample, you could setup your Automation to return the response via email, instead of a chat form. Then user's can email their questions to the bot.
Using AI To Chat With Local Databases
You can also use ThinkAutomation and AI to chat with your own local databases. You can quickly create a bot that allows you to asks questions about the data using natural language.
In ThinkAutomation, create a Message Source. Give it a name (such as 'Customers Bot'). 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. Unselect the Use AI To Respond To Incoming Chat Messages and Use A Knowledge Store To Provide AI Context options - since we will be creating the Automation manually. Click Next to save.
Edit the default Automation and change it to:
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.
We then use the Lookup From A Database Using AI action to read data from our database using the %ChatMessage% variable for the query text. This action uses AI to convert the natural language question into a SQL SELECT statement. It does this by sending details of your database schema long with the question to the AI.
The database schema is generated automatically by clicking the Load Schema button. This is done once, when you create the action. You should include additional comments in the schema to describe the tables and any specific information about column data. This will help the AI in generating queries.
Most of the time the AI can figure out what a column is based on its name (IE: It will know that the 'CompanyName' column is used for the company name). However for vague column names or where the value represents a specific thing - you should add comments to the schema text. For example:
-- Movie Reviews table (a record for each movie review)
-- The score column is between 1 and 100 - with 100 being the highest rated
-- The orig_lang column is the language that the movie is in (eg: 'English')
-- The country column is a 2 character ISO country code (in upper case)
CREATE TABLE [Reviews] (
[names] TEXT,
[date_x] DATETIME,
[score] INT,
[genre] TEXT,
[overview] TEXT,
[orig_lang] TEXT,
[revenue] REAL,
[country] TEXT
);
The generated SQL statement is then used to read the actual data from your database. The data is returned as text in a Markdown table format and assigned to the %MarkdownData% variable.
We could return the Markdown table directly to the chat. However we use the Ask AI action to add the data to the conversation as context using the Add Context To A Conversation operation.
We then add an additional Ask AI action to ask the question again. This will return a human response rather than just a table of data.
The AI response is assigned to the %Response% variable, which we then return to the chat form.
You can expand on this by adding additional static context that describes the data and how it is used within your business. This will improve the AI response.
To test your bot, right-click the Message Source and select Open Public Web Form or Open Local Web Form.
As with the Creating An AI Powered Email Responder sample, you could setup your Automation to return the response via email, instead of a chat form. Then user's can email their questions to the bot.
Combining Local Documents And Database Lookups
You can combine document lookups - as shown in the Using AI To Chat Local Documents and database lookups as shown in the Using AI To Chat With Local Databases samples within a single Automation.
You could use the Lookup From A Database Using AI action to lookup data from a database, and then use the Ask AI action with the Add Static Context option to add any data returned to the conversation. This could be done prior to the Ask AI with the Add Context From A Vector Database search from the document store.
Context can be added by any means using the Ask AI - Add Static Context operation. The more context you provide the AI - the more data it has to work with when giving a response.