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 or Yandex Cloud ML SDK.
Getting started
To use the examples:
-
Create a service account and assign the
ai.languageModels.user
role to it. -
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.
-
Use the pip
package manager to install the ML SDK library:pip install yandex-cloud-ml-sdk
-
Get API authentication credentials as described in Authentication with the Yandex Foundation Models API.
-
To use the examples, install cURL
.
Send a request to the classifier
This code includes two independent examples illustrating different uses of the SDK interface:
-
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.
-
-
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.
-
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, usechemistry
andphysics
rather thanchm
andphs
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, usechemistry
andphysics
rather thanchm
andphs
for class names. -
samples
: Array with examples of prompts for the classes specified in thelabels
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.
-
-
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, usehttps://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