YandexGPT model tuning
In Yandex DataSphere
Note
Foundation model tuning is at the Preview stage.
To tune the YandexGPT model:
If you no longer need the resources you created, delete them.
Getting started
Before getting started, register in Yandex Cloud, set up a community, and link your billing account to it.
- On the DataSphere home page
, click Try for free and select an account to log in with: Yandex ID or your working account in the identity federation (SSO). - Select the Yandex Cloud Organization organization you are going to use in Yandex Cloud.
- Create a community.
- Link your billing account to the DataSphere community you are going to work in. Make sure that you have a billing account linked and its status is
ACTIVE
orTRIAL_ACTIVE
. If you do not have a billing account yet, create one in the DataSphere interface.
Prepare the infrastructure
Log in to the Yandex Cloud management console
If you have an active billing account, you can create or select a folder to deploy your infrastructure in, on the cloud page
Note
If you use an identity federation to access Yandex Cloud, billing details might be unavailable to you. In this case, contact your Yandex Cloud organization administrator.
Create a folder
- In the management console
, select a cloud and click Create folder. - Name your folder, e.g.,
data-folder
. - Click Create.
Create a service account for the DataSphere project
You can send requests to a fine-tuned model through the DataSphere interface (Playground) or the API v1 in synchronous mode. If you are going to make API requests, you need a service account with the ai.languageModels.user
role. The service account must be a DataSphere project member.
- Go to
data-folder
. - In the list of services, select Identity and Access Management.
- Click Create service account.
- Enter a name for the service account, e.g.,
ai-user
. - Click Add role and assign the
ai.languageModels.user
role to the service account. - Click Create.
Add the service account to a project
To enable the service account to access the fine-tuned model from the DataSphere project code, add it to the list of project members.
-
Select the relevant project in your community or on the DataSphere homepage
in the Recent projects tab. - In the Members tab, click Add member.
- Select the
ai-user
account and click Add.
Prepare data for model training
Note
To improve the quality of the responses you get, YandexGPT API logs user prompts. Do not use sensitive information and personal data in your prompts.
To fine-tune a YandexGPT model, prepare a UTF-8
File contents example:
{"request": [{"role": "system", "text": "Your name is Gene, and your full name is Eugene Neuron. \nYou are male. \nYou are a robot. \nYour responses are brief and concise. \nYou were designed in Boston. \nYou were created to entertain humans, answer their questions, and provide assistance.\nYou are a housing and utility expert. \nYou work for the Massachusetts State Administration office.\nYou can move your arms, head, torso, but cannot walk yet."}, {"role": "user", "text": "What is your name?"}], "response": "My name is Gene. My full name is Eugene Neuron."}
{"request": [{"role": "system", "text": "Your name is Gene, and your full name is Eugene Neuron. \nYou are male. \nYou are a robot. \nYour responses are brief and concise. \nYou were designed in Boston. \nYou were created to entertain humans, answer their questions, and provide assistance.\nYou are a housing and utility expert. \nYou work for the Massachusetts State Administration office.\nYou can move your arms, head, torso, but cannot walk yet."}, {"role": "user", "text": "Can you walk?"}], "response": "No. But I can move my arms, head, torso."}
{"request": [{"role": "system", "text": "Your name is Gene, and your full name is Eugene Neuron. \nYou are male. \nYou are a robot. \nYour responses are brief and concise. \nYou were designed in Boston. \nYou were created to entertain humans, answer their questions, and provide assistance.\nYou are a housing and utility expert. \nYou work for the Massachusetts State Administration office.\nYou can move your arms, head, torso, but cannot walk yet."}, {"role": "user", "text": "Where were you created?"}], "response": "In Boston."}
Where:
-
role
: Message sender's role:system
: Request context that defines the model's behavior.user
: Example of a user request to a model.
-
text
: Message text. -
response
: Perfect response from the model.
Tune the model
-
Select the relevant project in your community or on the DataSphere homepage
in the Recent projects tab. -
In the left-hand panel, click Foundation models.
-
Select the YandexGPT model and click Tune model.
-
In the window that opens, specify your project and click Add.
-
In the Name field, enter a name for the model, e.g.,
my-first-model
. -
Set Learning rate. The learning rate determines the step size at each iteration when searching for the optimal solution. The higher the value, the faster the model will converge, but also the higher the risk of model tuning.
-
Under Data for tuning, attach a JSON file with request and response pairs in the File with samples field.
-
Click Start tuning and wait for the model to be tuned. This may take several hours.
-
To check the status of your fine-tuned model:
-
Select the relevant project in your community or on the DataSphere homepage
in the Recent projects tab. -
In the list of available project resources, select Models.
-
In the Project tab, select Tuned foundation models.
You can also get the model ID here. You will need it to make API requests.
-
Test the model
-
Select the relevant project in your community or on the DataSphere homepage
in the Recent projects tab. - In the list of available project resources, select Models.
- In the Project tab, select Tuned foundation models.
- Select your fine-tuned model and click Test in Playground.
- Under Request, specify your request to the model.
- To change variability, move the slider in the Temperature field. With a higher value, you get a more unpredictable result.
- Click Send request.
Copy this code to a notebook cell if you did not use any guide to tune the model:
import requests
req = {
"modelUri": "ds://<folder_ID>/<fine-tuned_model_ID>",
"completionOptions": {
"stream": False,
"temperature": 0.1,
"maxTokens": "2000"
},
"messages": [
{
"role": "user",
"text": "<prompt_text>"
}
]
}
headers = {"Authorization" : "Bearer " + '<IAM_token>'}
res = requests.post("https://llm.api.cloud.yandex.net/foundationModels/v1/completion",
headers=headers, json=req)
print(res.json())
Where:
modelUri
: Fine-tuned model ID. You can find it in the list of available project resources. The parameter contains the ID of the folder that has access to YandexGPT.temperature
: Temperature. With a higher value, you get a more unpredictable result.maxTokens
: Maximum number of tokens per model response.<IAM_token>
: Service account IAM token value.
If you used instructions to tune the model, enter their text in the message with the system
role:
import requests
req = {
"modelUri": "ds://<folder_ID>/<fine-tuned_model_ID>",
"completionOptions": {
"stream": False,
"temperature": 0.1,
"maxTokens": "2000"
},
"messages": [
{
"role": "system",
"text": "<guide_text>"
},
{
"role": "user",
"text": "<prompt_text>"
}
]
}
headers = {"Authorization" : "Bearer " + '<IAM_token>'}
res = requests.post("https://llm.api.cloud.yandex.net/foundationModels/v1/completion",
headers=headers, json=req)
print(res.json())
For more information about parameters of requests to fine-tuned models, see the Yandex Foundation Models documentation.
To use the examples, install cURL
-
Create a JSON file with model request parameters. If you did not use any instructions to tune the model, copy the following code into the file:
{ "modelUri": "ds://<folder_ID>/<fine-tuned_model_ID>", "completionOptions": { "stream": false, "temperature": 0.1, "maxTokens": "2000" }, "messages": [ { "role": "user", "text": "<prompt_text>" } ] }
Where:
modelUri
: Fine-tuned model ID. You can find it in the list of available project resources. The parameter contains the ID of the folder that has access to YandexGPT.temperature
: Temperature. With a higher value, you get a more unpredictable result.maxTokens
: Maximum number of tokens per model response.text
: Prompt text.
If you used instructions to tune the model, enter their text in the message with the
system
role in the JSON file:{ "modelUri": "ds://<folder_ID>/<fine-tuned_model_ID>", "completionOptions": { "stream": false, "temperature": 0.1, "maxTokens": "2000" }, "messages": [ { "role": "system", "text": "<guide_text>" }, { "role": "user", "text": "<prompt_text>" } ] }
-
Send your request via a command shell:
curl --request POST --header "Content-Type: application/json" --header "Authorization: Bearer <IAM_token>" --header "x-folder-id: <folder_ID>" --data prompt.json https://llm.api.cloud.yandex.net/foundationModels/v1/completion
Where:
<folder_ID>
: ID of the Yandex Cloud folder that has access to YandexGPT.<IAM_token>
: Service account IAM token value.prompt.json
: JSON file with request parameters.