> ## Documentation Index
> Fetch the complete documentation index at: https://docs.portkey.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Fine-tune

> Fine-tune your models with Bedrock

### Upload a file

Please follow to the fireworks file upload [guide](/integrations/llms/fireworks/files) for more details.

### Create a fine-tuning job

<Tabs>
  <Tab title="Python">
    ```python theme={"system"}
    from portkey_ai import Portkey

    # Initialize the Portkey client

    portkey = Portkey(
        api_key="PORTKEY_API_KEY", # Replace with your Portkey API key
        virtual_key="VIRTUAL_KEY", # Add your provider's virtual key
        fireworks_account_id="FIREWORKS_ACCOUNT_ID" # Add your fireworks's account id
    )

    fine_tune_job = portkey.fine_tuning.jobs.create(
        training_file="file_id",
        model="model_id",
        hyperparameters={
            "n_epochs": 1
        },
        validation_file="file_id",
        suffix="finetuned_model_name",
      )

    print(fine_tune_job)

    ```
  </Tab>

  <Tab title="NodeJS">
    ```typescript theme={"system"}
    import { Portkey } from "portkey-ai";
    // Initialize the Portkey client
    const portkey = Portkey(
        apiKey="PORTKEY_API_KEY",  // Replace with your Portkey API key
        virtualKey="VIRTUAL_KEY",   // Add your provider's virtual key
        fireworksAccountId="FIREWORKS_ACCOUNT_ID" // Add your fireworks's account id
    )

    (async () => {
        const fine_tune_job = await portkey.fineTuning.jobs.create(
          training_file:"file_id",
          model:"model_id",
          hyperparameters: {
            "n_epochs": 1
          },
          validation_file: "file_id",
          suffix: "finetuned_model_name",
        )

      console.log(fine_tune_job)
    })();
    ```
  </Tab>

  <Tab title="OpenAI Python">
    ```python theme={"system"}
    from openai import OpenAI
    from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders

    openai = OpenAI(
        api_key='OPENAI_API_KEY',
        base_url=PORTKEY_GATEWAY_URL,
        default_headers=createHeaders(
            virtual_key="VIRTUAL_KEY",
            api_key="PORTKEY_API_KEY",
            fireworks_account_id="FIREWORKS_ACCOUNT_ID"
        )
    )

    fine_tune_job = openai.fine_tuning.jobs.create(
        training_file="file_id",
        model="model_id",
        hyperparameters={
          "n_epochs": 1
        },
        validation_file="file_id",
        suffix="finetuned_model_name",
      )

    print(fine_tune_job)
    ```
  </Tab>

  <Tab title="OpenAI NodeJS">
    ```typescript theme={"system"}
    import OpenAI from 'openai'; // We're using the v4 SDK
    import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai'

    const openai = new OpenAI({
      apiKey: 'OPENAI_API_KEY', // defaults to process.env["OPENAI_API_KEY"],
      baseURL: PORTKEY_GATEWAY_URL,
      defaultHeaders: createHeaders({
        virtualKey: "VIRTUAL_KEY",
        apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"]
        fireworksAccountId: "FIREWORKS_ACCOUNT_ID"
      })
    });

    (async () => {
        const fine_tune_job = await openai.fineTuning.jobs.create({
          training_file: "file_id",
          model: "model_id",
          hyperparameters: {
            "n_epochs": 1
          },
          validation_file: "file_id",
          suffix: "finetuned_model_name",
        });

      console.log(fine_tune_job)
    })();
    ```
  </Tab>

  <Tab title="REST API">
    ```sh theme={"system"}
    curl \
    --header 'Content-Type: application/json' \
    --header 'x-portkey-api-key: <api_key>' \
    --header 'x-portkey-virtual-key: <virtual_key>' \
    --header 'x-portkey-fireworks-account-id: <fireworks_account_id>' \
    --data '{
      "model": "<model_id>",
      "suffix": "<finetune_model_name>",
      "training_file": "<s3_path.jsonl>",
      "hyperparameters": {
        "n_epochs": 1
      }
    }' \
    'https://api.portkey.ai/v1/fine_tuning/jobs'
    ```
  </Tab>
</Tabs>

## List Fine-tuning Jobs

