feat(llm): провайдер за протоколом — OpenAI-совместимый API и mock, фабрика по конфигу
- LLMProvider: complete(messages, schema) -> LLMResult (текст, разобранный JSON, токены) - openai_compat: /chat/completions, structured output через response_format json_schema, JSON из ```-обёртки тоже разбирается; ретраи на 429/5xx и сетевые ошибки, 4xx — сразу ошибка - mock: детерминированные правила для тестов и демо без сети - LLMSettings (LLM_PROVIDER/BASE_URL/MODEL/API_KEY…), CLI python -m app.llm.cli Refs #17 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+15
-1
@@ -1,7 +1,7 @@
|
||||
"""Typed configuration from the environment / `.env` — one settings class per subsystem (ARCHITECTURE §2.11)."""
|
||||
|
||||
from pathlib import Path
|
||||
from typing import Annotated
|
||||
from typing import Annotated, Literal
|
||||
|
||||
from pydantic import Field, SecretStr, field_validator
|
||||
from pydantic_settings import BaseSettings, NoDecode, SettingsConfigDict
|
||||
@@ -29,6 +29,19 @@ class AccessSettings(BaseSettings):
|
||||
return value
|
||||
|
||||
|
||||
class LLMSettings(BaseSettings):
|
||||
model_config = SettingsConfigDict(**_ENV, env_prefix="LLM_")
|
||||
|
||||
# mock: deterministic offline answers (tests, demo without network); openai_compat: any OpenAI-style API (ADR-008)
|
||||
provider: Literal["mock", "openai_compat"] = "mock"
|
||||
base_url: str = ""
|
||||
model: str = ""
|
||||
api_key: SecretStr = SecretStr("")
|
||||
timeout: float = Field(default=30.0, gt=0)
|
||||
max_attempts: int = Field(default=3, ge=1)
|
||||
temperature: float = Field(default=0.0, ge=0, le=2)
|
||||
|
||||
|
||||
class RuntimeSettings(BaseSettings):
|
||||
model_config = _ENV
|
||||
|
||||
@@ -46,4 +59,5 @@ class Settings(BaseSettings):
|
||||
|
||||
telegram: TelegramSettings = Field(default_factory=TelegramSettings)
|
||||
access: AccessSettings = Field(default_factory=AccessSettings)
|
||||
llm: LLMSettings = Field(default_factory=LLMSettings)
|
||||
runtime: RuntimeSettings = Field(default_factory=RuntimeSettings)
|
||||
|
||||
Reference in New Issue
Block a user