# Trains and deploys a fine-tuned model.

POST https://api.cohere.com/v1/finetuning/finetuned-models
Content-Type: application/json

Creates a new fine-tuned model. The model will be trained on the dataset specified in the request body. The training process may take some time, and the model will be available once the training is complete.

Reference: https://docs.cohere.com/reference/createfinetunedmodel

## OpenAPI Specification

```yaml
openapi: 3.1.1
info:
  title: Trains and deploys a fine-tuned model.
  version: endpoint_finetuning.CreateFinetunedModel
paths:
  /v1/finetuning/finetuned-models:
    post:
      operationId: create-finetuned-model
      summary: Trains and deploys a fine-tuned model.
      description: >-
        Creates a new fine-tuned model. The model will be trained on the dataset
        specified in the request body. The training process may take some time,
        and the model will be available once the training is complete.
      tags:
        - - subpackage_finetuning
      parameters:
        - name: Authorization
          in: header
          description: >-
            Bearer authentication of the form `Bearer <token>`, where token is
            your auth token.
          required: true
          schema:
            type: string
        - name: X-Client-Name
          in: header
          description: |
            The name of the project that is making the request.
          required: false
          schema:
            type: string
      responses:
        '200':
          description: A successful response.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/CreateFinetunedModelResponse'
        '400':
          description: Bad Request
          content: {}
        '401':
          description: Unauthorized
          content: {}
        '403':
          description: Forbidden
          content: {}
        '404':
          description: Not Found
          content: {}
        '500':
          description: Internal Server Error
          content: {}
        '503':
          description: Status Service Unavailable
          content: {}
      requestBody:
        description: >-
          Information about the fine-tuned model. Must contain name and
          settings.
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/FinetunedModel'
components:
  schemas:
    BaseType:
      type: string
      enum:
        - value: BASE_TYPE_UNSPECIFIED
        - value: BASE_TYPE_GENERATIVE
        - value: BASE_TYPE_CLASSIFICATION
        - value: BASE_TYPE_RERANK
        - value: BASE_TYPE_CHAT
      default: BASE_TYPE_UNSPECIFIED
    Strategy:
      type: string
      enum:
        - value: STRATEGY_UNSPECIFIED
        - value: STRATEGY_VANILLA
        - value: STRATEGY_TFEW
      default: STRATEGY_UNSPECIFIED
    BaseModel:
      type: object
      properties:
        name:
          type: string
          description: The name of the base model.
        version:
          type: string
          description: read-only. The version of the base model.
        base_type:
          $ref: '#/components/schemas/BaseType'
          description: The type of the base model.
        strategy:
          $ref: '#/components/schemas/Strategy'
          description: 'Deprecated: The fine-tuning strategy.'
      required:
        - base_type
    LoraTargetModules:
      type: string
      enum:
        - value: LORA_TARGET_MODULES_UNSPECIFIED
        - value: LORA_TARGET_MODULES_QV
        - value: LORA_TARGET_MODULES_QKVO
        - value: LORA_TARGET_MODULES_QKVO_FFN
      default: LORA_TARGET_MODULES_UNSPECIFIED
    Hyperparameters:
      type: object
      properties:
        early_stopping_patience:
          type: integer
          description: >-
            Stops training if the loss metric does not improve beyond the value
            of

            `early_stopping_threshold` after this many times of evaluation.
        early_stopping_threshold:
          type: number
          format: double
          description: How much the loss must improve to prevent early stopping.
        train_batch_size:
          type: integer
          description: >-
            The batch size is the number of training examples included in a
            single

            training pass.
        train_epochs:
          type: integer
          description: The number of epochs to train for.
        learning_rate:
          type: number
          format: double
          description: The learning rate to be used during training.
        lora_alpha:
          type: integer
          description: |-
            Controls the scaling factor for LoRA updates. Higher values make the
            updates more impactful.
        lora_rank:
          type: integer
          description: >-
            Specifies the rank for low-rank matrices. Lower ranks reduce
            parameters

            but may limit model flexibility.
        lora_target_modules:
          $ref: '#/components/schemas/LoraTargetModules'
          description: The combination of LoRA modules to target.
    WandbConfig:
      type: object
      properties:
        project:
          type: string
          description: The WandB project name to be used during training.
        api_key:
          type: string
          description: The WandB API key to be used during training.
        entity:
          type: string
          description: The WandB entity name to be used during training.
      required:
        - project
        - api_key
    Settings:
      type: object
      properties:
        base_model:
          $ref: '#/components/schemas/BaseModel'
          description: The base model to fine-tune.
        dataset_id:
          type: string
          description: The data used for training and evaluating the fine-tuned model.
        hyperparameters:
          $ref: '#/components/schemas/Hyperparameters'
          description: Fine-tuning hyper-parameters.
        multi_label:
          type: boolean
          description: >-
            read-only. Whether the model is single-label or multi-label (only
            for classification).
        wandb:
          $ref: '#/components/schemas/WandbConfig'
          description: The Weights & Biases configuration (Chat fine-tuning only).
      required:
        - base_model
        - dataset_id
    Status:
      type: string
      enum:
        - value: STATUS_UNSPECIFIED
        - value: STATUS_FINETUNING
        - value: STATUS_DEPLOYING_API
        - value: STATUS_READY
        - value: STATUS_FAILED
        - value: STATUS_DELETED
        - value: STATUS_TEMPORARILY_OFFLINE
        - value: STATUS_PAUSED
        - value: STATUS_QUEUED
      default: STATUS_UNSPECIFIED
    FinetunedModel:
      type: object
      properties:
        id:
          type: string
          description: read-only. FinetunedModel ID.
        name:
          type: string
          description: FinetunedModel name (e.g. `foobar`).
        creator_id:
          type: string
          description: read-only. User ID of the creator.
        organization_id:
          type: string
          description: read-only. Organization ID.
        settings:
          $ref: '#/components/schemas/Settings'
          description: FinetunedModel settings such as dataset, hyperparameters...
        status:
          $ref: '#/components/schemas/Status'
          description: read-only. Current stage in the life-cycle of the fine-tuned model.
        created_at:
          type: string
          format: date-time
          description: read-only. Creation timestamp.
        updated_at:
          type: string
          format: date-time
          description: read-only. Latest update timestamp.
        completed_at:
          type: string
          format: date-time
          description: read-only. Timestamp for the completed fine-tuning.
        last_used:
          type: string
          format: date-time
          description: >-
            read-only. Deprecated: Timestamp for the latest request to this
            fine-tuned model.
      required:
        - name
        - settings
    CreateFinetunedModelResponse:
      type: object
      properties:
        finetuned_model:
          $ref: '#/components/schemas/FinetunedModel'
          description: Information about the fine-tuned model.

```

