MindsDB provides the CREATE CHATBOT statement that lets you customize your chatbot with an AI model and a data source of your choice. Follow this tutorial to learn build a chatbot with a knowledge base.

The CREATE CHATBOT statement requires the following componenets:

  1. Chat app: A connection to a chat app, such as Slack or MS Teams.

  2. AI agent: An AI agent that comes with an AI model trained with the provided training data. Learn more about AI agents here.

Learn more about chatbots here.

Let’s go over getting all the components ready.

Chatbot Components

Chat App

Use the CREATE DATABASE statement to connect the chat app to MindsDB.

If you want to use Slack, follow this link to setup a Slack app, generate required tokens, and connect it to MindsDB.

If you want to use MS Teams, follow this link to generate required tokens and connect it to MindsDB.

AI Agent

Start by creating and deploying the model.

If you haven’t created a LangChain engine, use the CREATE ML_ENGINE statement, as explained here.

PREDICT answer
    engine = 'langchain',
    input_column = 'question',
    openai_api_key = 'your-model-api-key', -- choose one of OpenAI (openai_api_key) or Anthropic (anthropic_api_key)
    model_name='gpt-4', -- optional model name from OpenAI or Anthropic
    mode = 'conversational',
    user_column = 'question' ,
    assistant_column = 'answer',
    prompt_template='Answer the user input in a helpful way';

Here is the command to check its status:

DESCRIBE my_model;

The status should read complete before proceeding.

Next step is to create one or more skills for an AI agent. Here we create a knowledge base and assign it as a skill.

In this example, let’s create an embedding model (you can choose one from OpenAI, Hugging Face, or LangChain) for the knowledge base.

CREATE ML_ENGINE openai_engine
FROM openai
   openai_api_key = 'your-openai-api-key';

CREATE MODEL embedding_model
PREDICT embeddings
   engine = 'openai_engine',
   question_column = 'content';

Now let’s create a knowledge base that uses this embedding model and the default storage vector database (that is, ChromaDB).

CREATE KNOWLEDGE BASE my_knowledge_base
   model = embedding_model;

This is how you can insert data into the knowledge base and select it.

INSERT INTO my_knowledge_base (content)
VALUES ('I drink tea.');

SELECT * FROM my_knowledge_base;

Use this knowledge base to create a skill for an agent:

    type = 'knowledge_base',
    source = 'my_knowledge_base', -- this must be created with CREATE KNOWLEDGE BASE
    description = 'My data';      -- data description to help the agent know when to use the knowledge base

This skill enables a model to answer questions about data from the knowledge base.

Now let’s create an AI agent using the above model and skill.

CREATE AGENT support_agent
model = my_model,         -- this was created with CREATE MODEL
skills = ['kb_skill'];    -- this was created with CREATE SKILL

Create Chatbot

Once all the components are ready, let’s proceed to creating the chatbot.

   database = 'chat_app',     -- this parameters stores a connection to a chat app, like Slack or MS Teams
   agent = 'support_agent',   -- this parameter stores an agent name, which was create with CREATE AGENT
   enable_dms = true,         -- this parameter is optional and enable direct messages with the chatbot
   running = true;            -- this parameter is optional and set to true by default, meaning that the chatbot is running

The database parameter stores connection to a chat app. And the agent parameter stores an AI agent created by passing a model and training data.

You can query all chatbot using this query:

SELECT * FROM chatbots;

Now you can go to Slack or MS Teams and chat with the chatbot created with MindsDB.