Technology

Fine-Tuning Large Language Models (LLMs) and Preparing the Right Data

Reading time: 15 min | May 21, 2024

Understanding Fine-Tuning

Everywhere you go and anything you read about fine-tuning Large Language Models (LLMs) emphasizes the importance of your dataset. It is universally acknowledged that the quality of your data is crucial for fine-tuning performance. However, how do you actually achieve this? What does it imply for your organization? In this article, we will take a deeper look into how to fine-tune and prepare data for models provided by OpenAI, Cohere, Anthropic, and Google.

Fine-tuning involves adjusting the parameters of a pre-trained model to suit a specific task. Instead of retraining the model from scratch on extensive datasets, fine-tuning modifies the model using targeted examples to improve its performance on specific tasks and domains. This process enhances feature extraction, enabling the model to perform tasks like sentiment analysis and document similarity with higher accuracy. If you want to get more details about the theory behind fine-tuning you can check out our in depth article on this topic here.

Benefits of Fine-Tuning

  1. Higher Quality Results: Fine-tuning can achieve better outcomes compared to prompting alone.
  2. Token Savings: Shorter prompts lead to cost savings.
  3. Lower Latency: Fine-tuned models process requests faster.

Key Steps in Fine-Tuning at a glance

  1. Prepare and Upload Training Data: Collect a diverse set of examples that reflect the tasks the model will perform.
  2. Train the Model: Adjust the model's parameters based on the training data.
  3. Evaluate Results: Assess the model's performance and iterate as necessary.
  4. Deploy the Model: Use the fine-tuned model for production tasks.

Prerequisites - Preparing the Right Data

Before diving into the fine-tuning process, it's essential to understand the basics of preparing the data on which the LLM will be trained. The principle of 'Garbage in, garbage out' highlights that the success of fine-tuning largely depends on the quality and relevance of the training data. Properly curated datasets are crucial for effectively fine-tuning LLMs.

What is a Dataset?

A dataset is a structured collection of data related to a specific topic, theme, or industry. It can include various types of information such as numbers, text, images, videos, and audio, stored in formats like CSV, JSON, or SQL. Datasets are fundamental for training models, as they provide the necessary examples from which the models learn.

Importance of Datasets

Datasets are pivotal for several reasons:

  • Training Models: They provide the raw material that models use to learn patterns and make predictions.
  • Validation and Testing: Datasets help validate the accuracy of a model and test its performance before deployment.
  • Continuous Improvement: High-quality datasets allow for the ongoing refinement and improvement of models.

Instruction Datasets

In the context of fine-tuning LLMs, an instruction dataset is a specific type of dataset used for instruction tuning. It consists of pairs of instructions and corresponding answers, guiding the model on how to respond to specific prompts. This is particularly useful for creating models that can perform complex tasks based on detailed instructions.

Here are some examples to illustrate how instruction datasets are structured and used:

Human Resources

Instruction: "Describe the steps to onboard a new employee."

Response: "1. Send the offer letter and contract for signature. 2. Set up the employee's workstation and IT access. 3. Schedule orientation sessions and introduce the employee to the team. 4. Provide necessary training and resources. 5. Complete HR paperwork, including tax forms and benefits enrollment."

Instruction: "Explain how to handle a workplace harassment complaint."

Response: "1. Listen to the employee's complaint without judgment. 2. Document the details of the complaint thoroughly. 3. Investigate the complaint by speaking to involved parties and reviewing evidence. 4. Take appropriate disciplinary action if necessary. 5. Follow up with the complainant to ensure resolution and prevent retaliation."

Finance

Instruction: "Outline the steps to create a quarterly financial report."

Response: "1. Gather financial data, including income statements, balance sheets, and cash flow statements. 2. Verify the accuracy of the data by reconciling accounts. 3. Analyze the data to identify trends and variances. 4. Compile the data into a comprehensive report, including charts and graphs. 5. Review the report with stakeholders and make necessary adjustments before finalizing."

Instruction: "Explain how to conduct a financial risk assessment."

Response: "1. Identify potential financial risks, such as market fluctuations or credit risks. 2. Evaluate the likelihood and impact of each risk. 3. Develop strategies to mitigate identified risks, such as diversification or hedging. 4. Monitor risk factors regularly and adjust strategies as needed. 5. Document the risk assessment process and findings for future reference."

Insurance

Instruction: "Describe the process for filing a home insurance claim."

