yandex-cloud / python-sdk

Yandex.Cloud Python SDK
MIT License
83 stars 26 forks source link

Support for API key auth #80

Closed kuk closed 9 months ago

kuk commented 10 months ago

Trying to use YandexGPT with API key https://cloud.yandex.ru/docs/yandexgpt/api-ref/authentication. Does SDK support API key auth? In https://github.com/yandex-cloud/python-sdk/blob/master/yandexcloud/_channels.py#L26 I see support for token, service_account_key and iam_token

ildik commented 9 months ago

Hi, @kuk !

SDK support OAUTH Token, but you need to add folder_id parameter to use YandexGPT I found how to do it, just add it to the metadata :)

See example:

import os
import grpc
import yandexcloud

from google.protobuf.wrappers_pb2 import DoubleValue, Int64Value
from yandex.cloud.ai.llm.v1alpha.llm_service_pb2_grpc import TextGenerationServiceStub
from yandex.cloud.ai.llm.v1alpha.llm_service_pb2 import InstructRequest, InstructResponse
from yandex.cloud.ai.llm.v1alpha.llm_pb2 import GenerationOptions

metadata = (("x-folder-id", os.getenv("YANDEX_FOLDER_ID")),)

sdk = yandexcloud.SDK(token=os.getenv("YANDEX_OAUTH_TOKEN"))
text_gen = sdk.client(TextGenerationServiceStub)

request = InstructRequest(
    model="general",
    generation_options=GenerationOptions(
        temperature=DoubleValue(value=0.8),
        max_tokens=Int64Value(value=7400),
    ),
    instruction_text="Полностью перепиши стихотворение в стиль Маяковского. Срочно!",
    request_text="Жили у бабуси\nДва весёлых гуся,\nОдин - серый,\nдругой - белый,\nДва весёлых гуся.",
)

response: InstructResponse = text_gen.Instruct(request=request, metadata=metadata)
print(list(response))
kuk commented 9 months ago

The question is specifically about API key auth. In code above auth is done via iam token

achmedzhanov commented 9 months ago

in any case, forlder_id is also needed, without it the GPT API cannot be used

MAnyKey commented 9 months ago

API keys are intended to be used by simple clients that can't have any logic for auth. SDK is not one of them, so API keys are not supported intentionally, since they significantly weaken the security.