Article last updated on:
October 25, 2023

Tired of the same-looking answers ChatGPT gives you?

In this guide, we’ll teach you:

  • How to train ChatGPT with your own data
  • Make it sound like you
  • Give custom answers — according to your preference

Training ChatGPT is 100% free.

However, light coding is required.

Very relevant topic: How to give ChatGPT custom instructions.

For now, we’ll focus on training ChatGPT with data, which is very different than giving instructions to the AI.

How to train your own ChatGPT model

Yes, you can train your own ChatGPT model with custom data.

1. Install Python

You first need to install Python. Follow this process:

  1. Go to the Phyton.org website
  2. Select Download
  3. Choose your Operating System
  4. Download & Install the app.
Downloading Python from the official website

Then, install the essential libraries for your custom ChatGPT model:

Why install these? → They develop an LLM that accesses your data and trains the bot.

LLM = Lifelong Learning Machine.

2. Open a Code Editor

Now you need to install a code editor.

If you already have one, simply open it.

Sublime Text - Code Editor For Mac

Here are a few examples:

For this example, we’ve used Sublime Text on a Mac.

3. Copy your API Key

You need to create an API Key in your OpenAI-ChatGPT account.

Here’s how to:

  1. Go to your OpenAI API Keys — it takes you to settings
  2. Log in — if you’re not already, you will be asked to
  3. Click + Create a new secret key
  4. Optional: Name your API key
Creating a new API Key in OpenAI

After creating your API key, write it down somewhere 100% private.

Make sure you’re the only one to ever see it.

How to keep your API key secure:

  • Never type your key on the internet
  • Don’t give your API key to anybody
  • Make sure you store it in a private/secure document

4. Select a GPT Model

Select which ChatGPT model you want for your custom-made AI chatbot.

Find all of ChatGPT’s models here.

List of the current ChatGPT Models

The smarter the model, the more credits you need to pay for.

For example, GPT-3.5 will be cheaper to use than GPT-4.

5. Create a new Script

Now that you’ve selected your GPT model, you can use our free script below:

  1. Copy the code below
  2. Open your code editor
  3. Replace “Insert API Key” with your own key
  4. Save the file as “app.py” in the “docs” folder
from gpt_lib import CustomDirectoryLoader, GPTQueryIndex, GPTVectorIndex, LanguageModelInfer, QueryAssistant
from chain_language import OpenAIApi
import gradio as grd
import sys
import os

os.environ["API_KEY_OPENAI"] = 'Insert API Key'

def create_index(folder_path):
    max_query_size = 4096
    output_dims = 512
    overlap_margin = 20
    max_segment_size = 600

    query_assistant = QueryAssistant(max_query_size, output_dims, overlap_margin, segment_limit=max_segment_size)

    lang_model = LanguageModelInfer(temperature=0.7, model_type="davinci-text-003", token_limit=output_dims)

    doc_data = CustomDirectoryLoader(folder_path).get_documents()

    query_index = GPTVectorIndex(doc_data, model_predictor=lang_model, query_helper=query_assistant)

    query_index.save('index_data.json')

    return query_index

def my_chatbot(query_text):
    query_index = GPTVectorIndex.load('index_data.json')
    bot_response = query_index.search(query_text, mode="compact")
    return bot_response.answer

gui = grd.Interface(fn=my_chatbot,
                    inputs=grd.inputs.Textbox(lines=7, label="Input your text"),
                    outputs="text",
                    live_title="Custom Chatbot")

gpt_index = create_index("document_folder")
gui.launch(public=True)

After executing the code in the Terminal and the .JSON file is generated..

A local URL will pop up:

  • Copy that URL.
  • Paste it into your web browser.
  • You’re now accessing your custom ChatGPT bot.

Have fun asking questions!

Conclusion

That’s all you need to know — a simple and free way to train ChatGPT.

You’ve just learned how to create a custom AI bot based on ChatGPT.

Learn how to become more productive with our guides on how to use AI.

Thank you for reading this,

Ch David and Daniel

About the author

David, the head editor at Guides.ai, has four years of experience in Artificial Intelligence and Machine Learning. Join David and the team and explore AI tools and contributing to the creation and curation of AI educational content.