[Yandex Cloud documentation](../../index.md) > [Tutorials](../index.md) > [Machine learning and artificial intelligence](index.md) > Development with DataSphere > Model fine-tuning in DataSphere

# Model fine-tuning in DataSphere Notebooks

You can [fine-tune](https://aistudio.yandex.ru/docs/en/ai-studio/concepts/tuning/index) Yandex Cloud AI Studio language models for them to understand your tasks better via the API or ML SDK. It is convenient to run fine-tuning on [Yandex DataSphere](../../datasphere/index.md) notebooks.

In this tutorial, you will fine-tune a model in DataSphere using the SDK. You can also clone the [repository](https://github.com/yandex-cloud-examples/yc-foundation-models-datasphere-tuning) and run the notebook locally by changing the [authentication settings](https://aistudio.yandex.ru/docs/en/ai-studio/sdk/index).

To fine-tune a model:

1. [Set up your infrastructure](#infra).
1. [Create an API key for the service account](#create-key).
1. [Create secrets](#create-secrets).
1. [Fine-tune your model](#fine-tuning).

If you no longer need the resources you created, [delete them](#clear-out).

## Getting started {#before-you-begin}

Before getting started, register in Yandex Cloud, set up a [community](../../datasphere/concepts/community.md), and link your [billing account](../../billing/concepts/billing-account.md) to it.
1. [On the DataSphere home page](https://datasphere.yandex.cloud), click **Try for free** and select an account to log in with: Yandex ID or your working account with the identity federation (SSO).
1. Select the [Yandex Identity Hub organization](../../organization/index.md) you are going to use in Yandex Cloud.
1. [Create a community](../../datasphere/operations/community/create.md).
1. [Link your billing account](../../datasphere/operations/community/link-ba.md) to the DataSphere community you are going to work in. Make sure you have a linked billing account and its [status](../../billing/concepts/billing-account-statuses.md) is `ACTIVE` or `TRIAL_ACTIVE`. If you do not have a billing account yet, create one in the DataSphere interface.

### Required paid resources {#paid-resources}

The infrastructure support cost for fine-tuning a model includes:

* Fee for using [DataSphere computing resources](../../datasphere/pricing.md).
* Fee for [text generation](https://aistudio.yandex.ru/docs/en/ai-studio/pricing) by the model.

## Set up your infrastructure {#infra}

Log in to the Yandex Cloud [management console](https://console.yandex.cloud) and select the organization you use to access DataSphere. On the [**Yandex Cloud Billing**](https://center.yandex.cloud/billing/accounts) page, make sure you have a billing account linked.

If you have an active billing account, you can go to the [cloud page](https://console.yandex.cloud/cloud) to create or select a folder to run your infrastructure.

{% note info %}

If you are using an [identity federation](../../organization/concepts/add-federation.md) to work with Yandex Cloud, you might not have access to billing details. In this case, contact your Yandex Cloud organization administrator.

{% endnote %}

### Create a folder {#create-folder}

{% list tabs group=instructions %}

- Management console {#console}

   1. In the [management console](https://console.yandex.cloud), select a cloud and click ![create](../../_assets/console-icons/plus.svg)**Create folder**.
   1. Name your folder, e.g., `data-folder`.
   1. Click **Create**.

{% endlist %}

### Create a service account for the DataSphere project {#create-sa}

{% list tabs group=instructions %}

- Management console {#console}

  1. Navigate to `data-folder`.
  1. [Go](../../console/operations/select-service.md#select-service) to **Identity and Access Management**.
  1. Click **Create service account**.
  1. Name the [service account](../../iam/concepts/users/service-accounts.md), e.g., `gpt-user`.
  1. Click **Add role** and assign the `ai.languageModels.user` role to the service account.
  1. Click **Create**.

{% endlist %}

### Add the service account to the project {#sa-to-project}

To enable the service account to access the model from the notebook, add it to the list of project members.

{% list tabs group=instructions %}

- Management console {#console}

  1. Select the project in your community or on the DataSphere [home page](https://datasphere.yandex.cloud) in the **Recent projects** tab.
  1. In the **Members** tab, click **Add member**.
  1. Select the `gpt-user` account and click **Add**.

{% endlist %}

## Create an API key for the service account {#create-key}

To enable the service account to access the model, create an [API key](../../iam/concepts/authorization/api-key.md).

{% list tabs group=instructions %}

- Management console {#console}

  1. In the [management console](https://console.yandex.cloud), navigate to `data-folder`.
  1. [Go](../../console/operations/select-service.md#select-service) to **Identity and Access Management**.
  1. In the left-hand panel, select ![FaceRobot](../../_assets/console-icons/face-robot.svg) **Service accounts**.
  1. In the list that opens, select the `gpt-user` service account.
  1. In the top panel, click ![image](../../_assets/console-icons/plus.svg) **Create new key** and select **Create API key**.
  1. Click **Create**.
  1. Save the ID and secret key.

{% endlist %}

## Create secrets {#create-secrets}

To get the API key and folder ID from the notebook, create [secrets](../../datasphere/concepts/secrets.md) with the key and folder IDs.

1. Select the project in your community or on the DataSphere [home page](https://datasphere.yandex.cloud) in the **Recent projects** tab.
1. Under **Project resources**, click ![secret](../../_assets/console-icons/shield-check.svg)**Secret**.
1. Click **Create**.
1. In the **Name** field, enter the name for the secret: `API_KEY`.
1. In the **Value** field, paste the key ID.
1. Click **Create**.
1. Create another secret named `FOLDER_ID` and containing the folder ID.

## Fine-tune the model {#fine-tuning}

You will run the fine-tuning code from the DataSphere notebook. The [fine-tuning data](https://aistudio.yandex.ru/docs/en/ai-studio/concepts/resources/dataset#generating) is stored in [JSON Lines](https://jsonlines.org/) format.

1. Open the notebook with the code by following the link below:

   <a href="https://datasphere.yandex.cloud/import-ipynb?path=https://raw.githubusercontent.com/yandex-cloud-examples/yc-foundation-models-datasphere-tuning/main/LoRA-train-YandexGPT.ipynb"><img src="https://storage.yandexcloud.net/datasphere-assets/datasphere_badge_v2_ru.svg" 
     alt="Open in DataSphere"/></a>

1. Download the `generations.jsonlines` file from the [GitHub](https://github.com/yandex-cloud-examples/yc-foundation-models-datasphere-tuning) repository and place it in the same directory as your notebook.

1. Install the DataSphere SDK by running this code in a notebook cell:

   ```python
   %pip install yandex-ai-studio-sdk --upgrade
   ```

1. Import the required libraries:

   ```python
   import pathlib
   import uuid
   import os
   from __future__ import annotations

   from yandex_ai_studio_sdk import AIStudio
   import urllib.request
   import zipfile   
   ```

1. Download [TensorBoard](https://www.tensorflow.org/tensorboard?hl=en). You will need it to view the training metrics:

   ```python
   def download_tensorboard(url):
       urllib.request.urlretrieve(url, "tensorboard.zip")
       with zipfile.ZipFile('tensorboard.zip', 'r') as zip_ref:
           zip_ref.extractall("tensorboard")
   ```

1. Download the fine-tuning data:

   ```python
   def local_path(path: str) -> pathlib.Path:
       return pathlib.Path(path)

   dataset_path = local_path("generations.jsonlines")
   print(dataset_path)

   print("Tuning dataset")
   print(dataset_path.read_text())
   ```

1. Create an SDK object containing the authorization parameters:

   ```python
   sdk = AIStudio(
       folder_id=os.environ['FOLDER_ID'], # Folder ID stored in the DataSphere secret.
       auth=os.environ['API_KEY'] # Service account API key stored in the DataSphere secret.
   )
   ```

1. Create a fine-tuning dataset and initiate its loading and validation:

   ```python
   dataset_draft = sdk.datasets.draft_from_path(
       task_type='TextToTextGeneration',
       path=dataset_path,
       upload_format='jsonlines',
       name='test-generations'
   )

   dataset = dataset_draft.upload()
   print('Dataset is being loaded and validated')
   print(f'New dataset {dataset=} \n')
   ```

1. Select the base model you want to fine-tune and run fine-tuning. In our example, it is YandexGPT Lite:

   ```python
   base_model = sdk.models.completions('yandexgpt-lite')

   tuning_task = base_model.tune_deferred(
       dataset,
       name=str(uuid.uuid4()),
       n_samples=10000 
   )
   
   print(f'Tuning started {tuning_task} \n')

   # Fine-tuning can last up to several hours.
   # Wait until the fine-tuning is complete to get a new model.
   new_model = tuning_task.wait()
   print(f'Tuning completed, new model = {new_model} \n')
   ```

1. Get your fine-tuned model:

   ```python
   # If you closed your notebook, you can use task_id to check the operation status.
   tuning_task = sdk.tuning.get(task_id)
   print(tuning_task.get_task_info())

   new_model = tuning_task.get_result()
   print({new_model})
   ```

1. Test your new model:

   ```python
   print('Sending queries to the new model\n')
   # Examples of queries to the model
   completion_result = new_model.run("What is your name?")
   print(f'{completion_result=} \n')

   # You can use the fine-tuned model by specifying its URI.
   tuned_uri = new_model.uri 
   model = sdk.models.completions(tuned_uri)

   completion_result = model.run("Where are you from?")
   print(f'{completion_result=} \n')
   ```

   The model will generate and return answers considering the fine-tuning.

1. Download fine-tuning metrics:

   ```python
   # Get the link with the data for TensorBoard and download the files.
   metrics_url = new_model.get_metrics_url()
   download_tensorboard(metrics_url)
   ```

## How to delete the resources you created {#clear-out}

To stop paying for the resources you created, [delete the project](../../datasphere/operations/projects/delete.md).