Фиксация изменений

This commit is contained in:
2026-03-05 11:03:17 +03:00
parent 1ef0b4d68c
commit 417b8b6f72
261 changed files with 8215 additions and 332 deletions

View File

@@ -0,0 +1,37 @@
from __future__ import annotations
import os
from pathlib import Path
_ENV_FILES = (".env", ".env.local")
def load_workspace_env(start_dir: str | Path | None = None) -> list[Path]:
base = Path(start_dir or Path.cwd()).resolve()
loaded: list[Path] = []
for directory in reversed((base, *base.parents)):
for file_name in _ENV_FILES:
path = directory / file_name
if not path.is_file():
continue
_load_env_file(path)
loaded.append(path)
return loaded
def _load_env_file(path: Path) -> None:
for raw_line in path.read_text(encoding="utf-8").splitlines():
line = raw_line.strip()
if not line or line.startswith("#") or "=" not in line:
continue
key, raw_value = line.split("=", 1)
name = key.removeprefix("export ").strip()
if not name or name in os.environ:
continue
os.environ[name] = _normalize_value(raw_value.strip())
def _normalize_value(value: str) -> str:
if len(value) >= 2 and value[0] == value[-1] and value[0] in {"'", '"'}:
return value[1:-1]
return value

View File

@@ -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