### Fast Experimentation Example Source: https://docs.fireworks.ai/fine-tuning/cli-reference A quick setup for experimentation using a small model and a single epoch. ```bash eval-protocol create rft \ --base-model accounts/fireworks/models/qwen3-0p6b \ --output-model quick-test ``` -------------------------------- ### Quickstart SFT Recipe Source: https://docs.fireworks.ai/fine-tuning/training-api/cookbook/overview A basic example demonstrating how to configure and run a Supervised Fine-Tuning (SFT) recipe. It shows the necessary imports, configuration parameters, and the main function call. ```python from training.recipes.sft_loop import Config, main from training.utils import TrainerConfig cfg = Config( log_path="./sft_quickstart", base_model="accounts/fireworks/models/qwen3-8b", dataset="/path/to/training_data.jsonl", tokenizer_model="Qwen/Qwen3-8B", max_seq_len=4096, epochs=1, batch_size=4, trainer=TrainerConfig( training_shape_id="accounts/fireworks/trainingShapes/qwen3-8b-128k-h200", ), ) main(cfg) ``` -------------------------------- ### Setup and Load Example Data for Text-to-SQL Test Source: https://docs.fireworks.ai/examples/text-to-sql This Python script sets up the necessary environment variables, initializes the LLM, and loads a specific example from a JSONL dataset for testing Text-to-SQL generation. Ensure your FIREWORKS_API_KEY is set and MCP_SERVER_URL is configured. ```python import requests import json import os from fireworks import LLM # --- 1. SETUP: Define API keys, server URLs, and the model to use --- # IMPORTANT: Make sure your FIREWORKS_API_KEY is set as an environment variable. # You can get one from https://fireworks.ai if "FIREWORKS_API_KEY" not in os.environ: print("FATAL: FIREWORKS_API_KEY environment variable not set.") # If not set, you can hardcode it here for testing, but this is not recommended: # os.environ["FIREWORKS_API_KEY"] = "YOUR_API_KEY_HERE" # The model we'll use to generate the SQL. This acts as our "base" model. LLM_MODEL = "accounts/fireworks/models/llama-v3p1-8b-instruct" lLM = LLM(model=LLM_MODEL, deployment_type="auto", api_key=os.getenv("FIREWORKS_API_KEY")) # The URL for your running MCP server. MCP_SERVER_URL = None # PUT MCP SERVER URL HERE without the /mcp/ suffix at the end # --- 2. LOAD THE EXAMPLE DATA --- # This is the example data you provided. DATASET_FILE_PATH = "data/final_rft_sql_train_data.jsonl" ROW_INDEX_TO_TEST = 0 # 0 is the first row, 1 is the second row, etc. EXAMPLE_DATA = None try: with open(DATASET_FILE_PATH, 'r') as f: for i, line in enumerate(f): if i == ROW_INDEX_TO_TEST: EXAMPLE_DATA = json.loads(line) break if EXAMPLE_DATA is None: with open(DATASET_FILE_PATH, 'r') as f: line_count = sum(1 for line in f) raise IndexError(f"row index {ROW_INDEX_TO_TEST} is out of bounds for file with {line_count} rows.") print(f"Successfully loaded row {ROW_INDEX_TO_TEST} from '{DATASET_FILE_PATH}'.\n") print(EXAMPLE_DATA) print() except Exception as e: print(f"Warning: Could not load from file. Reason: {e}") ``` -------------------------------- ### Full Reinforcement Learning Training Example Source: https://docs.fireworks.ai/tools-sdks/python-client/sdk-reference Demonstrates the complete workflow for reinforcement learning training, including model setup, dataset creation, job submission, progress monitoring, and using the improved model. ```python import time from fireworks import LLM, Dataset # Create base model llm = LLM( model="qwen2p5-7b-instruct", deployment_type="on-demand", id="my-base-deployment", enable_addons=True ) # Apply deployment configuration to Fireworks llm.apply() # Generate rollouts and create dataset # (This would be your rollout generation logic) dataset = Dataset.from_list([ { "samples": [ { "messages": [ {"role": "user", "content": "What is 2+2?"}, {"role": "assistant", "content": "4"} ], "evals": {"score": 1.0} } ] } ]) dataset.sync() # Perform reinforcement learning step job = llm.reinforcement_step( dataset=dataset, output_model="my-improved-model-v1", epochs=1 ) # Monitor training progress while not job.is_completed: job.raise_if_bad_state() print(f"Training state: {job.state}") time.sleep(10) job = job.get() if job is None: raise Exception("Job was deleted while waiting for completion") print(f"Training completed! New model: {job.output_model}") # Use the improved model improved_llm = LLM( model=job.output_model, deployment_type="on-demand-lora", base_id=llm.deployment_id ) improved_llm.apply() ``` -------------------------------- ### Clone Repository and Install Dependencies Source: https://docs.fireworks.ai/fine-tuning/quickstart-math Clone the quickstart-gsm8k repository and install its dependencies. This method is recommended for setting up the project. ```bash git clone https://github.com/eval-protocol/quickstart-gsm8k.git cd quickstart-gsm8k pip install -r requirements.txt ``` ```bash mkdir -p gsm8k_artifacts/{tests/pytest/gsm8k,development} cp evaluation.py gsm8k_artifacts/tests/pytest/gsm8k/test_pytest_math_example.py cp gsm8k_sample.jsonl gsm8k_artifacts/development/gsm8k_sample.jsonl ``` -------------------------------- ### Get Dataset Information Source: https://docs.fireworks.ai/tools-sdks/firectl/commands/dataset-get Examples of how to get information for a dataset using its name or its fully qualified path. ```bash firectl dataset get my-dataset ``` ```bash firectl dataset get accounts/my-account/datasets/my-dataset ``` -------------------------------- ### Clone Quickstart Repository Source: https://docs.fireworks.ai/fine-tuning/quickstart-svg-agent Clone the necessary repository to begin the quickstart. This sets up the project structure for remote agent training. ```bash git clone git@github.com:eval-protocol/quickstart.git cd quickstart ``` -------------------------------- ### Set Example Monthly Spend Limit Source: https://docs.fireworks.ai/guides/quotas_usage/account-quotas Example of setting a specific monthly budget, in this case, $200. ```bash firectl quota update monthly-spend-usd --value 200 ``` -------------------------------- ### Install Fireworks Benchmark Tool Source: https://docs.fireworks.ai/deployments/benchmarking Clone the repository, navigate to the directory, and install dependencies using pip. ```bash git clone https://github.com/fw-ai/benchmark.git cd benchmark pip install -r requirements.txt ``` -------------------------------- ### Download Example VLM Dataset with wget Source: https://docs.fireworks.ai/fine-tuning/fine-tuning-vlm Use wget to download the example food reasoning dataset for VLM fine-tuning. ```bash # Download the example dataset wget https://huggingface.co/datasets/fireworks-ai/vision-food-reasoning-dataset/resolve/main/food_reasoning.jsonl ``` -------------------------------- ### Clone and Install Cookbook Source: https://docs.fireworks.ai/fine-tuning/training-api/cookbook/overview Clone the Fireworks Cookbook repository and install it locally. This sets up the necessary environment for using the training recipes. ```bash git clone https://github.com/fw-ai/cookbook.git cd cookbook/training && pip install -e . ``` -------------------------------- ### Basic firectl billing get-usage command Source: https://docs.fireworks.ai/tools-sdks/firectl/commands/billing-get-usage This is a basic example of how to get account usage for a specific month. Ensure you have the firectl CLI installed and authenticated. ```bash firectl billing get-usage --start-time 2026-05-01 --end-time 2026-06-01 ``` -------------------------------- ### Examples of Getting Training Shape Source: https://docs.fireworks.ai/tools-sdks/firectl/commands/training-shape-get These examples demonstrate how to call the 'firectl training-shape get' command with different training shape ID formats. ```bash firectl training-shape get my-shape ``` ```bash firectl training-shape get accounts/my-account/trainingShapes/my-shape ``` -------------------------------- ### Deployment Creation Response Example Source: https://docs.fireworks.ai/getting-started/ondemand-quickstart Example output from the 'firectl deployment create' command, showing details of the newly created deployment. ```bash Name: accounts//deployments/ Create Time: Expire Time: Created By: State: CREATING Status: OK Min Replica Count: 0 Max Replica Count: 1 Desired Replica Count: 0 Replica Count: 0 Autoscaling Policy: Scale Up Window: 30s Scale Down Window: 5m0s Scale To Zero Window: 5m0s Base Model: accounts/fireworks/models/gpt-oss-120b ...other fields... ``` -------------------------------- ### Install FireConnect CLI Source: https://docs.fireworks.ai/ecosystem/fireconnect/overview Installs the FireConnect CLI using a curl script. For non-interactive setup, provide your Fireworks API key. ```bash curl -fsSL https://raw.githubusercontent.com/fw-ai/fireconnect/main/install.sh | bash ``` ```bash curl -fsSL https://raw.githubusercontent.com/fw-ai/fireconnect/main/install.sh | FIREWORKS_API_KEY="fw_..." bash ``` -------------------------------- ### Example: Get DPO Job by ID Source: https://docs.fireworks.ai/tools-sdks/firectl/commands/dpo-job-get Example of retrieving a DPO job using its identifier. This is a common way to access job details. ```bash firectl dpo-job get my-dpo-job ``` -------------------------------- ### Tool Use Example: Get Stock Price Source: https://docs.fireworks.ai/api-reference/anthropic-messages Demonstrates how to define and use a tool to get a stock price. The model can then be prompted to use this tool with specific inputs. ```json { "name": "get_stock_price", "input": { "ticker": "^GSPC" } } ``` -------------------------------- ### High-Quality Training Example Source: https://docs.fireworks.ai/fine-tuning/cli-reference Configure for high-quality output by increasing the number of rollouts and adjusting the temperature. ```bash eval-protocol create rft \ --base-model accounts/fireworks/models/llama-v3p1-8b-instruct \ --output-model high-quality-model \ --n 8 \ --temperature 1.0 ``` -------------------------------- ### Example: Get DPO Job by Full Path Source: https://docs.fireworks.ai/tools-sdks/firectl/commands/dpo-job-get Example of retrieving a DPO job using its full account and job path. This provides a more specific way to target a job. ```bash firectl dpo-job get accounts/my-account/dpoJobs/my-dpo-job ``` -------------------------------- ### Set Up Environment Variables Source: https://docs.fireworks.ai/fine-tuning/quickstart-svg-agent Copy the example environment file and populate it with your Fireworks AI and OpenAI API keys. This step is crucial for authentication and secret management during training. ```bash cp evaluator/env.example evaluator/.env ``` ```bash FIREWORKS_API_KEY=your-fireworks-key-here OPENAI_API_KEY=your-openai-key-here ``` -------------------------------- ### Search and Select Model Source: https://docs.fireworks.ai/ecosystem/fireconnect/codex Search for models, for example those starting with 'glm', and then select one to be the default for Codex. ```bash fireconnect codex model select --search glm ``` -------------------------------- ### Tool Definition: Get Weather Source: https://docs.fireworks.ai/api-reference/anthropic-messages An example schema for defining a 'get_weather' tool, including its input parameters 'location' and 'unit'. ```json { "name": "get_weather", "input_schema": { "properties": { "location": { "description": "The city and state, e.g. San Francisco, CA", "type": "string" }, "unit": { "description": "Unit for the output - one of (celsius, fahrenheit)", "type": "string" } }, "required": [ "location" ], "type": "object" } } ``` -------------------------------- ### Start RFT from SFT Model with Parameters Source: https://docs.fireworks.ai/fine-tuning/warm-start Begin RFT training using a previously created SFT model as a warm start. This command includes additional RFT parameters like epochs, learning rate, and temperature. ```bash eval-protocol create rft \ --warm-start-from accounts/your-account/models/ \ --output-model \ --epochs 2 \ --learning-rate 5e-5 \ --temperature 0.8 ``` -------------------------------- ### Run OpenClaw Onboarding Wizard Source: https://docs.fireworks.ai/firepass Initiates the OpenClaw onboarding process, including setting up the background daemon. ```bash openclaw onboard --install-daemon ``` -------------------------------- ### Examples for Getting RLOR Trainer Job Source: https://docs.fireworks.ai/tools-sdks/firectl/commands/rlor-trainer-job-get Demonstrates how to specify the RLOR trainer job to retrieve. Jobs can be identified by their name or their full account/job path. ```bash firectl rlor-trainer-job get my-rlor-job ``` ```bash firectl rlor-trainer-job get accounts/my-account/rlorTrainerJobs/my-rlor-job ``` -------------------------------- ### Create Deployment Sampler Source: https://docs.fireworks.ai/fine-tuning/training-api/training-and-sampling Set up a deployment sampler for sampling model outputs. ```python deployment_sampler = service.create_deployment_sampler(...) ``` -------------------------------- ### List Grammar Example Source: https://docs.fireworks.ai/structured-responses/structured-output-grammar-based A simple grammar for lists, where each item starts with '- ' followed by the item content and a newline. The root rule ensures the list has one or more items. ```bnf # a grammar for lists root ::= ("- " item)+ item ::= [^\n]+ "\n" ``` -------------------------------- ### Configure and Run rl_loop Recipe Source: https://docs.fireworks.ai/fine-tuning/training-api/cookbook/rl Instantiate the `Config` object with necessary parameters for training, including model paths, dataset, and trainer/deployment configurations. Then, call `main(cfg)` to start the training loop. ```python from training.recipes.rl_loop import Config, main from training.utils import DeployConfig, TrainerConfig cfg = Config( log_path="./grpo_logs", base_model="accounts/fireworks/models/qwen3-8b", dataset="/path/to/gsm8k.jsonl", max_rows=200, completions_per_prompt=4, policy_loss="grpo", trainer=TrainerConfig(training_shape_id="accounts/fireworks/trainingShapes/qwen3-8b-128k-h200"), deployment=DeployConfig(deployment_id="grpo-serving", tokenizer_model="Qwen/Qwen3-8B"), weight_sync_interval=1, ) main(cfg) ``` -------------------------------- ### Create a training shape with advanced configurations Source: https://docs.fireworks.ai/tools-sdks/firectl/commands/training-shape-create This example demonstrates creating a training shape with additional configurations such as accelerator count, max context length, and node count for multi-node training. ```bash firectl training-shape create --base-model accounts/fireworks/models/llama-v2-7b --deployment-shape-version accounts/fireworks/deploymentShapes/my-shape/versions/v1 --trainer-image-tag 0.24.10 --accelerator-count 8 --max-context-length 4096 --node-count 1 ``` -------------------------------- ### Poll Load Status (cURL) Source: https://docs.fireworks.ai/fine-tuning/rl-rollout-integration Example using cURL to poll the status of a model deployment. This GET request helps monitor if all replicas are ready and have loaded the specified snapshot identity. ```bash curl https://api.fireworks.ai/hot_load/v1/models/hot_load \ -H "Authorization: Bearer " \ -H "fireworks-model: accounts//models/" \ -H "fireworks-deployment: accounts//deployments/" ``` -------------------------------- ### Create deployment with configuration file Source: https://docs.fireworks.ai/tools-sdks/firectl/commands/deployment-create Create a deployment using a specified configuration file. This allows for more complex deployment settings defined in a JSON file. ```bash firectl deployment create falcon-7b --file=/path/to/deployment-config.json ``` -------------------------------- ### Start Simple and Iterate on Evaluator Logic Source: https://docs.fireworks.ai/fine-tuning/evaluators Begin with a basic scoring mechanism and progressively add complexity as needed. This approach allows for quicker initial setup and refinement based on observed model behavior. ```python # Start here score = 1.0 if predicted == expected else 0.0 # Then refine if needed score = calculate_similarity(predicted, expected) ``` -------------------------------- ### Python Function Calling with Fireworks AI SDK Source: https://docs.fireworks.ai/getting-started/quickstart Use the Fireworks AI Python SDK to enable function calling. This example shows how to define a tool and make a request to the model to get weather information. ```python response = client.chat.completions.create( model="accounts/fireworks/models/kimi-k2-instruct-0905", messages=[ {"role": "user", "content": "What's the weather in Paris?"} ], tools=[ { "type": "function", "function": { "name": "get_weather", "description": "Get the current weather for a location", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "City name, e.g. San Francisco", } }, "required": ["location"], }, }, }, ], ) print(response.choices[0].message.tool_calls) ``` -------------------------------- ### LLM Instantiation Source: https://docs.fireworks.ai/tools-sdks/python-client/sdk-reference Demonstrates how to initialize an LLM instance with basic and advanced parameters, including different deployment types and LoRA configurations. ```APIDOC ## LLM Instantiation ### Description Initializes a new LLM instance with various configuration options. ### Method `LLM(*args, **kwargs)` ### Parameters #### Required Arguments * `model` (str) - The model identifier to use (e.g., `accounts/fireworks/models/llama-v3p2-3b-instruct`) * `deployment_type` (str) - The type of deployment to use. Must be one of: * `"serverless"`: Uses Fireworks' shared serverless infrastructure * `"on-demand"`: Uses dedicated resources for your deployment * `"auto"`: Automatically selects the most cost-effective option (recommended for experimentation) * `"on-demand-lora"`: For LoRA addons that require dedicated resources ### Request Example ```python from fireworks import LLM from datetime import timedelta # Basic usage with required parameters llm = LLM( model="accounts/fireworks/models/llama-v3p2-3b-instruct", deployment_type="auto" ) # Advanced usage with optional parameters llm = LLM( model="accounts/fireworks/models/llama-v3p2-3b-instruct", deployment_type="on-demand", id="my-custom-deployment", accelerator_type="NVIDIA_H100_80GB", min_replica_count=1, max_replica_count=3, scale_up_window=timedelta(seconds=30), scale_down_window=timedelta(minutes=10), enable_metrics=True ) # Apply deployment configuration to Fireworks llm.apply() # Deploy a fine-tuned model with on-demand deployment fine_tuned_llm = LLM( model="accounts/your-account/models/your-fine-tuned-model-id", deployment_type="on-demand", id="my-fine-tuned-deployment" # Simple string identifier ) # Apply deployment configuration to Fireworks fine_tuned_llm.apply() # Deploy fine-tuned model using multi-LoRA (sharing base deployment) base_model = LLM( model="accounts/fireworks/models/llama-v3p2-3b-instruct", deployment_type="on-demand", id="shared-base-deployment", enable_addons=True ) # Apply base deployment configuration to Fireworks base_model.apply() fine_tuned_with_lora = LLM( model="accounts/your-account/models/your-fine-tuned-model-id", deployment_type="on-demand-lora", base_id=base_model.deployment_id ) # Apply LoRA deployment configuration to Fireworks fine_tuned_with_lora.apply() ``` ``` -------------------------------- ### Usage Examples for Image Token Calculator Source: https://docs.fireworks.ai/faq-new/billing-pricing/how-many-tokens-per-image Demonstrates how to run the image token calculator script from the command line, specifying either a URL or a local file path for the image. ```bash # Calculate tokens for an image URL python token_calculator.py "https://example.com/image.jpg" # Calculate tokens for a local image python token_calculator.py "path/to/your/image.png" ``` -------------------------------- ### TypeScript Request to Get Generated Image Source: https://docs.fireworks.ai/api-reference/get-generated-image-from-flux-kontex This TypeScript example demonstrates how to fetch generated image results using `node-fetch`. Replace `{model}` with the desired FLUX.1 Kontext model and `$API_KEY` with your valid API key. ```typescript import fs from "fs"; import fetch from "node-fetch"; (async () => { const response = await fetch("https://api.fireworks.ai/inference/v1/workflows/accounts/fireworks/models/{model}/get_result", { method: "POST", headers: { "Content-Type": "application/json", "Authorization": "Bearer $API_KEY" }, body: JSON.stringify({ id: "request_id" }), }); })(); .catch(console.error); ``` -------------------------------- ### Example Formulas for Cost and Iteration Analysis Source: https://docs.fireworks.ai/fine-tuning/training-api/introduction These formulas can be used to estimate training iterations per month, effective cost per GPU hour, and multi-turn success rates. They are useful for comparing different training setups. ```text iterations_per_month = available_working_days / cycle_time_days effective_cost_per_gpu_hour = total_monthly_spend / gpu_hours_consumed multi_turn_success ~= (single_turn_success)^turn_count ``` -------------------------------- ### Configure and Run DPO Training Source: https://docs.fireworks.ai/fine-tuning/training-api/cookbook/dpo Set up the configuration for DPO training, specifying model paths, dataset, hyperparameters, and logging. Then, initiate the training process. ```python from training.recipes.dpo_loop import Config, main from training.utils import TrainerConfig, WandBConfig cfg = Config( log_path="./dpo_logs", base_model="accounts/fireworks/models/qwen3-8b", dataset="/path/to/preference_data.jsonl", tokenizer_model="Qwen/Qwen3-8B", beta=0.1, epochs=1, batch_size=4, max_seq_len=4096, trainer=TrainerConfig( training_shape_id="accounts/fireworks/trainingShapes/qwen3-8b-128k-h200", reference_training_shape_id="accounts/fireworks/trainingShapes/qwen3-8b-128k-h200-forward", ), wandb=WandBConfig(entity="my-team", project="dpo-experiment"), ) main(cfg) ``` -------------------------------- ### Configure and Run RL Training Source: https://docs.fireworks.ai/fine-tuning/training-api/cookbook/rl Set up the `Config` object with training parameters, model paths, and loss variants. Then, start the training loop by calling `main` with the configuration and the rollout function factory. ```python from training.recipes.async_rl_loop import Config, main from training.utils import DeployConfig, TrainerConfig, WandBConfig from my_rollout import make_rollout_fn # your rollout.py cfg = Config( log_path="./gsm8k_logs", base_model="accounts/fireworks/models/qwen3-8b", learning_rate=1.7e-5, completions_per_prompt=8, prompt_groups_per_step=8, policy_loss="grpo", # the "custom loss" knob max_head_offpolicy_versions=4, # off-policy staleness budget (0 = on-policy) trainer=TrainerConfig(training_shape_id="accounts/fireworks/trainingShapes/qwen3-8b-128k-h200"), deployment=DeployConfig(tokenizer_model="Qwen/Qwen3-8B"), wandb=WandBConfig(entity="my-team", project="gsm8k-rl"), ) rows = [...] # dataset rows; each becomes a sample_prompt main(cfg, rollout_fn_factory=make_rollout_fn, rows=rows) ``` -------------------------------- ### Incorrect Usage: Including --base-model with Warm Start Source: https://docs.fireworks.ai/fine-tuning/warm-start This example shows an incorrect usage pattern where `--base-model` is included alongside `--warm-start-from`. This will result in an error because the base model is automatically determined from the LoRA adapter. ```bash eval-protocol create rft \ --base-model accounts/fireworks/models/llama-v3p1-8b-instruct \ --warm-start-from accounts/your-account/models/ ``` -------------------------------- ### Create RFT Model with Warm Start Source: https://docs.fireworks.ai/fine-tuning/warm-start Use this command to initiate RFT training, continuing from a previously fine-tuned model. Do not include `--base-model` as it is inferred from the warm start model. ```bash eval-protocol create rft \ --warm-start-from accounts/your-account/models/ \ --output-model ``` -------------------------------- ### Get Dedicated Deployment Usage by Deployment and GPU Type Source: https://docs.fireworks.ai/accounts/exporting-usage-and-costs Use this command to retrieve detailed usage data for dedicated deployments, grouped by deployment name and accelerator type. Ensure you have set the start and end times for the desired period. ```bash firectl billing get-usage \ --start-time 2026-05-01 --end-time 2026-06-01 \ --usage-type dedicated-deployment \ --group-by deployment_name \ --group-by accelerator_type ``` ```bash curl -sG "https://api.fireworks.ai/v1/accounts/${ACCOUNT_ID}/billingUsage" \ -H "Authorization: Bearer ${FIREWORKS_API_KEY}" \ --data-urlencode "startTime=2026-05-01T00:00:00Z" \ --data-urlencode "endTime=2026-06-01T00:00:00Z" \ --data-urlencode "usageType=DEDICATED_DEPLOYMENT" \ --data-urlencode "groupBy=deployment_name" \ --data-urlencode "groupBy=accelerator_type" ``` -------------------------------- ### Quick Start: Fireworks AI Agent for AgentCore Source: https://docs.fireworks.ai/ecosystem/integrations/agentcore A minimal example of a Fireworks AI agent using the Strands framework, configured for deployment on AWS AgentCore Runtime. It includes setting up the model, defining tools, and an entrypoint for the AgentCore application. ```python from strands import Agent, tool from strands_tools import file_read, file_write from strands.models.openai import OpenAIModel import os from bedrock_agentcore.runtime import BedrockAgentCoreApp app = BedrockAgentCoreApp() @tool def code_python(user_prompt: str): """Generate Python code based on user requirements.""" return f"Generate clean Python code for: {user_prompt}" model = OpenAIModel( client_args={ "api_key": os.getenv("FIREWORKS_API_KEY"), "base_url": "https://api.fireworks.ai/inference/v1", }, model_id="accounts/fireworks/models/kimi-k2-instruct-0905", params={"max_tokens": 5000, "temperature": 0.0} ) agent = Agent( model=model, tools=[file_read, file_write, code_python], system_prompt="You are a software engineer. You can read files, write files and generate python code." ) @app.entrypoint def strands_agent_fireworks_ai(payload): user_input = payload.get("prompt") response = agent(user_input) return response.message['content'][0]['text'] if __name__ == "__main__": app.run() ``` -------------------------------- ### Chat Completion with Pydantic Schema and Reasoning Source: https://docs.fireworks.ai/structured-responses/structured-response-formatting Use this snippet to get structured JSON output from Fireworks AI chat completions, including the model's reasoning process. Ensure you have the 'fireworks' and 'pydantic' libraries installed. The Pydantic schema is defined and then converted to JSON schema for inclusion in the prompt. ```python import json from fireworks import Fireworks from pydantic import BaseModel client = Fireworks() # Define the output schema class QAResult(BaseModel): question: str answer: str # Include the schema in the prompt to preserve reasoning schema = QAResult.model_json_schema() response = client.chat.completions.create( model="accounts/fireworks/models/kimi-k2p5", messages=[ { "role": "user", "content": ( "Who wrote 'Pride and Prejudice'?\n\n" f"Reply in JSON matching this schema:\n{json.dumps(schema, indent=2)}" ) } ], max_tokens=1000 ) # The Fireworks SDK separates reasoning into its own field reasoning = response.choices[0].message.reasoning_content content = response.choices[0].message.content # Strip markdown code fences if the model wraps the JSON json_str = content.strip() if json_str.startswith("```"): json_str = json_str.split("\n", 1)[1].rsplit("```", 1)[0].strip() # Parse into Pydantic model qa_result = QAResult.model_validate_json(json_str) if reasoning: print("Reasoning:", reasoning) print("Result:", qa_result.model_dump_json(indent=2)) ``` -------------------------------- ### Setup OpenAI and Anthropic Clients Source: https://docs.fireworks.ai/examples/text-to-sql Initializes clients for OpenAI and Anthropic APIs, loading API keys from environment variables. Requires 'openai', 'anthropic', 'os', 'requests', 'json', 'time', 're', 'tqdm', and 'dotenv' libraries. ```python # Cell to evaluate OpenAI and Anthropic models (with manual logging) - GPT-4o and Claude Sonnet 4 import openai import anthropic import os import requests import json import time import re from tqdm.auto import tqdm from dotenv import load_dotenv load_dotenv() # --- 1. SETUP for Environment and Data --- MCP_SERVER_URL = None # <--- PUT MCP SERVER URL HERE without the /mcp/ suffix at the end DATASET_FILE_PATH = "data/final_rft_sql_test_data.jsonl" # --- Load Dataset --- dataset = [] try: with open(DATASET_FILE_PATH, 'r') as f: dataset = [json.loads(line) for line in f] print(f"Loaded {len(dataset)} evaluation examples from '{DATASET_FILE_PATH}'.") except Exception as e: print(f"FATAL: Could not load dataset. Error: {e}") dataset = [] # --- 2. SETUP for OpenAI and Anthropic --- # IMPORTANT: Make sure your API keys are set as environment variables. if "OPENAI_API_KEY" not in os.environ: print("FATAL: OPENAI_API_KEY environment variable not set.") if "ANTHROPIC_API_KEY" not in os.environ: print("FATAL: ANTHROPIC_API_KEY environment variable not set.") # Define Model IDs GPT_MODEL_ID = "gpt-4o" CLAUDE_MODEL_ID = "claude-sonnet-4-20250514" # --- Create API Clients --- openai_client = None anthropic_client = None try: openai_client = openai.OpenAI(api_key=os.getenv("OPENAI_API_KEY")) anthropic_client = anthropic.Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY")) print("OpenAI and Anthropic clients created successfully.") except Exception as e: print(f"FATAL: Could not create clients. Error: {e}") ``` -------------------------------- ### Remote Server Minimal Example for Fireworks Tracing Source: https://docs.fireworks.ai/fine-tuning/environments This Python snippet demonstrates a minimal remote server setup for integrating with Fireworks Tracing. It includes necessary imports for handling initialization requests, status updates, and tracing handlers, essential for correlating logs and model calls during RL fine-tuning. ```python import logging import os from eval_protocol import InitRequest, Status, FireworksTracingHttpHandler, RolloutIdFilter ``` -------------------------------- ### Quickstart GRPO Fine-tuning Source: https://docs.fireworks.ai/fine-tuning/training-api/cookbook/overview This snippet shows a basic configuration for GRPO fine-tuning using the training API. Ensure you have the correct model paths and dataset configured. ```python from training.recipes.rl_loop import Config, main from training.utils import DeployConfig, TrainerConfig cfg = Config( log_path="./grpo_quickstart", base_model="accounts/fireworks/models/qwen3-8b", dataset="/path/to/prompts.jsonl", max_rows=100, trainer=TrainerConfig( training_shape_id="accounts/fireworks/trainingShapes/qwen3-8b-128k-h200", ), deployment=DeployConfig( deployment_id="grpo-serving", tokenizer_model="Qwen/Qwen3-8B", ), weight_sync_interval=1, ) main(cfg) ``` -------------------------------- ### Install firectl CLI Source: https://docs.fireworks.ai/fine-tuning/agent/use-with-coding-agents Install the firectl command-line tool on Linux. Ensure you have curl and gunzip installed. ```bash curl -sL -o /tmp/firectl.gz https://storage.googleapis.com/fireworks-public/firectl/stable/linux-amd64.gz gunzip -f /tmp/firectl.gz sudo install -m 0755 /tmp/firectl /usr/local/bin/firectl ``` -------------------------------- ### Create Dataset from a List of Examples Source: https://docs.fireworks.ai/tools-sdks/python-client/sdk-reference Use the `Dataset.from_list()` class method to create a dataset for fine-tuning from a Python list. Each example in the list must be compatible with OpenAI's chat completion format. ```python from fireworks import Dataset # Create dataset from a list of examples examples = [ { "messages": [ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "What is the capital of France?"}, {"role": "assistant", "content": "Paris."} ] } ] dataset = Dataset.from_list(examples) ```