Первый коммит
This commit is contained in:
0
app/modules/shared/gigachat/__init__.py
Normal file
0
app/modules/shared/gigachat/__init__.py
Normal file
BIN
app/modules/shared/gigachat/__pycache__/__init__.cpython-312.pyc
Normal file
BIN
app/modules/shared/gigachat/__pycache__/__init__.cpython-312.pyc
Normal file
Binary file not shown.
BIN
app/modules/shared/gigachat/__pycache__/client.cpython-312.pyc
Normal file
BIN
app/modules/shared/gigachat/__pycache__/client.cpython-312.pyc
Normal file
Binary file not shown.
BIN
app/modules/shared/gigachat/__pycache__/errors.cpython-312.pyc
Normal file
BIN
app/modules/shared/gigachat/__pycache__/errors.cpython-312.pyc
Normal file
Binary file not shown.
BIN
app/modules/shared/gigachat/__pycache__/settings.cpython-312.pyc
Normal file
BIN
app/modules/shared/gigachat/__pycache__/settings.cpython-312.pyc
Normal file
Binary file not shown.
Binary file not shown.
73
app/modules/shared/gigachat/client.py
Normal file
73
app/modules/shared/gigachat/client.py
Normal file
@@ -0,0 +1,73 @@
|
||||
import requests
|
||||
|
||||
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
|
||||
|
||||
|
||||
class GigaChatClient:
|
||||
def __init__(self, settings: GigaChatSettings, token_provider: GigaChatTokenProvider) -> None:
|
||||
self._settings = settings
|
||||
self._tokens = token_provider
|
||||
|
||||
def complete(self, system_prompt: str, user_prompt: str) -> str:
|
||||
token = self._tokens.get_access_token()
|
||||
payload = {
|
||||
"model": self._settings.model,
|
||||
"messages": [
|
||||
{"role": "system", "content": system_prompt},
|
||||
{"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}")
|
||||
|
||||
data = response.json()
|
||||
choices = data.get("choices") or []
|
||||
if not choices:
|
||||
return ""
|
||||
message = choices[0].get("message") or {}
|
||||
return str(message.get("content") or "")
|
||||
|
||||
def embed(self, texts: list[str]) -> list[list[float]]:
|
||||
token = self._tokens.get_access_token()
|
||||
payload = {
|
||||
"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}")
|
||||
|
||||
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]
|
||||
2
app/modules/shared/gigachat/errors.py
Normal file
2
app/modules/shared/gigachat/errors.py
Normal file
@@ -0,0 +1,2 @@
|
||||
class GigaChatError(OSError):
|
||||
pass
|
||||
25
app/modules/shared/gigachat/settings.py
Normal file
25
app/modules/shared/gigachat/settings.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from dataclasses import dataclass
|
||||
import os
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class GigaChatSettings:
|
||||
auth_url: str
|
||||
api_url: str
|
||||
scope: str
|
||||
credentials: str
|
||||
ssl_verify: bool
|
||||
model: str
|
||||
embedding_model: str
|
||||
|
||||
@classmethod
|
||||
def from_env(cls) -> "GigaChatSettings":
|
||||
return cls(
|
||||
auth_url=os.getenv("GIGACHAT_AUTH_URL", "https://ngw.devices.sberbank.ru:9443/api/v2/oauth"),
|
||||
api_url=os.getenv("GIGACHAT_API_URL", "https://gigachat.devices.sberbank.ru/api/v1"),
|
||||
scope=os.getenv("GIGACHAT_SCOPE", "GIGACHAT_API_PERS"),
|
||||
credentials=os.getenv("GIGACHAT_TOKEN", "").strip(),
|
||||
ssl_verify=os.getenv("GIGACHAT_SSL_VERIFY", "true").lower() in {"1", "true", "yes"},
|
||||
model=os.getenv("GIGACHAT_MODEL", "GigaChat"),
|
||||
embedding_model=os.getenv("GIGACHAT_EMBEDDING_MODEL", "Embeddings"),
|
||||
)
|
||||
58
app/modules/shared/gigachat/token_provider.py
Normal file
58
app/modules/shared/gigachat/token_provider.py
Normal file
@@ -0,0 +1,58 @@
|
||||
import threading
|
||||
import time
|
||||
import uuid
|
||||
|
||||
import requests
|
||||
|
||||
from app.modules.shared.gigachat.errors import GigaChatError
|
||||
from app.modules.shared.gigachat.settings import GigaChatSettings
|
||||
|
||||
|
||||
class GigaChatTokenProvider:
|
||||
def __init__(self, settings: GigaChatSettings) -> None:
|
||||
self._settings = settings
|
||||
self._lock = threading.Lock()
|
||||
self._token: str | None = None
|
||||
self._expires_at_ms: float = 0
|
||||
|
||||
def get_access_token(self) -> str:
|
||||
now_ms = time.time() * 1000
|
||||
with self._lock:
|
||||
if self._token and self._expires_at_ms - 300_000 > now_ms:
|
||||
return self._token
|
||||
|
||||
token, expires_at = self._fetch_token()
|
||||
with self._lock:
|
||||
self._token = token
|
||||
self._expires_at_ms = expires_at
|
||||
return token
|
||||
|
||||
def _fetch_token(self) -> tuple[str, float]:
|
||||
if not self._settings.credentials:
|
||||
raise GigaChatError("GIGACHAT_TOKEN is not set")
|
||||
headers = {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
"Accept": "application/json",
|
||||
"Authorization": f"Basic {self._settings.credentials}",
|
||||
"RqUID": str(uuid.uuid4()),
|
||||
}
|
||||
try:
|
||||
response = requests.post(
|
||||
self._settings.auth_url,
|
||||
headers=headers,
|
||||
data=f"scope={self._settings.scope}",
|
||||
timeout=30,
|
||||
verify=self._settings.ssl_verify,
|
||||
)
|
||||
except requests.RequestException as exc:
|
||||
raise GigaChatError(f"GigaChat auth request failed: {exc}") from exc
|
||||
|
||||
if response.status_code >= 400:
|
||||
raise GigaChatError(f"GigaChat auth error {response.status_code}: {response.text}")
|
||||
|
||||
payload = response.json()
|
||||
token = payload.get("access_token")
|
||||
expires_at = float(payload.get("expires_at", 0))
|
||||
if not token:
|
||||
raise GigaChatError("GigaChat auth: no access_token in response")
|
||||
return token, expires_at
|
||||
Reference in New Issue
Block a user