## SDK Code Examples

```java Cohere java SDK
/* (C)2024 */
package finetuning;

import com.cohere.api.Cohere;
import com.cohere.api.resources.finetuning.finetuning.types.*;

public class CreateFinetunedModel {
  public static void main(String[] args) {
    Cohere cohere = Cohere.builder().clientName("snippet").build();

    CreateFinetunedModelResponse response =
        cohere
            .finetuning()
            .createFinetunedModel(
                FinetunedModel.builder()
                    .name("test-finetuned-model")
                    .settings(
                        Settings.builder()
                            .baseModel(
                                BaseModel.builder().baseType(BaseType.BASE_TYPE_CHAT).build())
                            .datasetId("my-dataset-id")
                            .build())
                    .build());

    System.out.println(response);
  }
}

```

```go Cohere Go SDK
package main

import (
	"context"
	"log"
	"os"

	"github.com/cohere-ai/cohere-go/v2/client"
	"github.com/cohere-ai/cohere-go/v2/finetuning"
)

func main() {
	co := client.NewClient(client.WithToken(os.Getenv("CO_API_KEY")))

	resp, err := co.Finetuning.CreateFinetunedModel(
		context.TODO(),
		&finetuning.FinetunedModel{
			Name: "test-finetuned-model",
			Settings: &finetuning.Settings{
				DatasetId: "my-dataset-id",
				BaseModel: &finetuning.BaseModel{
					BaseType: finetuning.BaseTypeBaseTypeChat,
				},
			},
		},
	)
	if err != nil {
		log.Fatal(err)
	}

	log.Printf("%+v", resp.FinetunedModel)
}

```

