DEV Community

Cover image for Automate Fine-Tuning Your LLM Models: the ChatGPT Fine-Tuning SDK
Rodrigo Gomez Palacio
Rodrigo Gomez Palacio

Posted on

Automate Fine-Tuning Your LLM Models: the ChatGPT Fine-Tuning SDK

Fine-tuning a ChatGPT model can significantly improve its accuracy and relevance for your specific use case. However, manually preparing datasets for fine-tuning can be tedious. Introducing the ChatGPT Fine-Tuning SDK, an open-source package designed to streamline the process of generating fine-tuning datasets in JSONL format.

πŸš€ Why Use ChatGPT Fine-Tuning SDK?

This SDK simplifies dataset generation by wrapping around the chatgpt npm package. It provides a structured approach to curating training data while allowing programmatic approval, rejection, and correction of AI-generated responses.

When finished, you will be left with a jsonl file you can upload directly to fine-tune a model.

πŸ”§ Installation

You can install the package using npm or yarn:

# npm
npm install chatgpt-fine-tuning

# yarn
yarn add chatgpt-fine-tuning
Enter fullscreen mode Exit fullscreen mode

βš™οΈ Configuration

Before using the SDK, configure it with your OpenAI API key and define a system message:

import ChatGptFineTuning from 'chatgpt-fine-tuning';

const outFile = 'fine-tuning-output.jsonl'; // required
const systemMessage = 'Marv is a factual chatbot that is also sarcastic.'; // required

const gpt4Api = new ChatGptFineTuning({
    apiKey: process.env.GPT4_API_KEY || '', // required
    systemMessage,
}, outFile);
Enter fullscreen mode Exit fullscreen mode

πŸ›  Usage

Each sendMessage call returns a tuner object, which enables:

  • Approving the AI response
  • Rejecting an incorrect response
  • Fixing and correcting AI responses
  • Logging actions to maintain a record

Example Workflow

const tuner = await gpt4Api.sendMessage("What is the capital of France?");

// Programmatic verification
if (tuner.response.text.includes("Paris")) {
  tuner.approve();
} else {
  tuner.reject();
  tuner.fix("You did not provide the correct answer", "Paris");
}

tuner.log("Finished run");
Enter fullscreen mode Exit fullscreen mode

πŸ“– API Reference

tuner Methods

Method Parameters Return Type Description
approve() - Promise<void> Approves the current response for fine-tuning.
reject() - Promise<void> Rejects the response, marking it with a weight of 0.
fix(userText, assistantText, log?) string, string, boolean (optional) Promise<void> Submits a correction for the AI response with optional logging.
log(message) string void Logs a message to the output file.

ChatMessage Properties

Name Type Description
id string Unique identifier for the chat message.
text string Text content of the message.
role Role Role of the message sender (user, assistant, etc.).
parentMessageId string (optional) ID of the parent message in the conversation.
conversationId string (optional) ID of the conversation this message belongs to.

The API follows the same structure as the chatgpt npm package, ensuring familiarity if you've used it before.

🀝 Contributing

Contributions, bug reports, and feature requests are welcome! Check out the issues page to get involved.

⭐ Show Your Support

If this SDK has been helpful, consider giving it a ⭐ on GitHub!

πŸ“ License

This project is MIT licensed.


Get Started Now!

Fine-tune your ChatGPT models effortlessly with ChatGPT Fine-Tuning SDK and take AI customization to the next level!

API Trace View

How I Cut 22.3 Seconds Off an API Call with Sentry πŸ•’

Struggling with slow API calls? Dan Mindru walks through how he used Sentry's new Trace View feature to shave off 22.3 seconds from an API call.

Get a practical walkthrough of how to identify bottlenecks, split tasks into multiple parallel tasks, identify slow AI model calls, and more.

Read more β†’

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay