OpenAI
This documentation describes the integration of MindsDB with OpenAI, an AI research organization known for developing AI models like GPT-3 and GPT-4. The integration allows for the deployment of OpenAI models within MindsDB, providing the models with access to data from various data sources.
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.
CREATE ML_ENGINE openai_engine
FROM openai
USING
openai_api_key = 'api-key-value';
Create a model using openai_engine
as an engine.
CREATE MODEL openai_model
PREDICT target_column
USING
engine = 'openai_engine', -- engine name as created via CREATE ML_ENGINE
api_base = 'base-url', -- optional, replaces the default base URL
mode = 'mode_name', -- optional, mode to run the model in
model_name = 'openai_model_name', -- optional with default value of gpt-3.5-turbo
question_column = 'question', -- optional, column name that stores user input
context_column = 'context', -- optional, column that stores context of the user input
prompt_template = 'input message to the model here', -- optional, user provides instructions to the model here
user_column = 'user_input', -- optional, stores user input
assistant_column = 'conversation_context', -- optional, stores conversation context
prompt = 'instruction to the model', -- optional stores instruction to the model
max_tokens = 100, -- optional, token limit for answer
temperature = 0.3, -- temp
json_struct = {
'key': 'value',
...
}'
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:
SELECT question, answer
FROM openai_model
WHERE question = 'input question here'
USING prompt_template = 'input new message to the model here';
The following parameters are available to use when creating an OpenAI model:
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.
The following usage examples utilize openai_engine
to create a model with the CREATE MODEL
statement.
Next Steps
Follow 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:
SELECT input.text, output.sentiment FROM integration.travel data AS input JOIN openai_engine AS output
- Incorrect:
SELECT input.text, output.sentiment FROM integration.'travel data' AS input JOIN openai_engine AS output
- Correct:
SELECT input.text, output.sentiment FROM integration.`travel data` AS input JOIN openai_engine AS output
- Incorrect:
- Ensure table names with spaces or special characters are enclosed in backticks.
Examples:
Was this page helpful?