Generating an image using YandexART
With YandexART, you can generate images in asynchronous mode. In response to an asynchronous request, the model will return an Operation object containing an operation ID, which you can use to track its execution and get the result after generation is completed. Generating a result in asynchronous mode can take from a couple of minutes to several hours.
Getting started
To run sample requests using the API, install:
Get API authentication credentials as described on the Authentication with the Yandex Foundation Models API page.
Generate an image
Note
To improve the quality of responses, YandexART logs user prompts. Do not use sensitive information and personal data in your prompts.
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.,
prompt.json
:{ "modelUri": "art://<folder_ID>/yandex-art/latest", "generationOptions": { "seed": "1863", "aspectRatio": { "widthRatio": "2", "heightRatio": "1" } }, "messages": [ { "weight": "1", "text": "a pattern of pastel colored succulents of various sorts, hd full wallpaper, sharp focus, many intricate details, picture depth, top view" } ] }
Where:
modelUri
: YandexART model ID which contains a Yandex Cloud folder ID.seed
: Generation seed.text
: Text description of the image to use for generation.weight
: Text description weight. If a request contains more than one description, their individual impact will be calculated based on weight, with the sum of all weights equal to 1.aspectRatio
: (Optional) Aspect ratio of the generated image:widthRatio
: Width (default value: 1).heightRatio
: Height (default value: 1).
-
To send a request to the neural network using the ImageGenerationAsync.generate method, run the following command:
curl \ --request POST \ --header "Authorization: Bearer <IAM_token_value>" \ --data "@prompt.json" \ "https://llm.api.cloud.yandex.net/foundationModels/v1/imageGenerationAsync"
Where:
<IAM_token_value>
: Your account's IAM token.prompt.json
: JSON file with request parameters.
In response, the service will return the Operation object:
{"id":"fbveu1sntj**********","description":"","createdAt":null,"createdBy":"","modifiedAt":null,"done":false,"metadata":null}
Save the operation
id
you get in the response. -
Generating an image may take from a few seconds to a few hours. Wait for a while and send a request to
https://llm.api.cloud.yandex.net:443/operations/<operation_ID>
to get the generation result. When the image is ready, you will get the result in a Base64-encoded file namedimage.jpeg
.curl --request GET --header "Authorization: Bearer <IAM_token_value>" https://llm.api.cloud.yandex.net:443/operations/<operation_ID> | jq -r '.response | .image' | base64 -d > image.jpeg
Where:
<IAM_token_value>
: IAM token you obtained when getting started.<operation_ID>
:id
field value obtained in response to the generation request.