Response: "1. Report the incident to your insurance company as soon as possible. 2. Document the damage with photos or videos and keep receipts for any related expenses. 3. Complete the claim form provided by your insurer. 4. Meet with the insurance adjuster to assess the damage. 5. Follow up with your insurer to track the claim status and receive your settlement."

Instruction: "Explain how to evaluate the coverage needs for a small business insurance policy."

Response: "1. Assess the risks specific to the business, such as property damage, liability, and employee injuries. 2. Determine the value of business assets and inventory. 3. Evaluate the business's financial stability and potential loss impact. 4. Consult with an insurance advisor to identify appropriate coverage options. 5. Review and update the policy regularly to ensure adequate protection as the business grows."

These examples show how instruction datasets provide clear, structured responses to specific prompts, enabling the fine-tuned model to deliver accurate and relevant information tailored to HR, Finance, and Insurance domains.

Types of Datasets

  • Numerical Datasets: These contain numerical data used for quantitative analysis. For example, a dataset of sales figures.
  • Text Datasets: Comprising text messages, documents, or posts. This can include customer service logs or social media comments.
  • Multimedia Datasets: Including images, videos, and audio files, used for tasks like image recognition or video analysis.
  • Time-Series Datasets: Collecting data over time to analyze trends. Examples include stock market data or temperature readings.
  • Spatial Datasets: Containing geographically referenced information like GPS data, useful for mapping and geolocation services.

Dataset Structure

  • Structured Datasets: Organized in specific formats (like tables) to facilitate easy querying and analysis. Examples include databases and spreadsheets.
  • Unstructured Datasets: Lack a well-defined schema and include various data types such as text and images found in raw form.
  • Hybrid Datasets: Combine structured and unstructured data, offering flexibility in data utilization. For instance, a dataset with both customer survey responses (structured) and review comments (unstructured).

Dataset Quality

Quality datasets are characterized by reliability, proper feature representation, and minimal skew. Reliable datasets ensure that the model's predictions are useful and accurate.

Ensuring Reliability
  • Avoid Label Errors: Human errors in labeling can compromise dataset reliability.
  • Filter Noisy Features: Ensure that data noise is minimized. “Noise” refers to irrelevant or random data that does not contribute to the model's learning. This can include typographical errors, irrelevant information, or inconsistencies in the data. While some noise is inevitable, excessive noise can confuse the model and degrade its performance. By filtering out these noisy features, you can improve the accuracy and reliability of the model.
  • Proper Data Filtering: Exclude irrelevant data that does not contribute to the model's objectives.

Feature Representation

Effective feature representation is about transforming raw data into a format that is useful for the model.

This process includes:

  1. Mapping Data to Features: Converting raw data into meaningful inputs that the model can use for making predictions. For example, turning a date of birth into an age value.
  2. Normalizing Numeric Values: Adjusting numbers so they fall within a similar range. This helps the model understand the data better and prevents it from being biased by large values.
  3. Handling Outliers: Identifying and managing unusual data points that can skew the model’s results. This might involve removing outliers or transforming them so they fit better with the rest of the data.

By properly representing features, you ensure the model can learn from the data more effectively and make more accurate predictions.

Minimizing Skew

Training and serving skew occurs when the data used to train the model is different from the data the model encounters in real-world applications. This can lead to inconsistencies in how the model performs. 

To minimize skew:

  1. Representative Training Set: Ensure that the data used for training the model closely matches the data it will process in real-world scenarios. This helps the model learn patterns that it will actually encounter during use.
  2. Consistent Features: Use only the features that will be available when the model makes predictions. This means not relying on data that won't be present during the model's deployment.

By aligning the training data with the serving data, you help the model maintain consistent performance and reliability in practical applications.

Examples of Good vs Bad Datasets

This table highlights the key differences between high-quality and low-quality datasets based on criteria like accuracy, diversity, balance, ethical sourcing, annotation, and contextual integrity.

Table 1: General Comparison Between Good vs. Bad Datasets

This table provides an in-depth comparison of good versus bad datasets, focusing on quality, diversity, balance, ethical considerations, annotation, and context preservation.

Table 2: Detailed Comparison Between Good vs. Bad Datasets

This table offers specific examples of good and bad datasets in the finance sector, illustrating the impact of dataset quality on financial analysis and decision-making.

Table 3: Good vs. Bad Datasets: Examples in Finance

Fine-Tuning an LLM Step-by-Step

