Sending a basic request

The Completions API supports such parameters as temperature, max_tokens, stream, response_format.

To use the example, you will need a service account with the ai.languageModels.user role and an API key with the yc.ai.foundationModels.execute scope. The API key you can create in AI Studio has these permissions. Refer to the Getting started section for an example of how to configure your runtime environment.

import openai
        
        YANDEX_API_KEY = "<API_key>"
        YANDEX_FOLDER_ID = "<folder_ID>"
        YANDEX_MODEL = "yandexgpt/latest"
        
        client = openai.OpenAI(
            api_key=YANDEX_API_KEY,
            base_url="https://ai.api.cloud.yandex.net/v1",
            project=YANDEX_FOLDER_ID,
        )
        
        response = client.chat.completions.create(
            model=f"gpt://{YANDEX_FOLDER_ID}/{YANDEX_MODEL}",
            messages=[
                {"role": "system", "content": "You are a very smart assistant."},
                {"role": "user", "content": "What can large language models do?"}
            ],
            max_tokens=2000,
            temperature=0.3,
            stream=True,
        )
        
        for chunk in response:
            if chunk.choices[0].delta.content is not None:
                print(chunk.choices[0].delta.content, end="")
        

Where:

  • YANDEX_API_KEY: API key for access to AI Studio.
  • YANDEX_FOLDER_ID: Service account folder ID.
  • YANDEX_MODEL: Model name to handle the request.

Result example:

Large language models (LLM) can generate text, answer questions, translate,
        summarize documents, write code, and much more. They are trained on large volumes of text
        data and are capable of addressing a broad range of tasks related to natural language processing.
        
import OpenAI from "openai";
        
        const YANDEX_API_KEY = "<API_key>";
        const YANDEX_FOLDER_ID = "<folder_ID>";
        const YANDEX_MODEL = "yandexgpt/latest";
        
        const client = new OpenAI({
          apiKey: YANDEX_API_KEY,
          baseURL: "https://ai.api.cloud.yandex.net/v1",
          project: YANDEX_FOLDER_ID,
        });
        
        const completion = await client.chat.completions.create({
          model: `gpt://${YANDEX_FOLDER_ID}/${YANDEX_MODEL}`,
          messages: [
            { role: "system", content: "You are a very smart assistant." },
            { role: "user", content: "What can large language models do?" },
          ],
          max_tokens: 2000,
          temperature: 0.3,
        });
        
        console.log(completion.choices[0].message.content);
        

Where:

  • YANDEX_API_KEY: API key for access to AI Studio.
  • YANDEX_FOLDER_ID: Service account folder ID.
  • YANDEX_MODEL: Model name to handle the request.

Result example:

Large language models (LLM) can generate text, answer questions, translate,
        summarize documents, write code, and much more. They are trained on large volumes of text
        data and are capable of addressing a broad range of tasks related to natural language processing.
        
package main
        
        import (
            "context"
            "fmt"
            "os"
        
            "github.com/openai/openai-go"
            "github.com/openai/openai-go/option"
        )
        
        func main() {
            yandexAPIKey := os.Getenv("YANDEX_API_KEY")
            yandexFolderID := os.Getenv("YANDEX_FOLDER_ID")
            yandexModel := "yandexgpt/latest"
        
            client := openai.NewClient(
                option.WithAPIKey(yandexAPIKey),
                option.WithBaseURL("https://ai.api.cloud.yandex.net/v1"),
                option.WithHeader("OpenAI-Project", yandexFolderID),
            )
        
            completion, err := client.Chat.Completions.New(
                context.Background(),
                openai.ChatCompletionNewParams{
                    Model: fmt.Sprintf("gpt://%s/%s", yandexFolderID, yandexModel),
                    Messages: []openai.ChatCompletionMessageParamUnion{
                        openai.SystemMessage("You are a very smart assistant."),
                        openai.UserMessage("What can large language models do?"),
                    },
                    MaxTokens:   openai.Int(2000),
                    Temperature: openai.Float(0.3),
                },
            )
            if err != nil {
                fmt.Fprintf(os.Stderr, "Error: %v\n", err)
                os.Exit(1)
            }
        
            fmt.Println(completion.Choices[0].Message.Content)
        }
        

Where:

  • YANDEX_API_KEY: API key for access to AI Studio.
  • YANDEX_FOLDER_ID: Service account folder ID.
  • yandexModel: Model name to handle the request.

Result example:

Large language models (LLM) can generate text, answer questions, translate,
        summarize documents, write code, and much more. They are trained on large volumes of text
        data and are capable of addressing a broad range of tasks related to natural language processing.
        
curl https://ai.api.cloud.yandex.net/v1/chat/completions \
          --header "Content-Type: application/json" \
          --header "Authorization: Api-Key <API_key>" \
          --header "OpenAI-Project: <folder_ID>" \
          --data '{
            "model": "gpt://<folder_ID>/yandexgpt/latest",
            "messages": [
              {
                "role": "system",
                "content": "You are a very smart assistant."
              },
              {
                "role": "user",
                "content": "What can large language models do?"
              }
            ],
            "max_tokens": 2000,
            "temperature": 0.3
          }'
        

Where:

  • <API_key>: API key for access to AI Studio.
  • <folder_ID>: Service account folder ID.

Result example:

{
          "id": "chatcmpl-a1b2c3d4-...",
          "object": "chat.completion",
          "choices": [
            {
              "index": 0,
              "message": {
                "role": "assistant",
                "content": "Large language models (LLMs) can generate text, answer questions, translate, summarize documents, write code, and much more."
              },
              "finish_reason": "stop"
            }
          ],
          "model": "gpt://b1go3el0d8fs********/yandexgpt/latest"
        }
        
from yandex_ai_studio_sdk import AIStudio
        
        YANDEX_API_KEY = "<API_key>"
        YANDEX_FOLDER_ID = "<folder_ID>"
        YANDEX_MODEL = "yandexgpt"
        
        sdk = AIStudio(
            folder_id=YANDEX_FOLDER_ID,
            auth=YANDEX_API_KEY,
        )
        
        model = sdk.chat.completions(YANDEX_MODEL)
        model = model.configure(temperature=0.3, max_tokens=2000)
        
        result = model.run([
            {"role": "system", "text": "You are a very smart assistant."},
            {"role": "user", "text": "What can large language models do?"},
        ])
        
        print(result.text)
        

Where:

  • YANDEX_API_KEY: API key for access to AI Studio.
  • YANDEX_FOLDER_ID: Service account folder ID.
  • YANDEX_MODEL: Model name to handle the request.

Result example:

Large language models (LLM) can generate text, answer questions, translate,
        summarize documents, write code, and much more. They are trained on large volumes of text
        data and are capable of addressing a broad range of tasks related to natural language processing.
        

See also