Skip to content

Instantly share code, notes, and snippets.

@thecatfix
Last active August 8, 2024 22:07
Show Gist options
  • Save thecatfix/57844436a6c94baa1bf83178c4316883 to your computer and use it in GitHub Desktop.
Save thecatfix/57844436a6c94baa1bf83178c4316883 to your computer and use it in GitHub Desktop.
The horrible idea of asking ChatGPT about custom gpts

How It Started (Simple Question to ChatGPT)

how do i build a custom GPT that will write book reviews for me in my voice. I would like to upload some previous reviews for the GPT to know what my voice sounds like

Answer

Building a custom GPT model that writes book reviews in your voice involves several steps, including collecting and preparing your existing reviews, fine-tuning a pre-trained language model, and deploying the model for use. Here's a step-by-step guide:

Step 1: Collect and Prepare Your Existing Reviews

  1. Gather Reviews: Collect all your previous book reviews. Ensure they are in a text format and organized in a way that highlights your writing style and voice.
  2. Format Data: Organize the reviews into a format suitable for training. A common format is a plain text file or a CSV file where each review is a separate entry.

Step 2: Fine-Tune a Pre-Trained Language Model

  1. Choose a Pre-Trained Model: Start with a pre-trained language model like GPT-3 from OpenAI. GPT-3 is powerful and can be fine-tuned to mimic specific writing styles.
  2. Set Up an Environment: You will need a coding environment with access to machine learning libraries (e.g., Python, PyTorch, TensorFlow) and the pre-trained model.
  3. Prepare the Training Data: Clean and preprocess your reviews. Tokenize the text if necessary and split it into training and validation sets.
  4. Fine-Tune the Model: Use the pre-trained model and fine-tune it with your reviews. This process involves adjusting the model weights based on your text to make it more reflective of your writing style.

Step 3: Training Process

  1. Install Dependencies: Ensure you have the necessary packages installed. You might use libraries such as transformers by Hugging Face, which makes working with GPT models easier.

    pip install transformers
    pip install torch
  2. Load the Pre-Trained Model:

    from transformers import GPT2LMHeadModel, GPT2Tokenizer
    
    model_name = "gpt2"
    model = GPT2LMHeadModel.from_pretrained(model_name)
    tokenizer = GPT2Tokenizer.from_pretrained(model_name)
  3. Prepare Your Dataset:

    from torch.utils.data import Dataset, DataLoader
    import torch
    
    class ReviewDataset(Dataset):
        def __init__(self, texts, tokenizer, max_length):
            self.texts = texts
            self.tokenizer = tokenizer
            self.max_length = max_length
    
        def __len__(self):
            return len(self.texts)
    
        def __getitem__(self, idx):
            text = self.texts[idx]
            inputs = self.tokenizer.encode_plus(
                text,
                max_length=self.max_length,
                truncation=True,
                padding="max_length",
                return_tensors="pt"
            )
            return {
                'input_ids': inputs['input_ids'].flatten(),
                'attention_mask': inputs['attention_mask'].flatten()
            }
    
    # Example data
    reviews = ["Your review text here...", "Another review..."]
    max_length = 512
    dataset = ReviewDataset(reviews, tokenizer, max_length)
    dataloader = DataLoader(dataset, batch_size=2, shuffle=True)
  4. Train the Model:

    from transformers import AdamW, get_linear_schedule_with_warmup
    
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    model = model.to(device)
    
    optimizer = AdamW(model.parameters(), lr=2e-5, correct_bias=False)
    total_steps = len(dataloader) * epochs
    
    scheduler = get_linear_schedule_with_warmup(
        optimizer,
        num_warmup_steps=0,
        num_training_steps=total_steps
    )
    
    for epoch in range(epochs):
        model.train()
        for batch in dataloader:
            input_ids = batch['input_ids'].to(device)
            attention_mask = batch['attention_mask'].to(device)
    
            outputs = model(
                input_ids=input_ids,
                attention_mask=attention_mask,
                labels=input_ids
            )
    
            loss = outputs.loss
            loss.backward()
    
            optimizer.step()
            scheduler.step()
            optimizer.zero_grad()

Step 4: Evaluate and Refine

  1. Evaluate: Test the model with new review prompts and compare the generated reviews with your style.
  2. Refine: Adjust the training process, hyperparameters, and data if necessary. Continue fine-tuning until the model outputs satisfactory reviews.