With a solid understanding of data preparation, we can now begin a detailed look at the process of fine-tuning a large language model. This involves several key steps that are crucial for optimizing the model's performance.

Data Preparation

As discussed in the prerequisites, data preparation is the foundation of fine-tuning. It involves gathering and processing the appropriate dataset to train your model effectively.

  1. Data Collection: Gather a comprehensive dataset relevant to your specific use case. This might involve compiling customer service interactions or technical documents.
  2. Data Cleaning: Remove irrelevant details and ensure the dataset contains high-quality information. This step includes eliminating personal information and off-topic discussions.
  3. Annotation: Categorize the data if necessary to help the model understand different problem areas. For example, labeling customer service logs by issue type.
  4. Dataset Split: Divide the dataset into training, validation, and test sets (typically 80%, 10%, and 10% respectively) to support the model's learning and evaluation process.
  5. Preprocessing: Convert the text into a format the model can understand using tokenization, which breaks down the text into smaller units.

Training the Model

Starting the training process involves several technical steps, which we will not detail in this blog post. Instead, we will provide an overview of what this process looks like.

Once the data is prepared, the next step is training the model.

  1. Upload Training Data: Because we are talking about proprietary models and not open-source ones we will have to use the appropriate out-of-the-box APIs to upload the training data. Platforms like OpenAI and Cohere provide specific tools for this.
  2. Train the Model: Initiate the training process, adjusting parameters to suit your needs. This involves running the fine-tuning job on the platform of your choice.
  3. Evaluate Results: Assess the model’s performance and iterate as needed. Use validation sets and testing to refine the model further.

Evaluating and Iterating

After training, evaluation is crucial to ensure the model performs as expected.

  1. Generate Samples: Evaluate the model using a test set that represents real-world inputs.
  2. Compare Performance: Generate samples from both the base and fine-tuned models, comparing their performance.
  3. Adjust Training Dataset: Address any shortcomings identified during evaluation by refining the training dataset.

Building an Evaluation Framework for Fine-Tuned LLMs

An evaluation framework is a structured approach to assessing the performance of fine-tuned Large Language Models (LLMs). This framework ensures that the model meets specific performance criteria and can handle the tasks it was fine-tuned for effectively. Here’s an in-depth look at what an evaluation framework is, what it does, and how you can set one up.

What is an Evaluation Framework?

An evaluation framework for fine-tuned LLMs is a set of guidelines, metrics, and processes used to systematically evaluate the performance of a model. It provides a consistent and objective way to measure how well the model performs on specific tasks, helping to identify areas for improvement and ensuring that the model meets the desired quality standards.

Key Functions of an Evaluation Framework

  1. Performance Measurement: Quantifies how well the model performs on specific tasks using various metrics such as accuracy, precision, recall, F1 score, and loss.
  2. Quality Assurance: Ensures the model's outputs are reliable, accurate, and meet the predefined quality standards.
  3. Error Analysis: Identifies and categorizes the types of errors the model makes, providing insights into potential areas for improvement.
  4. Comparison: Enables comparison between different versions of the model (e.g., base model vs. fine-tuned model) to assess the impact of fine-tuning.
  5. Iterative Improvement: Facilitates an ongoing process of refinement by continuously evaluating and adjusting the model based on feedback and performance metrics.

How to create your own Evaluation Framework

Setting up an evaluation framework involves several steps to ensure comprehensive and effective assessment of your fine-tuned LLM.

1. Define Evaluation Metrics

Select appropriate metrics that align with your business objectives and the specific tasks the model will perform. 

Common metrics include:

  • Accuracy: The proportion of correct predictions out of all predictions made.
  • Precision: The proportion of true positive predictions out of all positive predictions made.
  • Recall: The proportion of true positive predictions out of all actual positive instances.
  • F1 Score: The harmonic mean of precision and recall, balancing both concerns.
  • Loss: A measure of the difference between the predicted values and the actual values, indicating the model's error rate.

2. Prepare Evaluation Datasets

Create datasets that reflect real-world scenarios the model will encounter. These datasets should be diverse and representative of the actual use cases. 

Split the datasets into:

  • Training Set: Used to fine-tune the model.
  • Validation Set: Used to tune the model parameters during training.
  • Test Set: Used to evaluate the final performance of the model after fine-tuning.

3. Establish Baseline Performance

Evaluate the performance of the base model (before fine-tuning) on the test set to establish baseline metrics. This provides a point of comparison to assess the improvements achieved through fine-tuning.

