Create, Train, and Deploy a Model

The models.get() and models.create() methods enable you to get an existing model or create and deploy a new model.

Syntax

Use the models.get() method to get an existing model:

 Server server = MindsDB.connect();
 Project mindsdb = server.getProject();
 Model llama3 = mindsdb.models.getModel("llama3");

Or, the create() method to create and train a new model:

create() Method signature:

/**
 * Create a new model
 * @param name              - name of the model
 * @param predict           - predict column
 * @param engine            - engine name
 * @param query             - query for the model
 * @param database          - database name
 * @param options           - options for the model
 * @param timeseriesOptions - timeseries options for the model
 * @return Model object
 */
 public Model create(
            String name,
            String predict,
            String engine,
            String query,
            String database,
            Map<String, Object> options,
            Map<String, Object> timeseriesOptions);
Project mindsdb = server.getProject("mindsdb");
Model model = mindsdb.models.create("model_name", "target_column", null, "SELECT * FROM data_source.table_name", null, null); 

Please note that in the case of LLM models, the parameters can be stored in options. Here is the syntax to create an OpenAI and Llama3.2 model using the Ollama and Langchain:

Project mindsdb = server.getProject("mindsdb");

// Llama3 model
Model llama3 = mindsdb.models.create(
    "llama", 
    "answer", 
    "langchain_engine", 
    null, 
    null,
    Map.of(
        "provider", "ollama",
        "model_name", "llama3.2",
        "mode", "conversational",
        "user_column", "question",
        "assistant_column", "answer",
        "prompt_template", "Answer the users input in a helpful way: {{question}}" 
    ), 
    null
);


// OpenAI model
Model sentimentClassifier = mindsdb.models.create(
    "sentiment_classifier", 
    "sentiment", 
    "openai", 
    null, 
    null,
    Map.of(
        "prompt_template", "answer this question: {{questions}}",
        "model_name", "gpt4"
    ), 
    null
);

And in the case of time-series model, the additional options are stored in timeseriesOptions. Here is the syntax to create a time-series model:

Server server = MindsDB.connect();
Project mindsdb = server.getProject("mindsdb");

Model timeSeriesModel = mindsdb.getModels().create(
    "house_sales_model",
    "ma",
    null,
    "SELECT * FROM house_sales",
    "example_db",
    null,
    Map.of(
        "order", "saledate",
        "group", List.of("bedrooms", "type"),
        "window", "8",
        "horizon", "4"
    )
);
Updated on