api.services.ai.store ===================== .. py:module:: api.services.ai.store .. autoapi-nested-parse:: Persistence of AI usage: every call, feedback on its results, and the AI settings. SQLite on the API's persistent volume (shared by the api and worker containers; WAL handles their concurrent writes). Attributes ---------- .. autoapisummary:: api.services.ai.store.SCHEMA api.services.ai.store.OUTCOMES Classes ------- .. autoapisummary:: api.services.ai.store.AIStore Module Contents --------------- .. py:data:: SCHEMA :value: Multiline-String .. raw:: html
Show Value .. code-block:: python """ CREATE TABLE IF NOT EXISTS ai_calls ( id INTEGER PRIMARY KEY AUTOINCREMENT, created_at TEXT NOT NULL, feature TEXT NOT NULL, provider TEXT, profile TEXT, model TEXT NOT NULL, served_model TEXT, status TEXT NOT NULL, stop_reason TEXT, input_tokens INTEGER NOT NULL DEFAULT 0, output_tokens INTEGER NOT NULL DEFAULT 0, cache_read_tokens INTEGER NOT NULL DEFAULT 0, cache_write_tokens INTEGER NOT NULL DEFAULT 0, web_searches INTEGER NOT NULL DEFAULT 0, images INTEGER NOT NULL DEFAULT 0, cost_usd REAL NOT NULL DEFAULT 0, duration_ms INTEGER NOT NULL DEFAULT 0, subject TEXT, request_id TEXT, error TEXT ); CREATE INDEX IF NOT EXISTS ai_calls_feature_created ON ai_calls (feature, created_at); CREATE TABLE IF NOT EXISTS ai_feedback ( id INTEGER PRIMARY KEY AUTOINCREMENT, call_id INTEGER NOT NULL REFERENCES ai_calls (id), created_at TEXT NOT NULL, outcome TEXT NOT NULL, detail TEXT ); CREATE INDEX IF NOT EXISTS ai_feedback_call ON ai_feedback (call_id); CREATE TABLE IF NOT EXISTS ai_settings ( key TEXT PRIMARY KEY, value TEXT NOT NULL, updated_at TEXT NOT NULL ); """ .. raw:: html
.. py:data:: OUTCOMES :value: ('accepted', 'edited', 'rejected') .. py:class:: AIStore(path: str | None = None) .. py:attribute:: path :type: str :value: '' .. py:method:: record_call(**values: Any) -> int .. py:method:: calls_since(since: datetime.datetime, *, feature: str | None = None, provider: str | None = None, profile: str | None = None) -> int Calls actually made (blocked attempts excluded) since ``since``. .. py:method:: cost_since(since: datetime.datetime, *, provider: str | None = None, profile: str | None = None) -> float .. py:method:: last_rate_limit(profile: str, since: datetime.datetime) -> datetime.datetime | None When this AI model was last rate-limited (any request, any process). .. py:method:: link_fallback(call_ids: list[int], answered_by: int) -> None .. py:method:: recent_calls(limit: int = 50, feature: str | None = None) -> list[dict[str, Any]] .. py:method:: usage_summary(days: int = 30) -> dict[str, Any] Per feature over the period: calls, errors, tokens, cost, and how often its results were accepted / edited / rejected. .. py:method:: add_feedback(call_id: int, outcome: str, detail: dict[str, Any] | None = None) -> int .. py:method:: load_settings() -> dict[str, Any] | None .. py:method:: save_settings(value: dict[str, Any]) -> None