4. Implement Continuous Evaluation

Set up a process for continuous evaluation where the model’s performance is regularly assessed using the predefined metrics. This usually involves:

  • Automated Testing: Use scripts to automatically run evaluations on the test set.
  • Regular Monitoring: Continuously monitor the model’s performance in production to ensure it remains consistent.

5. Analyze Results and Iterate

Analyze the evaluation results to identify strengths and weaknesses in the model's performance and through this be able to:

  • Refine the Training Data: Adjust and improve the training dataset based on the types of errors identified.
  • Update the Model: Make necessary adjustments to the model's parameters and retrain if needed.
  • Repeat the Process: Continuously iterate on the model and evaluation process to achieve optimal performance.

Tools and Best Practices

  • Evaluation Libraries: Use tools like OpenAI’s Evals library, TensorFlow Extended (TFX), or scikit-learn to streamline the evaluation process.
  • Visualization Tools: Implement visualization tools like Weights and Biases (W&B) to track and visualize performance metrics.
  • Automated Pipelines: Set up automated evaluation pipelines to ensure consistent and repeatable assessments.

Example Workflow

  1. Collect Evaluation Data: Gather a diverse and representative dataset.
  2. Run Initial Evaluation: Use the evaluation metrics to assess the base model.
  3. Fine-Tune the Model: Apply fine-tuning techniques using the training dataset.
  4. Perform Continuous Evaluation: Regularly evaluate the fine-tuned model using automated scripts and monitoring tools.
  5. Analyze and Iterate: Review performance results, refine the model and data, and repeat the evaluation process.

By establishing a robust evaluation framework, you can ensure that your fine-tuned LLMs not only perform well on paper but also deliver reliable, high-quality results in real-world applications.

From Theory to Practice: Fine-Tuning Strategies Across Platforms

Having explored the theoretical foundations of data preparation and fine-tuning, it's time to move from theory to practice. Different platforms offer unique fine-tuning capabilities, each with its own methods and requirements. In this section, we will examine the fine-tuning approaches for models provided by OpenAI, Cohere, Anthropic, and Google. Understanding these platform-specific techniques will equip you with the knowledge needed to effectively fine-tune LLMs for your specific business needs.

Fine-Tuning with OpenAI

OpenAI provides robust fine-tuning capabilities for models like GPT-3.5-turbo and GPT-4. Here is a detailed look at the fine-tuning process with OpenAI:

  1. Training Data Preparation
    • Dataset Format: Each example in the dataset should be a conversation in the same format as the Chat Completions API, specifically a list of messages where each message has a role, content, and optional name.
    • Example Quality: Ensure that some of the training examples directly target cases where the model's performance needs improvement, providing ideal responses in the assistant messages.
    • Instructions: Include the set of instructions and prompts that worked best for the model in every training example. For example: You are a helpful assistant. Provide clear and concise answers. When asked for instructions or steps, list them out clearly. If the user asks for further clarification, provide additional details as needed. Always maintain a polite and professional tone.
  2. Upload Data
    • Files API: Use the Files API to upload your training data. The maximum file upload size is 1 GB, but typically you won’t need such large datasets to see improvements.
    • Processing Time: After uploading, the file will take some time to process before you can start a fine-tuning job.
  3. Fine-Tuning Process
    • Training Examples: At least 10 examples are required, but 50 to 100 well-crafted examples typically yield better results.
    • Token Limits: For GPT-3.5-turbo-0125, the maximum context length is 16,385 tokens. For GPT-3.5-turbo-0613, each training example is limited to 4,096 tokens.
    • Cost Estimation: Use the formula (base cost per 1k tokens) * (number of tokens in the input file) * (number of epochs trained) to estimate the cost. For instance, a training file with 100,000 tokens trained over 3 epochs costs approximately $2.40 USD.
  4. Monitoring and Evaluation
    • Initial Signal: Split your dataset into training and test portions to get statistics during the training.
    • Quality Checks: Generate samples from both the base and fine-tuned models on a test set to compare results.
    • Iterate: If the results are not satisfactory, scrutinize the training data for issues and collect more examples to target remaining problems.

Fine-Tuning with Cohere

Cohere's platform distinguishes itself from other fine-tuning services with its unique approach and specialized tools designed to streamline the fine-tuning process. Unlike other platforms, Cohere offers fine-tuning through a user-friendly dashboard, API, and Python SDK, making it accessible to users with varying levels of technical expertise.

