> ## 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 OpenAI

### Upload a file

> [tab] REST API
>
> ```sh theme={"system"}
> curl -X POST --header 'Authorization: Bearer <portkey_api_key>' \
>  --header 'x-portkey-provider: @provider' \
>  --form 'file=@dataset.jsonl' \
>  --form 'purpose=fine-tune' \
>  'https://aigw.portkey.ai/v1/files'
> ```

> [tab] OpenAI Python
>
> ```python theme={"system"}
> from openai import OpenAI
>
> openai = OpenAI(
>     api_key="PORTKEY_API_KEY",
>     base_url="https://aigw.portkey.ai/v1",
>     default_headers={"x-portkey-provider": "@PROVIDER"}
> )
>
> # Upload a file for fine-tuning
> file = openai.files.create(
>     file=open("dataset.jsonl", "rb"),
>     purpose="fine-tune"
> )
>
> print(file)
> ```

> [tab] OpenAI NodeJS
>
> ```typescript theme={"system"}
> import OpenAI from 'openai';
> import * as fs from 'fs';
>
> const openai = new OpenAI({
>     apiKey: "PORTKEY_API_KEY",
>     baseURL: "https://aigw.portkey.ai/v1",
>     defaultHeaders: { "x-portkey-provider": "@PROVIDER" }
> });
>
> (async () => {
>     // Upload a file for fine-tuning
>     const file = await openai.files.create({
>         file: fs.createReadStream("dataset.jsonl"),
>         purpose: "fine-tune"
>     });
>
>     console.log(file);
> })();
> ```

### Create a fine-tuning job

> [tab] REST API
>
> ```sh theme={"system"}
> curl -X POST --header 'Content-Type: application/json' \
>  --header 'Authorization: Bearer <portkey_api_key>' \
>  --data \
>  $'{"model": "@provider/<base_model>", "suffix": "<finetune_name>", "training_file": "<file_id>", "validation_file": "<file_id>", "method": {"type": "supervised", "supervised": {"hyperparameters": {"n_epochs": 1}}}}\n' \
> 'https://aigw.portkey.ai/v1/fine_tuning/jobs'
> ```

> [tab] OpenAI Python
>
> ```python theme={"system"}
> from openai import OpenAI
>
> openai = OpenAI(
>     api_key="PORTKEY_API_KEY",
>     base_url="https://aigw.portkey.ai/v1"
> )
>
> # Create a fine-tuning job
> fine_tune_job = openai.fine_tuning.jobs.create(
>     model="@PROVIDER/gpt-3.5-turbo", # Base model to fine-tune
>     training_file="file_id", # ID of the uploaded training file
>     validation_file="file_id", # Optional: ID of the uploaded validation file
>     suffix="finetune_name", # Custom suffix for the fine-tuned model name
>     hyperparameters={
>         "n_epochs": 1
>     }
> )
>
> print(fine_tune_job)
> ```

> [tab] OpenAI NodeJS
>
> ```typescript theme={"system"}
> import OpenAI from 'openai';
>
> const openai = new OpenAI({
>     apiKey: "PORTKEY_API_KEY",
>     baseURL: "https://aigw.portkey.ai/v1"
> });
>
> (async () => {
>     // Create a fine-tuning job
>     const fineTuneJob = await openai.fineTuning.jobs.create({
>         model: "@PROVIDER/gpt-3.5-turbo", // Base model to fine-tune
>         training_file: "file_id", // ID of the uploaded training file
>         validation_file: "file_id", // Optional: ID of the uploaded validation file
>         suffix: "finetune_name", // Custom suffix for the fine-tuned model name
>         hyperparameters: {
>             n_epochs: 1
>         }
>     });
>
>     console.log(fineTuneJob);
> })();
> ```

### List fine-tuning jobs

> [tab] REST API
>
> ```sh theme={"system"}
> curl -X GET --header 'Authorization: Bearer <portkey_api_key>' \
> --header 'x-portkey-provider: @provider' \
> 'https://aigw.portkey.ai/v1/fine_tuning/jobs'
> ```

> [tab] OpenAI Python
>
> ```python theme={"system"}
> from openai import OpenAI
>
> openai = OpenAI(
>     api_key="PORTKEY_API_KEY",
>     base_url="https://aigw.portkey.ai/v1",
>     default_headers={"x-portkey-provider": "@PROVIDER"}
> )
>
> # List all fine-tuning jobs
> jobs = openai.fine_tuning.jobs.list(
>     limit=10  # Optional: Number of jobs to retrieve (default: 20)
> )
>
> print(jobs)
> ```

> [tab] OpenAI NodeJS
>
> ```typescript theme={"system"}
> import OpenAI from 'openai';
>
> const openai = new OpenAI({
>     apiKey: "PORTKEY_API_KEY",
>     baseURL: "https://aigw.portkey.ai/v1",
>     defaultHeaders: { "x-portkey-provider": "@PROVIDER" }
> });
>
> (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);
> })();
> ```

### Get a fine-tuning job

> [tab] REST API
>
> ```sh theme={"system"}
> curl -X GET --header 'Authorization: Bearer <portkey_api_key>' \
> --header 'x-portkey-provider: @provider' \
> 'https://aigw.portkey.ai/v1/fine_tuning/jobs/<job_id>'
> ```

> [tab] OpenAI Python
>
> ```python theme={"system"}
> from openai import OpenAI
>
> openai = OpenAI(
>     api_key="PORTKEY_API_KEY",
>     base_url="https://aigw.portkey.ai/v1",
>     default_headers={"x-portkey-provider": "@PROVIDER"}
> )
>
> # 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] OpenAI NodeJS
>
> ```typescript theme={"system"}
> import OpenAI from 'openai';
>
> const openai = new OpenAI({
>     apiKey: "PORTKEY_API_KEY",
>     baseURL: "https://aigw.portkey.ai/v1",
>     defaultHeaders: { "x-portkey-provider": "@PROVIDER" }
> });
>
> (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);
> })();
> ```

### Cancel a fine-tuning job

> [tab] REST API
>
> ```sh theme={"system"}
> curl -X POST --header 'Authorization: Bearer <portkey_api_key>' \
> --header 'x-portkey-provider: @provider' \
> 'https://aigw.portkey.ai/v1/fine_tuning/jobs/<job_id>/cancel'
> ```

> [tab] OpenAI Python
>
> ```python theme={"system"}
> from openai import OpenAI
>
> openai = OpenAI(
>     api_key="PORTKEY_API_KEY",
>     base_url="https://aigw.portkey.ai/v1",
>     default_headers={"x-portkey-provider": "@PROVIDER"}
> )
>
> # Cancel a fine-tuning job
> cancelled_job = openai.fine_tuning.jobs.cancel(
>     "job_id"  # The ID of the fine-tuning job to cancel
> )
>
> print(cancelled_job)
> ```

> [tab] OpenAI NodeJS
>
> ```typescript theme={"system"}
> import OpenAI from 'openai';
>
> const openai = new OpenAI({
>     apiKey: "PORTKEY_API_KEY",
>     baseURL: "https://aigw.portkey.ai/v1",
>     defaultHeaders: { "x-portkey-provider": "@PROVIDER" }
> });
>
> (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);
> })();
> ```

Refer to [OpenAI's fine-tuning documentation](https://platform.openai.com/docs/api-reference/fine-tuning) for more information on the parameters and options available.


## Related topics

- [Fine-tune](https://docs.portkey.ai/docs/aigw/integrations/llms/fireworks/fine-tuning.md)
- [Data Service](https://docs.portkey.ai/docs/changelog/data-service.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.