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:

  1. Install MindsDB locally via Docker or Docker Desktop.
  2. To use OpenAI within MindsDB, install the required dependencies following this instruction.
  3. 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
      mode = 'mode_name', -- optional, ...
      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
      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

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:
    1. Make sure that your OpenAI account is active.
    2. Confirm that your API key is correct.
    3. Ensure that your API key has not been revoked.
    4. 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:
    1. 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