Key Differences:

  1. Multi-Label Classification Support: Cohere allows fine-tuning for both single-label and multi-label classification tasks, providing flexibility in handling complex data scenarios.
  2. Flexible Data Handling: The platform supports diverse data formats and ensures proper handling of roles in conversational data, which is crucial for applications like chatbots.
  3. Detailed Evaluation Metrics: Cohere provides comprehensive metrics such as accuracy, precision, recall, and F1 score, helping users thoroughly evaluate model performance.
  4. Focus on Specific Use Cases: Cohere's fine-tuning processes are tailored to specific applications like chat, classification, and rerank tasks, ensuring that models are optimized for these particular functionalities.
  5. Ease of Use: The integration of fine-tuning capabilities within a user-friendly dashboard and the availability of API and SDK options make Cohere's platform highly accessible and easy to use, even for those without extensive technical backgrounds.

Here’s how to fine-tune Cohere models: 

  1. Chat Fine-Tuning
    • Data Requirements: Ensure proper roles (System, Chatbot, User) are defined, with at least one instance of Chatbot and User in each conversation.
    • Token Length: Maintain a maximum token length of 8192 tokens per turn in the conversation.
    • Preamble: Include a preamble in the System role, not exceeding 4096 tokens.
  2. Classification Fine-Tuning
    • Single-Label Data: Texts with a single label, e.g., "text: This movie offers entertainment and education, label: positive".
    • Multi-Label Data: Texts with multiple labels, in JSON format.
    • Data Cleaning: Remove unnecessary symbols, URLs, and HTML code. Ensure at least 40 examples, with a minimum of 5 examples per label.
  3. Rerank Fine-Tuning
    • Data Requirements: Include diverse queries and relevant passages, with at least 256 unique queries.
    • High Quality Test Set: Provide a separate test set to benchmark the model.
  4. Evaluation Metrics
    • Accuracy: Measure the correct predictions.
    • Precision and Recall: Evaluate the correctness and completeness.
    • F1 Score: Balance between precision and recall.

Fine-Tuning with Anthropic

Anthropic’s Claude model is pre-fine-tuned to be a helpful assistant. For organizations interested in exploring fine-tuning options, direct contact with Anthropic is recommended to discuss specific needs and potential solutions.

Fine-Tuning with Gemini (Google)

Google’s Gemini models support parameter-efficient and full fine-tuning through Vertex AI Studio or Google AI Studio (for prototyping purposes).

Here’s a comprehensive guide to fine-tuning Gemini models:

  1. Tuning Approaches:
    • Parameter-Efficient Tuning: Updates a small subset of model parameters, suitable for multi-task learning and resource-efficient adaptation.
    • Full Fine-Tuning: Adjusts all model parameters, ideal for highly complex tasks but requires more computational resources.
  2. Supervised Tuning Data:
    • Dataset Format: Use JSON Lines format, with role-content pairs. The maximum token length for context and messages combined is 32,768 tokens.
    • Training Dataset: Include at least 16 examples, ideally 100 to 500 for best results.
    • Validation Dataset: Up to 256 examples to measure tuning effectiveness.
  3. Maintaining Consistency:
    • Ensure the examples in your datasets match the expected production traffic. Consistency in formatting, keywords, and instructions is crucial for effective model behavior.
  4. Evaluation:
    • Accuracy: Measure correct predictions.
    • Precision and Recall: Assess correctness and completeness.
    • F1 Score: Balance precision and recall for a comprehensive performance metric.

Integrations and Automation

OpenAI integrates with tools like Weights and Biases (W&B) ,especially in the case of enterprises, for tracking fine-tuning jobs. These integrations help monitor metrics, hyperparameters, and other job-related information, facilitating efficient model management. Other platforms may offer similar integrations to enhance the tracking and management of fine-tuning processes.

Conclusion

Fine-tuning LLMs is a powerful strategy to tailor pre-trained models for specific business tasks. By preparing high-quality, diverse, and relevant data, organizations can significantly enhance the performance of their models. Careful evaluation and iterative improvements ensure that the fine-tuned models meet the desired objectives, providing better results and efficiency in various applications.

Understanding the nuances of fine-tuning and data preparation is essential for leveraging LLMs effectively. With platforms like OpenAI, Cohere, Anthropic, and Google offering sophisticated fine-tuning capabilities, businesses have the tools to optimize their AI models for specific needs, driving better outcomes and operational efficiencies.

Here’s more cool stuff

Read what's next.