Agents enable conversation with data, including structured and unstructured data connected to MindsDB.

Create Agents

Here is the syntax for creating an agent:
agent = server.agents.create(
        'my_agent',
        model={
            'model_name': 'gpt-4o',
            'provider': 'openai',
            'api_key': 'sk-abc123',
            'base_url': 'http://example.com',
            'api_version': '2024-02-01'
        },
        data={
            'knowledge_bases': ['project_name.kb_name', ...]
            'tables': ['datasource_conn_name.table_name', ...]
        },
        prompt_template='describe data'
)
It creates an agent that uses the defined model and has access to the connected data. Here is how to list all available agents.
agents = server.agents.list()
print(agents)
The following sections explain all the agent parameters.

model

This parameter defines the underlying language model, including:
  • provider It is a required parameter. It defines the model provider from the list below.
  • model_name It is a required parameter. It defines the model name from the list below.
  • api_key It is an optional parameter (applicable to selected providers), which stores the API key to access the model. Users can provide it either in this api_key parameter, or using environment variables.
  • base_url It is an optional parameter (applicable to selected providers), which stores the base URL for accessing the model. It is the root URL used to send API requests.
  • api_version It is an optional parameter (applicable to selected providers), which defines the API version.
The available models and providers include the following.
Users can define the model for the agent choosing one of the following options. Option 1. Use the model parameter to define the specification.
        ...
        model={
            'model_name': 'gpt-4o',
            'provider': 'openai',
            'api_key': 'sk-abc123',
            'base_url': 'http://example.com',
            'api_version': '2024-02-01'
        },
        ...
Option 2. Define the default model in the MindsDB configuration file. If you define default_llm in the configuration file, you do not need to provide the model parameter when creating an agent. If provide both, then the values from the model parameter are used.
You can define the default models in the Settings of the MindsDB Editor GUI.
"default_llm": {

      "provider": "openai",
      "model_name" : "got-4o",
      "api_key": "sk-abc123",
      "base_url": "https://example.com/",
      "api_version": "2024-02-01"

}

data

This parameter stores data connected to the agent, including knowledge bases and data sources connected to MindsDB. The following parameters store the list of connected data.
  • knowledge_bases stores the list of knowledge bases to be used by the agent.
  • tables stores the list of tables from data sources connected to MindsDB.
Note that you can insert all tables from a connected data source and all knowledge bases from a project using the * syntax.
        ...
        data={
            'knowledge_bases': ['project_name.*', ...]
            'tables': ['datasource_conn_name.*', ...]
        },
        ...

prompt_template

This parameter stores instructions for the agent. It is recommended to provide data description of the data sources listed in the knowledge_bases and tables parameters to help the agent locate relevant data for answering questions.

timeout

This parameter defines the time the agent can take to come back with an answer. For example, when the timeout parameter is set to 10, the agent has 10 seconds to return an answer. If the agent takes longer than 10 seconds, it aborts the process and comes back with an answer indicating its failure to return an answer within the defined time interval.

Get Agents

You can get an existing agent with the get() method.
agent = server.agents.get('sales_agent')

Query Agents

Query an agent to generate responses to questions.
completion = agent.completion([{'question': 'What is the average number of orders per customers?', 'answer': None}])
print(completion.content)
Here is how to query agents with enabled streaming, allowing users to view agent’s thoughts when it is working on answering questions.
completion = agent.completion_stream([{'question': 'What is the average number of orders per customers?', 'answer': None}])
for chunk in completion:
    print(chunk)

Update Agents

Update existing agents with new data, model, or prompt.
agent.data['tables'].append('mysql_demo_db.car_sales')
updated_agent = server.agents.update('my_agent', agent)
print(updated_agent)

Delete Agents

Here is the syntax for deleting an agent:
server.agents.drop('my_agent')