Yandex Cloud
Search
Contact UsGet started
  • Blog
  • Pricing
  • Documentation
  • All Services
  • System Status
    • Featured
    • Infrastructure & Network
    • Data Platform
    • Containers
    • Developer tools
    • Serverless
    • Security
    • Monitoring & Resources
    • ML & AI
    • Business tools
  • All Solutions
    • By industry
    • By use case
    • Economics and Pricing
    • Security
    • Technical Support
    • Customer Stories
    • Gateway to Russia
    • Cloud for Startups
    • Education and Science
  • Blog
  • Pricing
  • Documentation
Yandex project
© 2025 Yandex.Cloud LLC
Yandex Foundation Models
    • All tutorials
    • Disabling request logging
    • Getting an API key
    • Batch processing
      • Using prompt-based classifiers
      • Using fine-tuned classifiers
  • Yandex Cloud ML SDK
  • Compatibility with OpenAI
  • Access management
  • Pricing policy
  • Public materials
  • Release notes

In this article:

  • Getting started
  • Send a request to the classifier
  1. Step-by-step guides
  2. Classification
  3. Using prompt-based classifiers

Using prompt-based classifiers based on YandexGPT

Written by
Yandex Cloud
Updated at April 11, 2025
  • Getting started
  • Send a request to the classifier

Yandex Foundation Models provides YandexGPT prompt-based classifiers of these two types: zero-shot and few-shot. To send a request to a prompt-based classifier, use the fewShotClassify text classification API method or Yandex Cloud ML SDK.

Getting started

To use the examples:

SDK
cURL
  1. Create a service account and assign the ai.languageModels.user role to it.

  2. Get the service account API key and save it.

    The following examples use API key authentication. Yandex Cloud ML SDK also supports IAM token and OAuth token authentication. For more information, see Authentication in Yandex Cloud ML SDK.

  3. Use the pip package manager to install the ML SDK library:

    pip install yandex-cloud-ml-sdk
    
  1. Get API authentication credentials as described in Authentication with the Yandex Foundation Models API.

  2. To use the examples, install cURL.

Send a request to the classifier

SDK
cURL

This code includes two independent examples illustrating different uses of the SDK interface:

  • Example 1: Request to the zero-shot classifier.
  • Example 2: Request to the few-shot classifier.
  1. Create a file named classify.py and paste the following code into it:

    #!/usr/bin/env python3
    # pylint: disable=duplicate-code
    
    from __future__ import annotations
    from yandex_cloud_ml_sdk import YCloudML
    
    request_text = 'translate into Russian \"what\'s the weather like in London?\"'
    
    
    def main():
        sdk = YCloudML(
            folder_id="<folder_ID>",
            auth="<API_key>",
        )
    
        # Sample 1: Zero-shot classification
        model = sdk.models.text_classifiers("yandexgpt").configure(
            task_description="determine the intent type",
            labels=["translation", "alarm", "weather"],
        )
    
        result = model.run(request_text)
    
        print("Zero-shot classification:")
    
        for prediction in result:
            print(prediction)
    
        # Sample 2: Few-shot classification
        model = model.configure(
            task_description="determine the intent type",
            labels=["translation", "alarm", "weather"],
            samples=[
                {"text": "set an alarm", "label": "alarm"},
                {"text": "weather for tomorrow", "label": "weather"},
                {"text": "translate the phrase \"set an alarm\"", "label": "translation"},
            ],
        )
    
        result = model.run(request_text)
    
        print("Few-shot classification:")
    
        for prediction in result:
            print(prediction)
    
    
    if __name__ == "__main__":
        main()
    

    Where:

    • request_text: Message text.

      As input data for a request, Yandex Cloud ML SDK can accept a string, a dictionary, an object of the TextMessage class, or an array containing any combination of these data types. For more information, see Yandex Cloud ML SDK usage.

    • <folder_ID>: ID of the folder in which the service account was created.

    • <API_key>: Service account API key you got earlier required for authentication in the API.

      The following examples use API key authentication. Yandex Cloud ML SDK also supports IAM token and OAuth token authentication. For more information, see Authentication in Yandex Cloud ML SDK.

    For more information about accessing classifier models, see Accessing models.

  2. Run the created file:

    python3 classify.py
    

    In response, the service will return the classification results for both examples with the confidence values for the probability of classifying the query text into each class:

    TextClassificationLabel(label='translation', confidence=0.9999947046656092)
    TextClassificationLabel(label='alarm', confidence=6.01089130732152e-09)
    TextClassificationLabel(label='weather', confidence=4.289328794822987e-06)
    Few-shot classification:
    TextClassificationLabel(label='translation', confidence=0.9999989886405171)
    TextClassificationLabel(label='alarm', confidence=4.4148929001561725e-09)
    TextClassificationLabel(label='weather', confidence=6.945601458737463e-09)
    

The example below is intended to be run in MacOS and Linux. To run it in Windows, see how to work with Bash in Microsoft Windows.

  1. Create a file with the request body, e.g., body.json:

    Zero-shot classifier
    {
      "modelUri": "cls://<folder_ID>/yandexgpt/latest",
      "text": "5:0",
      "task_description": "Categorize an article by its title",
      "labels": [
        "culture",
        "technologies",
        "sports"
      ]
    }
    

    Where:

    • modelUri: ID of the model that will be used to classify the message. This parameter contains the Yandex Cloud folder ID.

    • text: Message text.

    • taskDescription: Text description of the task for the classifier.

    • labels: Array of classes.

      Give meaningful names to label classes: this is essential for correct classification results. For example, use chemistry and physics rather than chm and phs for class names.

    Few-shot classifier
    {
      "modelUri": "cls://<folder_ID>/yandexgpt/latest",
      "text": "translate into Russian \"what's the weather like in London?\"",
      "task_description": "determine the intent type",
      "labels": [
        "translation",
        "alarm",
        "weather"
      ],
      "samples": [
        {
          "text": "set an alarm",
          "label": "alarm"
        },
        {
          "text": "weather for tomorrow",
          "label": "weather"
        },
        {
          "text": "translate the phrase \"set an alarm\"",
          "label": "translation"
        }
      ]
    }
    

    Where:

    • modelUri: ID of the model that will be used to classify the message. This parameter contains the Yandex Cloud folder ID.

    • text: Message text.

    • taskDescription: Text description of the task for the classifier.

    • labels: Array of classes.

      Give meaningful names to label classes: this is essential for correct classification results. For example, use chemistry and physics rather than chm and phs for class names.

    • samples: Array with examples of prompts for the classes specified in the labels field. Examples of prompts are provided as objects, each containing an example of a text query and the class to which such query should belong.

  2. Send a request to the classifier by running the following command:

    export IAM_TOKEN=<IAM_token>
    curl \
      --request POST \
      --header "Authorization: Bearer ${IAM_TOKEN}" \
      --data "@<path_to_file_with_request_body>" \
      "https://llm.api.cloud.yandex.net/foundationModels/v1/fewShotTextClassification"
    

    Note

    The https://llm.api.cloud.yandex.net/foundationModels/v1/fewShotTextClassification endpoint only works with prompt-based classifiers. For fine-tuned classifiers, use https://llm.api.cloud.yandex.net:443/foundationModels/v1/textClassification.

    In response, the service will return the classification results with the confidence values for the probability of classifying the query text into each class:

    Zero-shot classifier
    {
      "predictions": [
        {
          "label": "culture",
          "confidence": 2.2111835562554916e-7
        },
        {
          "label": "technologies",
          "confidence": 0.0003487042267806828
        },
        {
          "label": "sports",
          "confidence": 0.9996510744094849
        }
      ],
      "modelVersion": "07.03.2024"
    }
    
    Few-shot classifier
    {
      "predictions": [
        {
          "label": "translation",
          "confidence": 0.9357050657272339
        },
        {
          "label": "alarm",
          "confidence": 0.00061939493753016
        },
        {
          "label": "weather",
          "confidence": 0.06367553025484085
        }
      ],
      "modelVersion": "07.03.2024"
    }
    

The sum of the confidence values for all classes is always 1.

See also

  • Classifiers based on YandexGPT
  • Examples of working with ML SDK on GitHub

Was the article helpful?

Previous
Batch processing
Next
Using fine-tuned classifiers
Yandex project
© 2025 Yandex.Cloud LLC