> ## 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 bedrock file upload [guide](https://docs.portkey.ai/docs/integrations/llms/bedrock/files.md) for more details.

### Create a fine-tuning job

> [tab] 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
> provider="@PROVIDER" 
> )
>
> fine_tune_job = portkey.fine_tuning.jobs.create(
>     training_file="file_id", # encoded s3 file URI of the training data.
>     model="model_id", # ex: modelId from bedrock for fine-tuning
>     hyperparameters={
>     "n_epochs": 1
>     },
>     role_arn="role_arn", # service role arn for bedrock job to assume when running.
>     job_name="job_name", # name for the job, optional will created random if not provided.
>     validation_file="file_id", # optional, must be encoded s3 file URI.
>     suffix="finetuned_model_name",
>     model_type="text" # optional, chat or text.
>   )
>
> print(fine_tune_job)
>
> ```

> [tab] 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
>     provider="@PROVIDER"   
> )
>
> (async () => {
>     const fine_tune_job = await portkey.fineTuning.jobs.create(
>       training_file:"file_id", // encoded s3 file URI of the training data.
>       model:"model_id", // ex: modelId from bedrock for fine-tuning
>       hyperparameters: {
>         "n_epochs": 1
>       },
>       role_arn: "role_arn", // service role arn for bedrock job to assume when running.
>       job_name: "job_name", // name for the job, optional will created random if not provided.
>       validation_file: "file_id", // optional, must be encoded s3 file URI.
>       suffix: "finetuned_model_name",
>       model_type: "text" // optional, chat or text.
>     )
>
>   console.log(fine_tune_job)
> })();
> ```

> [tab] 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(
>         provider="@PROVIDER",
>         api_key="PORTKEY_API_KEY"
>     )
> )
>
> fine_tune_job = openai.fine_tuning.jobs.create(
>     training_file="file_id", # encoded s3 file URI of the training data.
>     model="model_id", # bedrock modelId for fine-tuning
>     hyperparameters={
>       "n_epochs": 1
>     },
>     role_arn="role_arn", # service role arn for bedrock job to assume when running.
>     job_name="job_name", # name for the job, optional will created random if not provided.
>     validation_file="file_id", # optional, must be encoded s3 file URI.
>     suffix="finetuned_model_name",
>     model_type="text" # optional, chat or text.
>   )
>
> print(fine_tune_job)
> ```

> [tab] 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({
>     provider:"@PROVIDER",
>     apiKey: "PORTKEY_API_KEY" // defaults to process.env["PORTKEY_API_KEY"]
>   })
> });
>
> (async () => {
>     const fine_tune_job = await openai.fineTuning.jobs.create({
>       training_file: "file_id", // encoded s3 file URI of the training data.
>       model: "model_id", // ex: `modelId` from bedrock for fine-tuning
>       hyperparameters: {
>         "n_epochs": 1
>       },
>       role_arn: "role_arn", // service role arn for bedrock job to assume when running.
>       job_name: "job_name", // name for the job, optional will created random if not provided.
>       validation_file: "file_id", // optional, must be encoded s3 file URI.
>       suffix: "finetuned_model_name",
>       model_type: "text" // optional, chat or text.
>     });
>
>   console.log(fine_tune_job)
> })();
> ```

> [tab] REST API
>
> ```sh theme={"system"}
> curl \
> --header 'Content-Type: application/json' \
> --header 'x-portkey-api-key: <api_key>' \
> --header 'x-portkey-provider: @provider' \
> --header 'x-portkey-aws-s3-bucket: <s3_bucket>' \
> --data '{
>   "model": "<model_id>",
>   "model_type": "text", #chat or text
>   "suffix": "<finetune_model_name>",
>   "training_file": "<s3_path.jsonl>",
>   "role_arn": "<role_arn>",
>   "job_name": "<job_name>",
>   "hyperparameters": {
>     "n_epochs": 1
>   }
> }' \
> 'https://api.portkey.ai/v1/fine_tuning/jobs'
> ```

**Notes:**

