Sending a request to an instance
Follow this guide to send a request to an instance in AI Studio.
You can get the request code and the model ID in to the management console
Getting started
To use an example:
- Create a service account and assign the
ai.languageModels.userrole to it. -
Get and save the service account's API key with
yc.ai.foundationModels.executefor its scope.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.
Note
If you are using Windows
, we recommend installing the WSL shell first and using it to proceed. -
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 -
Use the pip
package manager to install the ML SDK library:pip install yandex-cloud-ml-sdk
Send your request
-
Create a file named
test_ml.pyand paste the following code into it:from yandex_cloud_ml_sdk import YCloudML sdk = YCloudML( folder_id="<folder_ID>", auth="<API_key_or_IAM_token>" ) model = sdk.chat.completions("<model_ID>", model_version="<version_ID>") model = model.configure(temperature=0.3) result = model.run( [ { "role": "system", "text": "You are a smart assistant" }, { "role": "user", "text": "Hi! What fields of science did Albert Einstein study?", }, ] ) for alternative in result: print(result[0].text) # Uncomment the next line to get the full response: # print(alternative)Where:
folder_id: Yandex Cloud folder ID.auth: Service account API key or IAM token you got earlier for authentication in the API.<model_ID>: ID of the dedicated instance model.model_version: Model version ID.temperature: With a higher temperature, you get more creative and randomized responses from the model. Its values range from0to999999, inclusive.
-
Run the file you created:
python3 test_ml.pyResult:
Albert Einstein was an outstanding physicist, whose works in theoretical physics, theoretical mechanics, and philosophy of science became fundamental. He dedicated his career to studying the fundamentals of the universe, including the theory of relativity, both special and general. Additionally, Albert Einstein studied:\n\n* thermodynamics,\n* statistical mechanics,\n* electromagnetism,\n* quantum theory,\n* special relativity, and more.\n\nHis general relativity works found wide recognition and had a profound influence on the development of modern physics.