<Tabs>
  <Tab title="Python">
    ```python theme={"system"}
    from portkey_ai import Portkey

    # Initialize the Portkey client
    portkey = Portkey(
        api_key="PORTKEY_API_KEY", # Replace with your Portkey API key
        virtual_key="VIRTUAL_KEY", # Add your provider's virtual key
        fireworks_account_id="FIREWORKS_ACCOUNT_ID" # Add your fireworks's account id
    )

    # List all fine-tuning jobs
    jobs = portkey.fine_tuning.jobs.list(
        limit=10  # Optional: Number of jobs to retrieve (default: 20)
    )

    print(jobs)
    ```
  </Tab>

  <Tab title="NodeJS">
    ```typescript theme={"system"}
    import { Portkey } from "portkey-ai";

    // Initialize the Portkey client
    const portkey = Portkey({
        apiKey: "PORTKEY_API_KEY",  // Replace with your Portkey API key
        virtualKey: "VIRTUAL_KEY",   // Add your provider's virtual key
        fireworksAccountId: "FIREWORKS_ACCOUNT_ID" // Add your fireworks's account id
    });

    (async () => {
        // List all fine-tuning jobs
        const jobs = await portkey.fineTuning.jobs.list({
            limit: 10  // Optional: Number of jobs to retrieve (default: 20)
        });

        console.log(jobs);
    })();
    ```
  </Tab>

  <Tab title="OpenAI Python">
    ```python theme={"system"}
    from openai import OpenAI
    from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders

    openai = OpenAI(
        api_key='OPENAI_API_KEY',
        base_url=PORTKEY_GATEWAY_URL,
        default_headers=createHeaders(
            virtual_key="VIRTUAL_KEY",
            api_key="PORTKEY_API_KEY",
            fireworks_account_id="FIREWORKS_ACCOUNT_ID"
        )
    )

    # List all fine-tuning jobs
    jobs = openai.fine_tuning.jobs.list(
        limit=10  # Optional: Number of jobs to retrieve (default: 20)
    )

    print(jobs)
    ```
  </Tab>

  <Tab title="OpenAI NodeJS">
    ```typescript theme={"system"}
    import OpenAI from 'openai';
    import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai';

    const openai = new OpenAI({
        apiKey: 'OPENAI_API_KEY',
        baseURL: PORTKEY_GATEWAY_URL,
        defaultHeaders: createHeaders({
            virtualKey: "VIRTUAL_KEY",
            apiKey: "PORTKEY_API_KEY",
            fireworksAccountId: "FIREWORKS_ACCOUNT_ID"
        })
    });

    (async () => {
        // List all fine-tuning jobs
        const jobs = await openai.fineTuning.jobs.list({
            limit: 10  // Optional: Number of jobs to retrieve (default: 20)
        });

        console.log(jobs);
    })();
    ```
  </Tab>

  <Tab title="REST API">
    ```sh theme={"system"}
    curl \
    --header 'Content-Type: application/json' \
    --header 'x-portkey-api-key: <api_key>' \
    --header 'x-portkey-virtual-key: <virtual_key>' \
    --header 'x-portkey-fireworks-account-id: <fireworks_account_id>' \
    'https://api.portkey.ai/v1/fine_tuning/jobs?limit=10'
    ```
  </Tab>
</Tabs>

## Retrieve Fine-tuning Job

<Tabs>
  <Tab title="Python">
    ```python theme={"system"}
    from portkey_ai import Portkey

    # Initialize the Portkey client
    portkey = Portkey(
        api_key="PORTKEY_API_KEY", # Replace with your Portkey API key
        virtual_key="VIRTUAL_KEY", # Add your provider's virtual key
        fireworks_account_id="FIREWORKS_ACCOUNT_ID" # Add your fireworks's account id
    )

    # Retrieve a specific fine-tuning job
    job = portkey.fine_tuning.jobs.retrieve(
        job_id="job_id"  # The ID of the fine-tuning job to retrieve
    )

    print(job)
    ```
  </Tab>

  <Tab title="NodeJS">
    ```typescript theme={"system"}
    import { Portkey } from "portkey-ai";

    // Initialize the Portkey client
    const portkey = Portkey({
        apiKey: "PORTKEY_API_KEY",  // Replace with your Portkey API key
        virtualKey: "VIRTUAL_KEY",   // Add your provider's virtual key
        fireworksAccountId: "FIREWORKS_ACCOUNT_ID" // Add your fireworks's account id
    });

    (async () => {
        // Retrieve a specific fine-tuning job
        const job = await portkey.fineTuning.jobs.retrieve({
            job_id: "job_id"  // The ID of the fine-tuning job to retrieve
        });

        console.log(job);
    })();
    ```
  </Tab>

  <Tab title="OpenAI Python">
    ```python theme={"system"}
    from openai import OpenAI
    from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders

    openai = OpenAI(
        api_key='OPENAI_API_KEY',
        base_url=PORTKEY_GATEWAY_URL,
        default_headers=createHeaders(
            virtual_key="VIRTUAL_KEY",
            api_key="PORTKEY_API_KEY",
            fireworks_account_id="FIREWORKS_ACCOUNT_ID"
        )
    )

    # Retrieve a specific fine-tuning job
    job = openai.fine_tuning.jobs.retrieve(
        fine_tuning_job_id="job_id"  # The ID of the fine-tuning job to retrieve
    )

    print(job)
    ```
  </Tab>

  <Tab title="OpenAI NodeJS">
    ```typescript theme={"system"}
    import OpenAI from 'openai';
    import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai';

    const openai = new OpenAI({
        apiKey: 'OPENAI_API_KEY',
        baseURL: PORTKEY_GATEWAY_URL,
        defaultHeaders: createHeaders({
            virtualKey: "VIRTUAL_KEY",
            apiKey: "PORTKEY_API_KEY",
            fireworksAccountId: "FIREWORKS_ACCOUNT_ID"
        })
    });

    (async () => {
        // Retrieve a specific fine-tuning job
        const job = await openai.fineTuning.jobs.retrieve(
            "job_id"  // The ID of the fine-tuning job to retrieve
        );

        console.log(job);
    })();
    ```
  </Tab>

  <Tab title="REST API">
    ```sh theme={"system"}
    curl \
    --header 'Content-Type: application/json' \
    --header 'x-portkey-api-key: <api_key>' \
    --header 'x-portkey-virtual-key: <virtual_key>' \
    --header 'x-portkey-fireworks-account-id: <fireworks_account_id>' \
    'https://api.portkey.ai/v1/fine_tuning/jobs/<job_id>'
    ```
  </Tab>
