LLM Fine-Tuning Made Easy: Customizing AI Models with MindsDB

Cover Image for LLM Fine-Tuning Made Easy: Customizing AI Models with MindsDB

Fine-tuning is a powerful technique used to customize AI models to specific use cases by leveraging new data that was not observed during the original training process. By fine-tuning large language models (LLMs) with specific datasets, we can effectively teach them to recognize patterns and come up with just the right answers. Fine-tuning allows us to adapt existing AI models, keeping them up-to-date and enhancing their performance over time.

The process of fine-tuning becomes more accessible and efficient with MindsDB, as it empowers users to fine-tune AI models seamlessly with a single SQL statement. This streamlined approach revolutionizes the way we tailor AI models, making customization more accessible and effective.

How to Fine-Tune LLMs with MindsDB

MindsDB provides its custom SQL syntax that may not be familiar to large language model users. In this example, we’ll demonstrate how to fine-tune an OpenAI model using a data table that stores information about MindsDB’s custom SQL statements.

Let’s start by creating an OpenAI model:

CREATE MODEL openai_davinci
PREDICT completion
USING
    engine = 'openai',
    model_name = 'davinci',
    prompt_template = 'Return a valid SQL string for the following question about MindsDB in-database machine learning: {{prompt}}';

Please note that OpenAI enables fine-tuning of specific models, including davinci, curie, babbage, and ada. For more details, visit OpenAI docs here.

Before we move on to fine-tuning our model, let’s see its answers without additional training.

SELECT prompt, completion
FROM openai_davinci as m
WHERE prompt = 'What is the SQL syntax to join input data with predictions from a MindsDB machine learning model?'
USING max_tokens=400;

Here is the output:

promptcompletion
What is the SQL syntax to join input data with predictions from a MindsDB machine learning model?The SQL syntax is: SELECT * FROM input_data INNER JOIN predictions ON input_data.id = predictions.id

The model provides an answer that is closer to standard SQL than to MindsDB SQL. So it’s time to fine-tune our model with the specific dataset.

Let’s look at a table that will be used to fine-tune the model:

SELECT prompt, completion
FROM mindsdb_custom_syntax;

Please note that we require the prompt and completion columns - the prompt column stores a message to the model and the completion column stores an expected answer.

promptcompletion
What is the SQL syntax to connect a database to MindsDB?CREATE DATABASE datasource_name
What is the SQL command to create a home rentals MindsDB machine learning model?CREATE MODEL mindsdb.home_rentals_modelFROM example_db (SELECT * FROM demo_data.home_rentals)PREDICT rental_price;
What is the SQL syntax to join input data with predictions from a MindsDB machine learning model?SELECT t.column_name, p.column_name, ...FROM integration_name.table_name [AS] tJOIN project_name.model_name [AS] p;

Here is how to fine-tune the model with a single SQL statement:

FINETUNE openai_davinci
FROM db
    (SELECT prompt, completion FROM mindsdb_custom_syntax);

Please note that fine-tuning is a time-consuming process and may take up to a few hours to complete for OpenAI models. However, there are models that may be fine-tunable in shorter time periods.

Once the fine-tuning is complete, we can query the model again:

SELECT prompt, completion
FROM openai_davinci as m
WHERE prompt = 'What is the SQL syntax to join input data with predictions from a MindsDB machine learning model?'
USING max_tokens=400;

Here is the output:

promptcompletion
What is the SQL syntax to join input data with predictions from a MindsDB machine learning model?SELECT * FROM mindsdb.models.my_model JOIN mindsdb.input_data_name;

Now you see the answer is different. The model became aware of the custom JOIN statement offered by MindsDB to join models with data tables and get predictions.

Visit our FINETUNE doc page to see more examples of customizing and enhancing your AI models.

What’s Next

Fine-tuning with MindsDB simplifies the customization of AI models to suit specific use cases. Users can easily fine-tune existing AI models, tailoring them precisely to their unique requirements.

To further explore MindsDB, you can create a demo account on MindsDB Cloud that grants access to all the features offered by MindsDB without any installation hassle. And for a more hands-on and customizable experience, you have the option to install MindsDB locally using pip or Docker and have enhanced control over the back end, enabling you to modify and debug the code according to your specific needs.

Check us out on GitHub to see how you can contribute. Love what we’re doing? Give us a high five and star our repo.