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 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 English \"what is 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 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 of sample requests for the classes specified in thelabels
field. Sample requests are provided as objects, each one containing one text request sample and the class to which such request 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 -X POST \ -H "Authorization: Bearer ${IAM_TOKEN}" \ -d "@<path_to_request_body_file>" \ "https://llm.api.cloud.yandex.net/foundationModels/v1/fewShotTextClassification"
In the response, the service will return classification results with certain
confidence
values for the probability of classifying the request 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 always equal to1
.