Step 5: Deploy the Model

  1. Save the Model: Once satisfied with the fine-tuning, save the model.

    model.save_pretrained("path_to_save_model")
    tokenizer.save_pretrained("path_to_save_tokenizer")
  2. Deploy: Use the model in your application. You can create a simple web interface or integrate it into a writing tool.

Example Usage

  1. Load and Generate:

    model = GPT2LMHeadModel.from_pretrained("path_to_save_model")
    tokenizer = GPT2Tokenizer.from_pretrained("path_to_save_tokenizer")
    
    prompt = "This book was an amazing journey from start to finish. The characters"
    inputs = tokenizer.encode(prompt, return_tensors="pt")
    outputs = model.generate(inputs, max_length=200, num_return_sequences=1)
    review = tokenizer.decode(outputs[0], skip_special_tokens=True)
    print(review)

Tips

  • Regularly Update: Continue to fine-tune the model with new reviews to keep it up-to-date with your evolving writing style.
  • Experiment with Parameters: Experiment with different parameters like learning rates, batch sizes, and model architectures to improve performance.
  • Ethical Considerations: Ensure the generated content aligns with ethical guidelines and accurately reflects your voice.

By following these steps, you can build a custom GPT model that writes book reviews in your unique voice.

Then I Had To Follow Up

I can't do that with Open AI custom GPTs?

FML

Yes, you can use OpenAI's fine-tuning capabilities to create a custom GPT model tailored to your writing style. This process is more straightforward than training a model from scratch and can be done through the OpenAI platform. Here’s how you can achieve this:

Step-by-Step Guide to Fine-Tuning with OpenAI Custom GPTs

  1. Sign Up for OpenAI API Access:

    • Ensure you have access to OpenAI's API. You may need to sign up for an API key if you don’t already have one.
  2. Prepare Your Data:

    • Collect your book reviews and format them appropriately. OpenAI expects data in a specific JSONL (JSON Lines) format where each line is a separate JSON object.
    • Example format for your reviews:
      {
        "prompt": "<|startoftext|>",
        "completion": "Your review text here<|endoftext|>"
      }
  3. Set Up Your Environment:

    • You can use tools like openai Python package to manage the fine-tuning process. Install the package if you haven’t already:
      pip install openai
  4. Upload Your Data:

    • Use the OpenAI API to upload your data for fine-tuning.

    • Example using Python:

      import openai
      openai.api_key = 'your-api-key'
      
      response = openai.File.create(
        file=open("path_to_your_file.jsonl"),
        purpose='fine-tune'
      )
  5. Fine-Tune the Model:

    • Once your data is uploaded, you can start the fine-tuning job.
    • Example:
      response = openai.FineTune.create(
        training_file="file-id-received-after-upload",
        model="davinci"  # or another suitable model like `curie`, `babbage`, or `ada`
      )
  6. Monitor the Fine-Tuning Process:

    • You can check the status of your fine-tuning job:
      response = openai.FineTune.list()
  7. Use Your Fine-Tuned Model:

    • Once the fine-tuning is complete, you can use the model for generating book reviews in your voice.
    • Example:
      response = openai.Completion.create(
        model="fine-tuned-model-name",
        prompt="This book was an amazing journey from start to finish. The characters",
        max_tokens=200
      )
      print(response.choices[0].text)

Tips for Fine-Tuning

  • Quality Data: Ensure your data is high quality and representative of your writing style.
  • Prompt Engineering: Carefully design the prompts to elicit the desired completions.
  • Iteration: You may need to iterate on the process, refining your data and fine-tuning parameters to get the best results.

Benefits of Using OpenAI's Custom GPTs

  • Ease of Use: The process is straightforward and doesn't require extensive machine learning expertise.
  • Infrastructure: OpenAI handles the heavy lifting of managing the infrastructure and scaling the model.
  • State-of-the-Art Models: You benefit from using some of the most advanced language models available.

By following these steps, you can create a custom GPT model that generates book reviews in your unique voice using OpenAI's platform. This method leverages OpenAI's fine-tuning capabilities, making it accessible and efficient.

pip install openai==0.28

import openai

openai.api_key = 'OPEN_API_KEY'

response = openai.File.create( file=open("/content/cleanedreviews.jsonl"), purpose='fine-tune' )

Print the response to get the file ID

print(response)

import openai

openai.api_key = 'OPEN_API_KEY'

response = openai.FineTuningJob.create( # Use FineTuningJob instead of FineTune
training_file="file-xMG5KSDmAQ42Rs1YKrG52cXv",
model="gpt-3.5-turbo-0613"
)

print(response)