</Tabs>

## Cancel Fine-tuning Job

<Tabs>
  <Tab title="Python">
    ```python theme={"system"}
    from portkey_ai import Portkey

    # Initialize the Portkey client
    portkey = Portkey(
        api_key="PORTKEY_API_KEY", # Replace with your Portkey API key
        virtual_key="VIRTUAL_KEY", # Add your provider's virtual key
        fireworks_account_id="FIREWORKS_ACCOUNT_ID" # Add your fireworks's account id
    )

    # Cancel a fine-tuning job
    cancelled_job = portkey.fine_tuning.jobs.cancel(
        job_id="job_id"  # The ID of the fine-tuning job to cancel
    )

    print(cancelled_job)
    ```
  </Tab>

  <Tab title="NodeJS">
    ```typescript theme={"system"}
    import { Portkey } from "portkey-ai";

    // Initialize the Portkey client
    const portkey = Portkey({
        apiKey: "PORTKEY_API_KEY",  // Replace with your Portkey API key
        virtualKey: "VIRTUAL_KEY",   // Add your provider's virtual key
        fireworksAccountId: "FIREWORKS_ACCOUNT_ID" // Add your fireworks's account id
    });

    (async () => {
        // Cancel a fine-tuning job
        const cancelledJob = await portkey.fineTuning.jobs.cancel({
            job_id: "job_id"  // The ID of the fine-tuning job to cancel
        });

        console.log(cancelledJob);
    })();
    ```
  </Tab>

  <Tab title="OpenAI Python">
    ```python theme={"system"}
    from openai import OpenAI
    from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders

    openai = OpenAI(
        api_key='OPENAI_API_KEY',
        base_url=PORTKEY_GATEWAY_URL,
        default_headers=createHeaders(
            virtual_key="VIRTUAL_KEY",
            api_key="PORTKEY_API_KEY",
            fireworks_account_id="FIREWORKS_ACCOUNT_ID"
        )
    )

    # Cancel a fine-tuning job
    cancelled_job = openai.fine_tuning.jobs.cancel(
        fine_tuning_job_id="job_id"  # The ID of the fine-tuning job to cancel
    )

    print(cancelled_job)
    ```
  </Tab>

  <Tab title="OpenAI NodeJS">
    ```typescript theme={"system"}
    import OpenAI from 'openai';
    import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai';

    const openai = new OpenAI({
        apiKey: 'OPENAI_API_KEY',
        baseURL: PORTKEY_GATEWAY_URL,
        defaultHeaders: createHeaders({
            virtualKey: "VIRTUAL_KEY",
            apiKey: "PORTKEY_API_KEY",
            fireworksAccountId: "FIREWORKS_ACCOUNT_ID"
        })
    });

    (async () => {
        // Cancel a fine-tuning job
        const cancelledJob = await openai.fineTuning.jobs.cancel(
            "job_id"  // The ID of the fine-tuning job to cancel
        );

        console.log(cancelledJob);
    })();
    ```
  </Tab>

  <Tab title="REST API">
    ```sh theme={"system"}
    curl \
    --request POST \
    --header 'Content-Type: application/json' \
    --header 'x-portkey-api-key: <api_key>' \
    --header 'x-portkey-virtual-key: <virtual_key>' \
    --header 'x-portkey-fireworks-account-id: <fireworks_account_id>' \
    'https://api.portkey.ai/v1/fine_tuning/jobs/<job_id>/cancel'
    ```
  </Tab>
</Tabs>

## References

* [Fireworks Fine-tuning](https://docs.fireworks.ai/fine-tuning/fine-tuning-models)
* [Fireworks Fine-tuning API](https://docs.fireworks.ai/api-reference/list-supervised-fine-tuning-jobs)
