yandex_gpt_rest_api 0.5.2 copy "yandex_gpt_rest_api: ^0.5.2" to clipboard
yandex_gpt_rest_api: ^0.5.2 copied to clipboard

A library for using YandexGPT API, such as generating text and obtain embeddings.

yandex_gpt_rest_api #

Test CI codecov

Getting started #

Create YandexGptApi instance.


// For passing BaseOptions or Dio use other constructors.
final api = YandexGptApi(
  token: AuthToken.api("your_token"), // or AuthToken.iam
  // Not necessary, by default uses catalog from AuthToken account.
  catalog: "catalog_id?",
);

Now you can use the YandexGPT API.

API calls #

The names of methods YandexGptApi are same to the names of API methods.

Available API calls:

Text Generation

Generate sync text #

void main() async {
  final response = await api.generateText(
    TextGenerationRequest(
      model: GModel.yandexGpt('folder_id'),
      messages: const [
        Message.system("Some joke"),
        Message.user("Generate joke"),
      ],
    ),
  );
  print(response.alternatives.first.message);
  print(response.usage.totalTokens);
}

Generate async text #

void main() async {
  final response = await api.generateAsyncText(
    TextGenerationRequest(
      model: GModel.yandexGpt('folder_id'),
      messages: const [
        Message.system("Some joke"),
        Message.user("Generate joke"),
      ],
    ),
  );
  print(response.done);
}
Tokenize

Tokenize completion #

void main() async {
  final response = await api.tokenizeCompletion(
    TextGenerationRequest(
      model: GModel.yandexGpt('folder_id'),
      messages: const [
        Message.system("Some joke"),
        Message.user("Generate joke"),
      ],
    ),
  );
  print(response.tokens.length);
}

Tokenize text #

void main() async {
  final response = await api.tokenizeText(
    TokenizeTextRequest(
      model: GModel.yandexGpt('folder_id'),
      text: 'some_response_text',
    ),
  );
  print(response.tokens.length);
}
Embeddings

Text embedding #

void main() async {
  final response = await api.getTextEmbedding(
    EmbeddingRequest(
      model: VModel.documentation('folder_id'),
      text: 'Some text',
    ),
  );
  print(response.embedding);
}

Handling errors #

It is enough to catch an error of type ApiError.

try {
  await api.generateText(/*request*/);
} on ApiError catch (e) {
  // handle ApiErrors
} on DioException catch (e) {
  // Handle network errors
}

If you need information about the error:

try {
  await api.generateText(/*request*/);
} on DetailedApiError catch (e) {
  // Do some
} on ShortApiError catch (e) {
  // Do some
} on DioException catch (e) {
  // Handle network errors
}

Cancel requests #

To cancel requests use Dio CancelToken by passing API requests with cancelToken param.

The handling cancellation is similar to the example from the Dio doc.

3
likes
150
points
74
downloads

Publisher

unverified uploader

Weekly Downloads

A library for using YandexGPT API, such as generating text and obtain embeddings.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

dio

More

Packages that depend on yandex_gpt_rest_api