Рабочий вариант

This commit is contained in:
2026-01-31 20:19:44 +03:00
parent e210f483b7
commit c8980abe2b
1307 changed files with 1279 additions and 25 deletions

View File

@@ -34,6 +34,8 @@ class StubEmbeddingClient:
_GIGACHAT_BATCH_SIZE = 50
# GigaChat embeddings: max 514 tokens per input; Russian/English ~3 chars/token → truncate to stay under
_GIGACHAT_MAX_CHARS_PER_INPUT = 1200
class GigaChatEmbeddingClient:
@@ -57,16 +59,43 @@ class GigaChatEmbeddingClient:
return []
result: list[list[float]] = []
for i in range(0, len(texts_list), _GIGACHAT_BATCH_SIZE):
batch = texts_list[i : i + _GIGACHAT_BATCH_SIZE]
with GigaChat(
credentials=self._credentials,
verify_ssl_certs=self._verify_ssl_certs,
) as giga:
response = giga.embeddings(model=self._model, input=batch)
# Preserve order by index
by_index = {item.index: item.embedding for item in response.data}
result.extend(by_index[j] for j in range(len(batch)))
try:
for i in range(0, len(texts_list), _GIGACHAT_BATCH_SIZE):
raw_batch = texts_list[i : i + _GIGACHAT_BATCH_SIZE]
batch = [
t[: _GIGACHAT_MAX_CHARS_PER_INPUT] if len(t) > _GIGACHAT_MAX_CHARS_PER_INPUT else t
for t in raw_batch
]
with GigaChat(
credentials=self._credentials,
model=self._model,
verify_ssl_certs=self._verify_ssl_certs,
) as giga:
# API: embeddings(texts: list[str]) — single positional argument (gigachat 0.2+)
response = giga.embeddings(batch)
# Preserve order by index
by_index = {item.index: item.embedding for item in response.data}
result.extend(by_index[j] for j in range(len(batch)))
except Exception as e:
from gigachat.exceptions import ResponseError, RequestEntityTooLargeError
is_402 = (
isinstance(e, ResponseError)
and (getattr(e, "status_code", None) == 402 or "402" in str(e) or "Payment Required" in str(e))
)
if is_402:
raise ValueError(
"GigaChat: недостаточно средств (402 Payment Required). "
"Пополните баланс в кабинете GigaChat или отключите GIGACHAT_CREDENTIALS для stub-режима."
)
if isinstance(e, RequestEntityTooLargeError) or (
isinstance(e, ResponseError) and getattr(e, "status_code", None) == 413
):
raise ValueError(
"GigaChat: превышен лимит токенов на один запрос (413). "
"Уменьшите RAG_CHUNK_SIZE_LINES или RAG_CHUNK_SIZE в .env (текущий лимит ~514 токенов на чанк)."
)
raise
return result