docs/use-cases/ai_agents/build_ai_agents.mdx
MindsDB provides a custom syntax to build AI agents that comprises an AI model augmented with users' data access. AI agents can be connected to a chat interface, like Slack or MS Teams, to create chatbots.
See details following this link for Agents and this link for Chatbots.
This tutorial demonstrates how to build AI agents with MindsDB using MindsDB SQL editor. This can be also accomplished with APIs and Python SDK.
Let's list all the steps required to build an AI agent.
<Steps> <Step title="Create a conversational model"> Create a conversational model using the [LangChain integration](/integrations/ai-engines/langchain). </Step> <Step title="Create skills"> Create one or more skills to be assigned to an agent. *Note that skills store data to be passed to an agent, so it is required to connect users' data to MindsDB before creating skills.* </Step> <Step title="Create an AI agent"> Create an AI agent providing the conversational model and the set of skills. </Step> <Step title="Create a chatbot"> Optionally, connect an agent to a chat interface to create a chatbot. </Step> </Steps>The following sections walk you through the process of building an AI agent.
Use the CREATE MODEL statement below to create a conversational model. If required, adjust the parameters and prompts to fit your use case.
CREATE MODEL conversational_model
PREDICT answer
USING
engine = 'langchain',
openai_api_key = 'YOUR_OPENAI_API_KEY_HERE',
model_name = 'gpt-4',
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';
Ensure that the model status reads complete using this command:
DESCRIBE conversational_model;
Learn more about models created with LangChain.
A skill is essentially users' data fed to the model, so the model can answer questions over users' data.
First, connect your database to MindsDB. Here the sample database is used.
CREATE DATABASE datasource
WITH ENGINE = "postgres",
PARAMETERS = {
"user": "demo_user",
"password": "demo_password",
"host": "samples.mindsdb.com",
"port": "5432",
"database": "demo",
"schema": "demo_data"
};
Create a skill using the connected data.
CREATE SKILL text2sql_skill
USING
type = 'text2sql',
database = 'datasource', -- connect your database with CREATE DATABASE and pass its name here
tables = ['car_sales'], -- list table(s) to be made accessible by an agent
description = 'this is car sales data';
Note that there are two types of skills: text-to-SQL and knowledge bases. Learn more about skills here.
Verify that the skill has been created successully using this command:
SHOW SKILLS;
Now that both the conversational model and the skill are ready, let's create an AI agent.
CREATE AGENT ai_agent
USING
model = 'conversational_model',
skills = ['text2sql_skill'];
Verify that the agent has been created successully using this command:
SHOW AGENTS;
At this point, you can query an agent to ask questions over the data.
SELECT question, answer
FROM ai_agent
WHERE question = 'how many cars were sold in 2016?';
Optionally, you can create a chatbot by connecitng an AI agent to a chat interface.
First connect a chat interface to MindsDB. Here the Slack connection is made.
CREATE DATABASE mindsdb_slack
WITH
ENGINE = 'slack',
PARAMETERS = {
"token": "xoxb-xxx",
"app_token": "xapp-xxx"
};
Follow the instructions on how to connect Slack to MindsDB for this use case.
Now create a chatbot providing the AI agent and the Slack connection.
CREATE CHATBOT ai_chatbot
USING
database = 'mindsdb_slack', -- connect a chat interface with CREATE DATABASE
agent = 'ai_agent'; -- create an agent with with CREATE AGENT
Verify that the chatbot is running using this command:
SHOW CHATBOTS;
Now you can go ahead and chat with the AI agent via Slack.