AI Agents with LLMs and Skills

With MindsDB, you can create and deploy AI agents that comprise AI models and customizable skills such as knowledge bases and text-to-SQL.

AI agents comprise of skills, such as text2sql and knowledge_base, and a conversational model.

  • Skills provide data resources to an agent, enabling it to answer questions about available data. Learn more about skills here. Learn more about knowledge bases here.

  • A conversational model (like OpenAI) from LangChain utilizes tools as skills to respond to user input. Users can customize these models with their own prompts to fit their use cases.

Syntax and Example

Creating an agent

Agent testAgent = server.agents.create("test_agent");

Or create agent with model and prompt_template

Agent sarcasticAgent = server.agents.create("sarcastic_agent", "llama3", null, null, Map.of(
    "prompt_template", "Answer user's question sarcastically {{question}}"
));

Or use an existing model:

Agent sarcasticAgent = server.agents.get("sarcastic_agent");

Furthermore, you can list all existing agents, get agents by name, update agents, and delete agents.

package mindsdb;

import java.util.List;
import java.util.Map;

import mindsdb.models.Model;
import mindsdb.models.agent.Agent;
import mindsdb.models.skill.Skill;
import mindsdb.services.Agents;
import mindsdb.services.Models;
import mindsdb.services.Server;
import mindsdb.services.Skills;

public class Main {
    public static void main(String[] args) {
        Server server = MindsDB.connect();
        Agents agents = server.agents;
        Models models = server.getModels();
        Skills skills = server.getSkills();

        // List all agents
        List<Agent> agentList = agents.list();

        // Get an agent by name
        Agent agent = agents.get("my_agent");

        // Update an agent
        Model newModel = models.getModel("new_model");
        agent.setModelName(newModel.getName());
        Skill newSkill = skills.create("new_skill", "sql", Map.of("tables", List.of("new_table"), "database", "new_database"));
        agent.getSkills().add(newSkill);
        agents.update("my_agent", agent);

        // Delete an agent by name
        agents.drop("my_agent");
    } 
    
}

Assigning skills to an agent

You can add skills to an agent, providing it with data stored in databases, files, or webpages.

The retrieval skill is similar to knowledge bases.

/**
 * Add a file to the agent.
 *
 * @param filePath - file path
 * @param description - description of the file
 * @param knowledgebase - knowledgebase
 */
 public void addFile(String filePath, String description, String knowledgebase);

/**
 * Add files to an agent
 * 
 * @param name          - Name of the agent
 * @param filePaths     - List of file paths
 * @param description   - Description of the files
 * @param knowledgeBase - Name of the knowledge base
 */
 public void addFiles(List<String> filePaths, String description, String knowledgeBase)
 // Example

myAgent.addFile("file_path", "file description", null or "kb name");
myAgent.addFiles(List.of("file1", "file2", ...), "Files description", null or "kb name);

The text2SQL skill retrieves relevant information from databases.

Database db = server.databases.get("datasource_name");
yourAgent.addDatabase(db.getName(), List.of("table_name"), "data description");

Querying an agent

Once you created an agent and assigned it a set of skills, you can ask questions related to your data.

/**
 * Generates a completion for the given list of messages.
 *
 * @param messages A list of messages, where each message is a map of key-value pairs.
 * @return An AgentCompletion object containing the result of the
 * completion.
 */
 public AgentCompletion completion(List<Map<String, String>> messages);



/**
 * Generates a completion for the given message.
 *
 * @param input - input message
 * @param inputColumnName - input column name
 * @return An AgentCompletion object containing the result of the
 * completion.
 */
 public AgentCompletion completion(String input, String inputColumnName);

Example

Server server = MindsDB.connect();
Agent yourAgent = server.agents.get("your_agent");

String answer = yourAgent.completion(List.of(Map.of("question", "what is an AI agent?")));

// Or

String answer = yourAgent.completion("What is an AI agent?","question").getContent();

Here question is the input field required by the model used in AI Agent.

Here is a sample Java code to deploy an agent:

package mindsdb;

import java.util.List;
import java.util.Map;
import java.util.Scanner;

import mindsdb.models.agent.Agent;
import mindsdb.services.Server;

public class Main {

    public static void main(String[] args) {
        Server server = MindsDB.connect();
        
        // Create a new agent
        Agent agent = server.agents.create("new_demo_agent");

        System.out.println("Adding Hooblyblob details...");
        agent.addFile("hooblyblob.txt", "Details about the company Hooblyblob", null);

        System.out.println("Adding rulebook details...");
        agent.addFiles(List.of("codenames-rulebook.pdf"), "Rulebooks for various board games", null);

        System.out.println("Adding MindsDB docs...");
        agent.addWebPages(List.of("https://docs.mindsdb.com"), "Documentation for MindsDB", null, 1, null);

        System.out.println("Agent ready to use.");
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.print("Ask a question: ");
            String question = scanner.nextLine();
            String answer = agent.completion(List.of(Map.of("question", question, "answer", null))).getContent();
            System.out.println(answer);
        }
    }
}
Updated on