Skip to main content
You can also check this cookbook in colab here To run this, press “Runtime” and press “Run all” on a free Tesla T4 Google Colab instance!
CAMEL HomepageJoin Discord
Star us on GitHub, join our Discord, or follow us on X
This notebook demonstrates how to set up and leverage CAMEL’s CoTDataGenerator for generating high-quality question-answer pairs like o1 thinking data, fine-tuning a language model using Unsloth, and uploading the results to Hugging Face. In this notebook, you’ll explore:
  • CAMEL: A powerful multi-agent framework that enables SFT data generation and multi-agent role-playing scenarios, allowing for sophisticated AI-driven tasks.
  • CoTDataGenerator: A tool for generating like o1 thinking data.
  • Unsloth: An efficient library for fine-tuning large language models with LoRA (Low-Rank Adaptation) and other optimization techniques.
  • Hugging Face Integration: Uploading datasets and fine-tuned models to the Hugging Face platform for sharing.
Slide 16_9 - 25.png

📦 Installation

Unsloth require GPU environment, To install Unsloth on your own computer, follow the installation instructions here.

🔑 Setting Up API Keys

First we will set the OPENAI_API_KEY that will be used to generate the data.
Alternatively, if running on Colab, you could save your API keys and tokens as Colab Secrets, and use them across notebooks. To do so, comment out the above manual API key prompt code block(s), and uncomment the following codeblock. ⚠️ Don’t forget granting access to the API key you would be using to the current notebook.

Set ChatAgent

Create a system message to define agent’s default role and behaviors.
Use ModelFactory to set up the backend model for agent CAMEL supports many other models. See here for a list.

Load Q&A data from a JSON file

please prepare the qa data like below in json file:

The script fetches a example JSON file containing question-answer pairs from a GitHub repository and saves it locally. The JSON file is then loaded into the qa_data variable.

Create an instance of CoTDataGenerator

Test Q&A

The script iterates through the questions, generates answers, and verifies their correctness. The generated answers are stored in a dictionary

Export the generated answers to a JSON file and transform these to Alpaca traing data format

The script transforms the Q&A data into the Alpaca training data format, which is suitable for supervised fine-tuning (SFT). The transformed data is saved to a new JSON file.

Upload the Data to Huggingface

This defines a function upload_to_huggingface that uploads a dataset to Hugging Face. The script is modular, with helper functions handling specific tasks such as dataset name generation, dataset creation, metadata card creation, and record addition

Config Access Token of Huggingface

You can go to here to get API Key from Huggingface image.png

final example preview

image.png

Configure the Unsloth environment

choose the base model

We now add LoRA adapters so we only need to update 1 to 10% of all parameters!

Convert CoT data into an SFT-compliant training data format

Train the model

Now let’s use Huggingface TRL’s SFTTrainer! More docs here: TRL SFT docs. We do 60 steps to speed things up, but you can set num_train_epochs=1 for a full run, and turn off max_steps=None. We also support TRL’s DPOTrainer!

Start model training

Inference

Let’s run the model! You can change the instruction and input - leave the output blank!
Here are the results of the official Qwen2.5-1.5b-instruct demo answering the same question:Qwen2.5-1.5b-instruct-demo image.png

Saving, loading finetuned models

To save the final model as LoRA adapters, either use Huggingface’s push_to_hub for an online save or save_pretrained for a local save. [NOTE] This ONLY saves the LoRA adapters, and not the full model.
Now if you want to load the LoRA adapters we just saved for inference, set False to True:

🌟 Highlights

Through this notebook demonstration, we showcased how to use the CoTDataGenerator from the CAMEL framework to generate high-quality question-answer data and efficiently fine-tune language models using the Unsloth library. The entire process covers the end-to-end workflow from data generation, model fine-tuning, to model deployment, demonstrating how to leverage modern AI tools and platforms to build and optimize question-answering systems.

Key Takeaways:

Data Generation: Using CoTDataGenerator from CAMEL, we were able to generate high-quality question-answer data similar to o1 thinking. This data can be used for training and evaluating question-answering systems. Model Fine-Tuning: With the Unsloth library, we were able to fine-tune large language models with minimal computational resources. By leveraging LoRA (Low-Rank Adaptation) technology, we only needed to update a small portion of the model parameters, significantly reducing the resources required for training. Data and Model Upload: We uploaded the generated data and fine-tuned models to the Hugging Face platform for easy sharing and deployment. Hugging Face provides powerful dataset management and model hosting capabilities, making the entire process more efficient and convenient. Inference and Deployment: After fine-tuning the model, we used it for inference to generate high-quality answers. By saving and loading LoRA adapters, we can easily deploy and use the fine-tuned model in different environments. That’s everything: Got questions about 🐫 CAMEL-AI? Join us on Discord! Whether you want to share feedback, explore the latest in multi-agent systems, get support, or connect with others on exciting projects, we’d love to have you in the community! 🤝 Check out some of our other work:
  1. 🐫 Creating Your First CAMEL Agent free Colab
  2. Graph RAG Cookbook free Colab
  3. 🧑‍⚖️ Create A Hackathon Judge Committee with Workforce free Colab
  4. 🔥 3 ways to ingest data from websites with Firecrawl & CAMEL free Colab
  5. 🦥 Agentic SFT Data Generation with CAMEL and Mistral Models, Fine-Tuned with Unsloth free Colab
Thanks from everyone at 🐫 CAMEL-AI
CAMEL HomepageJoin Discord
Star us on GitHub, join our Discord, or follow us on X