Sending a request in synchronous mode
In text generation tasks that involve short responses or chat mode model operation, the synchronous request mode proves the most effective.
If you are facing a large-scale text generation task, e.g., to process massive documents, the background request mode can be a better option.
Getting started
To use an example:
-
Create a service account and assign it the
ai.languageModels.userrole. -
Get and save the service account's API key.
Note
If you are using Windows
, we recommend installing the WSL shell first and then using it to perform other operations. -
Install Python 3.10
or higher. -
Install Python venv
to create isolated virtual environments in Python. -
Create a new Python virtual environment and activate it:
python3 -m venv new-env source new-env/bin/activate -
Install the OpenAI library
for Python:pip install openai
Send a request to the model
-
Create a file named
index.pyand add the following code to it:import openai YANDEX_CLOUD_MODEL = "yandexgpt-lite" client = openai.OpenAI( api_key=YANDEX_CLOUD_API_KEY, base_url="https://rest-assistant.api.cloud.yandex.net/v1", project= YANDEX_CLOUD_FOLDER ) response = client.responses.create( model=f"gpt://{YANDEX_CLOUD_FOLDER}/{YANDEX_CLOUD_MODEL}", input="Come up with three off-beat ideas for a travel startup.", temperature=0.8, max_output_tokens=1500 ) print(response.output[0].content[0].text) -
Save authentication data to environment variables:
export YANDEX_CLOUD_FOLDER=<folder_ID> export YANDEX_CLOUD_API_KEY=<API_key> -
Run the file you created:
python index.pyResponse example:
1. **Travel search and management platform with astrology factored in.** The service will help travelers choose a destination, trip dates, and activities based on their astrological sign or individual natal chart. This may include recommendations on the best periods for traveling, places that are in tune with your sign's energy, and the choice of cultural and natural attractions that align with your astrological preferences. 2. **Virtual travel guide with augmented reality features.** This is an app or service allowing users to "come across" a virtual guide wherever they are via augmented reality (AR). This guide can share facts if interest about the location, show hidden sightseeing attractions, and suggest routes for your hikes. The user can ask questions and get answers in real time with the AR technology responsible for visualization. 3. **AI-based travel management service.** A platform that relies on AI algorithms to personalize your travels. AI can analyze the user’s preferences, budget, season, etc., and propose optimized routes, including flights, transfers, accommodations, and activities. The service can also propose alternatives if there is a change of schedule or other unforeseen events.
See also
- Sending a request in background mode
- Overview of Yandex AI Studio AI models
- Examples of working with ML SDK on GitHub