response = openai.FineTuningJob.retrieve(id="ftjob-VGZTTkC84bm2AAM0HP72NFiC")
print(response)

import json

def validate_jsonl(file_path):
    with open(file_path, 'r', encoding='utf-8') as f:
        for line_number, line in enumerate(f, start=1):
            try:
                json.loads(line)
            except json.JSONDecodeError as e:
                print(f"Invalid JSON on line {line_number}: {e}")
                return False
    print("All lines are valid JSON.")
    return True

def clean_text(text): # Remove any non-UTF-8 characters or invalid tokens
    return text.encode('utf-8', 'ignore').decode('utf-8')

def clean_jsonl(input_path, output_path):
    with open(input_path, 'r', encoding='utf-8') as infile, open(output_path, 'w', encoding='utf-8') as outfile:
        for line in infile:
            data = json.loads(line)
            data['prompt'] = clean_text(data['prompt'])
            data['completion'] = clean_text(data['completion'])
            json.dump(data, outfile)
            outfile.write('\n')

input_file_path = '/content/cleanedreviews.jsonl'
output_file_path = '/content/reviews.jsonl'

if validate_jsonl(input_file_path):
    clean_jsonl(input_file_path, output_file_path)
    print(f"Cleaned file saved to {output_file_path}")
else:
    print("Please fix the JSON errors and try again.")

import json
import re

def clean_text(text): # Remove non-printable characters
    return re.sub(r'[^\x20-\x7E]+', ' ', text)

def clean_jsonl(input_path, output_path):
    with open(input_path, 'r', encoding='utf-8') as infile, open(output_path, 'w', encoding='utf-8') as outfile:
        for line in infile:
            data = json.loads(line)
            data['prompt'] = clean_text(data['prompt'])
            data['completion'] = clean_text(data['completion'])
            json.dump(data, outfile)
            outfile.write('\n')

input_file_path = '/content/validating.jsonl'
output_file_path = '/content/cleanvalidating.jsonl'

clean_jsonl(input_file_path, output_file_path)
output_file_path

It was so painful so I finally asked

Can't i use openai's site to do this?

And ChatGPT said YES

Yes, you can use OpenAI's website to upload your training file and manage fine-tuning jobs. Here are the steps to do it through the OpenAI platform:

Step-by-Step Guide

Step 1: Access the OpenAI Platform

  1. Go to the OpenAI Platform.

Step 2: Sign In

  1. Sign in with your OpenAI account credentials.

Step 3: Navigate to the Fine-Tuning Section

  1. Once you are logged in, navigate to the fine-tuning section:
    • Click on the "View API Documentation" link in the navigation bar.
    • In the documentation, find the section on "Fine-tuning" under the "API Reference".

Step 4: Upload Your Training File

  1. In the fine-tuning section, you will find the option to upload a training file.
    • Click on the "Upload a file" button.
    • Select your JSONL file (cleaned_reviews.jsonl) from your computer.

Step 5: Monitor the File Upload

  1. After uploading, you will receive a file ID. Note this file ID for the next steps.

Step 6: Create a Fine-Tuning Job

  1. Use the provided interface to create a new fine-tuning job:
    • Enter the file ID you received from the upload.
    • Select the model you want to fine-tune, for example, davinci-002.

Step 7: Monitor the Fine-Tuning Job

  1. After creating the job, monitor its progress on the OpenAI platform. The status will update as the job progresses.

Using the Web Interface

If you prefer to use the web interface rather than coding, follow these steps:

  1. Upload the Training File via the Web Interface:

    • Go to the "Files" section of the OpenAI platform.
    • Click on "Upload file" and select your JSONL file.
    • Once uploaded, note the file ID displayed.
  2. Create a Fine-Tuning Job via the Web Interface:

    • Navigate to the "Fine-tuning" section.
    • Click on "Create fine-tuning job".
    • Enter the file ID you noted earlier and select the model (e.g., davinci-002).
    • Configure any additional parameters as needed.
  3. Monitor the Job:

    • You can monitor the status of your fine-tuning job in the "Fine-tuning" section. The interface will show you the progress and status updates.

Example Screenshots (Hypothetical)

Here are some hypothetical screenshots to help guide you through the process:

  1. Files Section: Files Section

  2. Upload File: Upload File

  3. Fine-Tuning Section: Fine-Tuning Section

  4. Create Fine-Tuning Job: Create Fine-Tuning Job

By using the OpenAI platform's web interface, you can manage your files and fine-tuning jobs without needing to write code. If you need any further assistance or encounter any issues, feel free to ask!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment