Using prompt-based classifiers based on YandexGPT
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.
Getting started
Get API authentication credentials as described in Authentication with the Yandex Foundation Models API.
Send a request to the classifier
To send a request to the classifier:
-
Create a file with the request body, e.g.,
body.json
:Zero-shot classifierFew-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. The parameter contains the Yandex Cloud folder ID. -
text
: Text content of the message. -
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, usechemistry
andphysics
rather thanchm
andphs
for class names.
{ "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. The parameter contains the Yandex Cloud folder ID. -
text
: Text content of the message. -
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, usechemistry
andphysics
rather thanchm
andphs
for class names. -
samples
: Array with examples of requests for the classes specified in thelabels
field. Examples of requests are provided as objects, each containing one example of a text query and the class to which such query should belong.
-
-
Send a request to the classifier by running the following command:
BashTo use the examples, install cURL
.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.
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, usehttps://llm.api.cloud.yandex.net:443/foundationModels/v1/textClassification
.In the response, the service will return classification results with certain
confidence
values for the probability of classifying the query text into each one of the classes:Zero-shot classifierFew-shot classifier{ "predictions": [ { "label": "culture", "confidence": 2.2111835562554916e-7 }, { "label": "technologies", "confidence": 0.0003487042267806828 }, { "label": "sports", "confidence": 0.9996510744094849 } ], "modelVersion": "07.03.2024" }
{ "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 always1
.