```typescript Cohere TypeScript SDK
const { Cohere, CohereClient } = require('cohere-ai');

const cohere = new CohereClient({
  token: '<<apiKey>>',
});

(async () => {
  const finetunedModel = await cohere.finetuning.createFinetunedModel({
    name: 'test-finetuned-model',
    settings: {
      base_model: {
        base_type: Cohere.Finetuning.BaseType.BaseTypeChat,
      },
      dataset_id: 'test-dataset-id',
    },
  });

  console.log(finetunedModel);
})();

```

```python Sync
from cohere.finetuning import (
    BaseModel,
    FinetunedModel,
    Hyperparameters,
    Settings,
    WandbConfig,
)
import cohere

co = cohere.Client()
hp = Hyperparameters(
    early_stopping_patience=10,
    early_stopping_threshold=0.001,
    train_batch_size=16,
    train_epochs=1,
    learning_rate=0.01,
)
wnb_config = WandbConfig(
    project="test-project",
    api_key="<<wandbApiKey>>",
    entity="test-entity",
)
finetuned_model = co.finetuning.create_finetuned_model(
    request=FinetunedModel(
        name="test-finetuned-model",
        settings=Settings(
            base_model=BaseModel(
                base_type="BASE_TYPE_CHAT",
            ),
            dataset_id="my-dataset-id",
            hyperparameters=hp,
            wandb=wnb_config,
        ),
    )
)
print(finetuned_model)

```

```python Async
from cohere.finetuning import (
    BaseModel,
    FinetunedModel,
    Settings,
)
import cohere
import asyncio

co = cohere.AsyncClient()


async def main():
    response = await co.finetuning.create_finetuned_model(
        request=FinetunedModel(
            name="test-finetuned-model",
            settings=Settings(
                base_model=BaseModel(
                    base_type="BASE_TYPE_CHAT",
                ),
                dataset_id="my-dataset-id",
            ),
        )
    )
    print(response)


asyncio.run(main())

```

```ruby
require 'uri'
require 'net/http'

url = URI("https://api.cohere.com/v1/finetuning/finetuned-models")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

request = Net::HTTP::Post.new(url)
request["X-Client-Name"] = 'my-cool-project'
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n  \"name\": \"customer-support-chatbot-v1\",\n  \"settings\": {\n    \"base_model\": {\n      \"base_type\": \"BASE_TYPE_CHAT\"\n    },\n    \"dataset_id\": \"customer-support-dataset-2024\"\n  }\n}"

response = http.request(request)
puts response.read_body
```

```php
<?php

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', 'https://api.cohere.com/v1/finetuning/finetuned-models', [
  'body' => '{
  "name": "customer-support-chatbot-v1",
  "settings": {
    "base_model": {
      "base_type": "BASE_TYPE_CHAT"
    },
    "dataset_id": "customer-support-dataset-2024"
  }
}',
  'headers' => [
    'Authorization' => 'Bearer <token>',
    'Content-Type' => 'application/json',
    'X-Client-Name' => 'my-cool-project',
  ],
]);

echo $response->getBody();
```

```csharp
var client = new RestClient("https://api.cohere.com/v1/finetuning/finetuned-models");
var request = new RestRequest(Method.POST);
request.AddHeader("X-Client-Name", "my-cool-project");
request.AddHeader("Authorization", "Bearer <token>");
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", "{\n  \"name\": \"customer-support-chatbot-v1\",\n  \"settings\": {\n    \"base_model\": {\n      \"base_type\": \"BASE_TYPE_CHAT\"\n    },\n    \"dataset_id\": \"customer-support-dataset-2024\"\n  }\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
```

```swift
import Foundation

let headers = [
  "X-Client-Name": "my-cool-project",
  "Authorization": "Bearer <token>",
  "Content-Type": "application/json"
]
let parameters = [
  "name": "customer-support-chatbot-v1",
  "settings": [
    "base_model": ["base_type": "BASE_TYPE_CHAT"],
    "dataset_id": "customer-support-dataset-2024"
  ]
] as [String : Any]

let postData = JSONSerialization.data(withJSONObject: parameters, options: [])

let request = NSMutableURLRequest(url: NSURL(string: "https://api.cohere.com/v1/finetuning/finetuned-models")! as URL,
                                        cachePolicy: .useProtocolCachePolicy,
                                    timeoutInterval: 10.0)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error as Any)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()
```