Prerequisites
Before proceeding, ensure the following prerequisites are met:- Install MindsDB locally via Docker or Docker Desktop.
- To use OpenAI within MindsDB, install the required dependencies following this instruction.
- Obtain the OpenAI API key required to deploy and use OpenAI models within MindsDB. Follow the instructions for obtaining the API key.
Setup
Create an AI engine from the OpenAI handler.openai_engine
as an engine.
If you want to update the
prompt_template
parameter, you do not have to recreate the model. Instead, you can override the prompt_template
parameter at prediction time like this:engine
engine
This is the engine name as created with the
CREATE ML_ENGINE
statement.api_base
api_base
This parameter is optional.It replaces the default OpenAI’s base URL with the defined value.
mode
mode
This parameter is optional.The available modes include
default
, conversational
, conversational-full
, image
, and embedding
.- The
default
mode is used by default. The model replies to theprompt_template
message. - The
conversational
mode enables the model to read and reply to multiple messages. - The
conversational-full
mode enables the model to read and reply to multiple messages, one reply per message. - The
image
mode is used to create an image instead of a text reply. - The
embedding
mode enables the model to return output in the form of embeddings.
model_name
model_name
This parameter is optional. By default, the
gpt-3.5-turbo
model is used.You can find all available models here.question_column
question_column
This parameter is optional. It contains the column name that stores user input.
context_column
context_column
This parameter is optional. It contains the column name that stores context for the user input.
prompt_template
prompt_template
This parameter is optional if you use
question_column
. It stores the message or instructions to the model. Please note that this parameter can be overridden at prediction time.max_tokens
max_tokens
This parameter is optional. It defines the maximum token cost of the prediction. Please note that this parameter can be overridden at prediction time.
temperature
temperature
This parameter is optional. It defines how risky the answers are. The value of
0
marks a well-defined answer, and the value of 0.9
marks a more creative answer. Please note that this parameter can be overridden at prediction time.json_struct
json_struct
This parameter is optional. It is used to extract JSON data from a text column provided in the
prompt_template
parameter. See examples here.Usage with OpenAI-Compatible APIs
The OpenAI handler can be used with any OpenAI-compatible APIs by providing theapi_base
parameter that stores the base URL of the OpenAI-compatible APIs.
Here is an example of how to use the OpenAI handler with OpenRouter, the OpenAI-compatible interface for accessing LLMs.
api_base
parameter and the API key in the openai_api_key
parameter.
Usage
Here are the combination of parameters for creating a model:- Provide a
prompt_template
alone. - Provide a
question_column
and optionally acontext_column
. - Provide a
prompt
,user_column
, andassistant_column
to create a model in the conversational mode.
openai_engine
to create a model with the CREATE MODEL
statement.
Answering questions without context
Answering questions without context
Here is how to create a model that answers questions without context.Query the model to get predictions.Here is the output:
Answering questions with context
Answering questions with context
Here is how to create a model that answers questions with context.Query the model to get predictions.On execution, we get:
Prompt completion
Prompt completion
Here is how to create a model that offers the most flexible mode of operation. It answers any query provided in the Let’s look at an example that reuses the On execution, we get:
prompt_template
parameter.Good prompts are the key to getting great completions out of large language models like the ones that OpenAI offers. For best performance, we recommend you read their prompting guide before trying your hand at prompt templating.
openai_model
model created earlier and overrides parameters at prediction time.Conversational mode
Conversational mode
Here is how to create a model in the conversational mode.And here is how to query this model:
Next StepsFollow this tutorial on sentiment analysis and this tutorial on finetuning OpenAI models to see more use case examples.
Troubleshooting Guide
Authentication Error
- Symptoms: Failure to authenticate to the OpenAI API.
- Checklist:
- Make sure that your OpenAI account is active.
- Confirm that your API key is correct.
- Ensure that your API key has not been revoked.
- Ensure that you have not exceeded the API usage or rate limit.
SQL statement cannot be parsed by mindsdb_sql
- Symptoms: SQL queries failing or not recognizing table and model names containing spaces or special characters.
- Checklist:
- Ensure table names with spaces or special characters are enclosed in backticks.
Examples:
- Incorrect:
- Incorrect:
- Correct:
- Incorrect:
- Ensure table names with spaces or special characters are enclosed in backticks.
Examples: