Back to Mindsdb

Build a Chatbot with a Text2SQL Skill

docs/use-cases/ai_agents/create-chatbot.mdx

26.1.03.6 KB
Original Source

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 Text2SQL skill.

The CREATE CHATBOT statement requires the following components:

  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.

<Tip> Learn more about [chatbots here](/agents/chatbot). </Tip>

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.

<Tip> If you want to use Slack, follow [this link](/integrations/app-integrations/slack#method-1-chatbot-responds-in-direct-messages-to-a-slack-app) 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. </Tip>

AI Agent

Start by creating and deploying the model.

<Note> If you haven't created a LangChain engine, use the `CREATE ML_ENGINE` statement, as explained [here](/integrations/ai-engines/langchain#ai-engine). </Note>
sql
CREATE MODEL my_model
PREDICT answer
USING
    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',
    max_tokens=100,
    temperature=0,
    verbose=True,
    prompt_template='Answer the user input in a helpful way';

Here is the command to check its status:

sql
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 Text2SQL skill.

sql
CREATE SKILL text_to_sql_skill
USING
    type = 'text2sql',
    database = 'example_db',   -- this is a data source that must be connected to MindsDB with CREATE DATABASE statement
    tables = ['sales_data'],   -- this table comes from the connected example_db data source
    description = "Sales data that includes stores, sold products, and other sale details";

This skill enables a model to answer questions about data from the sales_data table.

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

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

Create Chatbot

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

sql
CREATE CHATBOT my_chatbot
USING
   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
   is_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:

sql
SELECT * FROM chatbots;

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