* Bedrock fine-tuning dataset format is a little bit different from OpenAI's fine-tuning dataset format.
* `model_type` field is required for the dataset transformation, currently gateway does the following dataset transformation:
  * `chat` -> `text-to-text`
  * `chat` -> `chat`.
* `model` param should be the `ModelID` that is required for fine-tuning not for the inference. `ModelID` is different for inference and fine-tuning.

> List of supported finetune models and their IDs are available at [Bedrock documentation](https://docs.aws.amazon.com/bedrock/latest/userguide/model-customization.html)

## List Fine-tuning Jobs

> [tab] 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
>     provider="@PROVIDER" 
> )
>
> # List all fine-tuning jobs
> jobs = portkey.fine_tuning.jobs.list(
>     limit=10  # Optional: Number of jobs to retrieve (default: 20)
> )
>
> print(jobs)
> ```

> [tab] 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
>     provider:"@PROVIDER"   
> });
>
> (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] 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(
>         provider="@PROVIDER",
>         api_key="PORTKEY_API_KEY"
>     )
> )
>
> # 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';
> import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai';
>
> const openai = new OpenAI({
>     apiKey: 'OPENAI_API_KEY',
>     baseURL: PORTKEY_GATEWAY_URL,
>     defaultHeaders: createHeaders({
>         provider:"@PROVIDER",
>         apiKey: "PORTKEY_API_KEY"
>     })
> });
>
> (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] REST API
>
> ```sh theme={"system"}
> curl \
> --header 'Content-Type: application/json' \
> --header 'x-portkey-api-key: <api_key>' \
> --header 'x-portkey-provider: @provider' \
> 'https://api.portkey.ai/v1/fine_tuning/jobs?limit=10'
> ```

## Retrieve Fine-tuning Job

> [tab] 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
>     provider="@PROVIDER" 
> )
>
> # 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] 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
>     provider:"@PROVIDER"   
> });
>
> (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] 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(
>         provider="@PROVIDER",
>         api_key="PORTKEY_API_KEY"
>     )
> )
>
> # 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';
> import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai';
>
> const openai = new OpenAI({
>     apiKey: 'OPENAI_API_KEY',
>     baseURL: PORTKEY_GATEWAY_URL,
>     defaultHeaders: createHeaders({
>         provider:"@PROVIDER",
>         apiKey: "PORTKEY_API_KEY"
>     })
> });
>
> (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] REST API
>
> ```sh theme={"system"}
> curl \
> --header 'Content-Type: application/json' \
> --header 'x-portkey-api-key: <api_key>' \
> --header 'x-portkey-provider: @provider' \
> 'https://api.portkey.ai/v1/fine_tuning/jobs/<job_id>'
> ```

## Cancel Fine-tuning Job

> [tab] 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
>     provider="@PROVIDER" 
> )
>
> # 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] 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
>     provider:"@PROVIDER"   
> });
>
> (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] 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(
>         provider="@PROVIDER",
>         api_key="PORTKEY_API_KEY"
>     )
> )
>
> # 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] 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({
>         provider:"@PROVIDER",
>         apiKey: "PORTKEY_API_KEY"
>     })
> });
>
> (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] REST API
>
> ```sh theme={"system"}
> curl \
> --request POST \
> --header 'Content-Type: application/json' \
> --header 'x-portkey-api-key: <api_key>' \
> --header 'x-portkey-provider: @provider' \
> 'https://api.portkey.ai/v1/fine_tuning/jobs/<job_id>/cancel'
> ```

## References

* Fine-tune Support types for models: [Link](https://docs.aws.amazon.com/bedrock/latest/userguide/model-customization-prepare.html#model-customization-data-support)
* Fine-tuning Documentation: [Link](https://docs.aws.amazon.com/bedrock/latest/userguide/custom-models.html)

***

> [card] [Portkey is now PRISMA AIRS AI Gateway. See it in action.](https://www.paloaltonetworks.in/ai-security/ai-gateway?utm_source=portkey\&utm_medium=referral\&utm_campaign=prisma_airs\&utm_content=docs_nav#contact)
>
> Contact Us


## Related topics

- [Fine-tune](https://docs.portkey.ai/docs/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.