|
| 1 | +"""Basic FireworksTrainer example: managed supervised fine-tuning on Fireworks AI. |
| 2 | +
|
| 3 | +This example demonstrates: |
| 4 | +1. Creating training data in chat format |
| 5 | +2. Configuring FireworksTrainer (base model + LoRA settings) |
| 6 | +3. Running a managed supervised fine-tuning (SFT) job |
| 7 | +4. Deploying the fine-tuned model and sampling from it |
| 8 | +
|
| 9 | +Fireworks runs fine-tuning as a managed, server-side job. Compared to the Tinker |
| 10 | +example, there is no client-side training loop: the dataset is uploaded, an SFT job |
| 11 | +is submitted, and Fireworks trains the model for you. |
| 12 | +
|
| 13 | +Notes: |
| 14 | +- Fireworks requires at least 3 training examples. |
| 15 | +- Fine-tuned LoRA models cannot be served serverlessly, so `sample()` provisions an |
| 16 | + on-demand deployment (which costs GPU time and takes a few minutes to spin up). |
| 17 | + Call `trainer.close()` (or use the trainer as a context manager) to tear it down |
| 18 | + when finished, so you don't leak a billable deployment. |
| 19 | +- Running real training requires a Fireworks account with training quota |
| 20 | + (Tier 2 / credits). Without the SDK or an API key, this runs in mock mode. |
| 21 | +
|
| 22 | +Usage: |
| 23 | + # Mock mode (no API key / SDK required) |
| 24 | + poetry run python examples/fireworks/basic_example.py |
| 25 | +
|
| 26 | + # Real fine-tuning (fireworks-ai is an optional add-on installed separately) |
| 27 | + pip install fireworks-ai |
| 28 | + export FIREWORKS_API_KEY="fw_..." |
| 29 | + poetry run python examples/fireworks/basic_example.py |
| 30 | +""" |
| 31 | + |
| 32 | +from bespokelabs.curator import FireworksTrainer, FireworksTrainerConfig |
| 33 | + |
| 34 | + |
| 35 | +def get_training_data() -> list[dict]: |
| 36 | + """Create sample chat training data with a distinctive, learnable style. |
| 37 | +
|
| 38 | + Each assistant answer ends with a fixed catchphrase. After fine-tuning, the |
| 39 | + model should reproduce that catchphrase on unseen questions. |
| 40 | + """ |
| 41 | + catchphrase = " Arrr, that's the bespoke truth, matey!" |
| 42 | + facts = [ |
| 43 | + ("What is the capital of France?", "The capital of France is Paris."), |
| 44 | + ("What is 2 + 2?", "2 + 2 equals 4."), |
| 45 | + ("Who wrote Romeo and Juliet?", "Romeo and Juliet was written by William Shakespeare."), |
| 46 | + ("What color is a clear daytime sky?", "A clear daytime sky is blue."), |
| 47 | + ("Name a popular programming language.", "Python is a popular programming language."), |
| 48 | + ("What is the largest planet in our solar system?", "Jupiter is the largest planet."), |
| 49 | + ("How many continents are there?", "There are seven continents on Earth."), |
| 50 | + ("What gas do plants absorb from the air?", "Plants absorb carbon dioxide."), |
| 51 | + ("What is the boiling point of water in Celsius?", "Water boils at 100 degrees Celsius at sea level."), |
| 52 | + ("Who painted the Mona Lisa?", "Leonardo da Vinci painted the Mona Lisa."), |
| 53 | + ] |
| 54 | + return [ |
| 55 | + { |
| 56 | + "messages": [ |
| 57 | + {"role": "user", "content": question}, |
| 58 | + {"role": "assistant", "content": answer + catchphrase}, |
| 59 | + ] |
| 60 | + } |
| 61 | + for question, answer in facts |
| 62 | + ] |
| 63 | + |
| 64 | + |
| 65 | +def main(): |
| 66 | + """Run the basic FireworksTrainer example.""" |
| 67 | + print("=" * 60) |
| 68 | + print("Basic FireworksTrainer Example") |
| 69 | + print("=" * 60) |
| 70 | + |
| 71 | + print("\n[Step 1] Preparing training data...") |
| 72 | + training_data = get_training_data() |
| 73 | + print(f" Training examples: {len(training_data)}") |
| 74 | + |
| 75 | + print("\n[Step 2] Configuring FireworksTrainer...") |
| 76 | + config = FireworksTrainerConfig( |
| 77 | + base_model="qwen3-4b", # smallest fine-tunable Fireworks model |
| 78 | + # Fireworks packs examples into batches by token count, so a SMALL dataset can |
| 79 | + # be a single batch (one optimizer step) per epoch. With few examples, use many |
| 80 | + # epochs so the LoRA gets enough gradient steps to actually learn. |
| 81 | + epochs=30, |
| 82 | + lora_rank=16, |
| 83 | + learning_rate=3e-4, # or omit to let Fireworks auto-select |
| 84 | + display_name="curator-basic-demo", |
| 85 | + output_model="curator-basic-demo", |
| 86 | + ) |
| 87 | + print(f" Base Model: {config.qualified_base_model}") |
| 88 | + print(f" Epochs: {config.epochs}") |
| 89 | + print(f" LoRA Rank: {config.lora_rank}") |
| 90 | + |
| 91 | + print("\n[Step 3] Running supervised fine-tuning...") |
| 92 | + trainer = FireworksTrainer(config) |
| 93 | + result = trainer.train(training_data) |
| 94 | + |
| 95 | + print("\n" + "=" * 60) |
| 96 | + print("Fine-tuning Complete!") |
| 97 | + print("=" * 60) |
| 98 | + print(f" Output Model: {result.weights_name}") |
| 99 | + print(f" Samples Processed: {result.samples_processed}") |
| 100 | + print(f" Total Time: {result.total_time:.2f}s") |
| 101 | + if result.loss_history: |
| 102 | + print(f" Final Loss: {result.final_loss:.4f}") |
| 103 | + print(f" Job: {result.metadata.get('job_url')}") |
| 104 | + |
| 105 | + print("\n[Step 4] Sampling from the fine-tuned model...") |
| 106 | + print(" (this deploys the model on-demand; first call may take a few minutes)") |
| 107 | + test_prompts = [ |
| 108 | + "What is the capital of Japan?", |
| 109 | + "Who discovered gravity?", |
| 110 | + ] |
| 111 | + for prompt in test_prompts: |
| 112 | + print(f"\n User: {prompt}") |
| 113 | + response = trainer.sample(prompt) |
| 114 | + print(f" Assistant: {response}") |
| 115 | + |
| 116 | + print("\n[Step 5] Cleaning up the deployment...") |
| 117 | + trainer.close() |
| 118 | + |
| 119 | + print("\n" + "=" * 60) |
| 120 | + print("Example Complete!") |
| 121 | + print("=" * 60) |
| 122 | + return trainer |
| 123 | + |
| 124 | + |
| 125 | +if __name__ == "__main__": |
| 126 | + trainer = main() |
0 commit comments