Фиксация изменений
This commit is contained in:
Binary file not shown.
@@ -1,5 +1,8 @@
|
||||
import time
|
||||
|
||||
import requests
|
||||
|
||||
from app.core.constants import MAX_RETRIES
|
||||
from app.modules.shared.gigachat.errors import GigaChatError
|
||||
from app.modules.shared.gigachat.settings import GigaChatSettings
|
||||
from app.modules.shared.gigachat.token_provider import GigaChatTokenProvider
|
||||
@@ -19,23 +22,7 @@ class GigaChatClient:
|
||||
{"role": "user", "content": user_prompt},
|
||||
],
|
||||
}
|
||||
try:
|
||||
response = requests.post(
|
||||
f"{self._settings.api_url.rstrip('/')}/chat/completions",
|
||||
json=payload,
|
||||
headers={
|
||||
"Authorization": f"Bearer {token}",
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
timeout=90,
|
||||
verify=self._settings.ssl_verify,
|
||||
)
|
||||
except requests.RequestException as exc:
|
||||
raise GigaChatError(f"GigaChat completion request failed: {exc}") from exc
|
||||
|
||||
if response.status_code >= 400:
|
||||
raise GigaChatError(f"GigaChat completion error {response.status_code}: {response.text}")
|
||||
|
||||
response = self._post_with_retry("/chat/completions", payload, token=token, timeout=90, operation_name="completion")
|
||||
data = response.json()
|
||||
choices = data.get("choices") or []
|
||||
if not choices:
|
||||
@@ -49,25 +36,49 @@ class GigaChatClient:
|
||||
"model": self._settings.embedding_model,
|
||||
"input": texts,
|
||||
}
|
||||
try:
|
||||
response = requests.post(
|
||||
f"{self._settings.api_url.rstrip('/')}/embeddings",
|
||||
json=payload,
|
||||
headers={
|
||||
"Authorization": f"Bearer {token}",
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
timeout=90,
|
||||
verify=self._settings.ssl_verify,
|
||||
)
|
||||
except requests.RequestException as exc:
|
||||
raise GigaChatError(f"GigaChat embeddings request failed: {exc}") from exc
|
||||
|
||||
if response.status_code >= 400:
|
||||
raise GigaChatError(f"GigaChat embeddings error {response.status_code}: {response.text}")
|
||||
|
||||
response = self._post_with_retry("/embeddings", payload, token=token, timeout=90, operation_name="embeddings")
|
||||
data = response.json()
|
||||
items = data.get("data")
|
||||
if not isinstance(items, list):
|
||||
raise GigaChatError("Unexpected GigaChat embeddings response")
|
||||
return [list(map(float, x.get("embedding") or [])) for x in items]
|
||||
|
||||
def _post_with_retry(
|
||||
self,
|
||||
path: str,
|
||||
payload: dict,
|
||||
*,
|
||||
token: str,
|
||||
timeout: int,
|
||||
operation_name: str,
|
||||
):
|
||||
last_error: Exception | None = None
|
||||
for attempt in range(1, MAX_RETRIES + 1):
|
||||
try:
|
||||
response = requests.post(
|
||||
f"{self._settings.api_url.rstrip('/')}{path}",
|
||||
json=payload,
|
||||
headers={
|
||||
"Authorization": f"Bearer {token}",
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
timeout=timeout,
|
||||
verify=self._settings.ssl_verify,
|
||||
)
|
||||
except requests.RequestException as exc:
|
||||
last_error = GigaChatError(f"GigaChat {operation_name} request failed: {exc}")
|
||||
else:
|
||||
if response.status_code < 400:
|
||||
return response
|
||||
last_error = GigaChatError(f"GigaChat {operation_name} error {response.status_code}: {response.text}")
|
||||
if not self._is_retryable_status(response.status_code):
|
||||
raise last_error
|
||||
if attempt == MAX_RETRIES:
|
||||
break
|
||||
time.sleep(0.1 * attempt)
|
||||
if last_error is None:
|
||||
raise GigaChatError(f"GigaChat {operation_name} failed without response")
|
||||
raise last_error
|
||||
|
||||
def _is_retryable_status(self, status_code: int) -> bool:
|
||||
return status_code == 429 or status_code >= 500
|
||||
|
||||
Reference in New Issue
Block a user