Sending a request to an instance

Follow this guide to send a request to an instance in AI Studio.

You also can get the request code and model ID in the AI Studio interface.

Getting started

To use an example:

  1. Create a service account and assign it the ai.languageModels.user role.
  2. Get and save the service account's API key with yc.ai.foundationModels.execute for 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.

  3. Install Python 3.10 or higher.

  4. Optionally, install Python venv to create isolated virtual environments in Python.

  5. Optionally, create a new Python virtual environment and activate it:

    python3 -m venv new-env
            source new-env/bin/activate
            
  6. Use the pip package manager to install the ML SDK library:

    pip install yandex-ai-studio-sdk
            

Send your request

  1. Create a file named test_ml.py and paste the following code into it:

    from yandex_ai_studio_sdk import AIStudio
            
            sdk = AIStudio(
                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 from 0 to 999999, inclusive.
  2. Run the file you created:

    python3 test_ml.py
            

    Result:

    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.