api.services.ai.service ======================= .. py:module:: api.services.ai.service .. autoapi-nested-parse:: The one way the API uses AI. A feature describes its request once (AIRequest); ``AIService.run`` tries the feature's chain of AI models in order: an AI model is skipped when it is off, its provider is off or has no API key, it lacks a capability the feature needs (web search, structured JSON), or a limit is reached (AI model, provider, feature, monthly budget); a failed call (rate limit, no credit, overload, refusal, invalid answer, error) moves on to the next one. Every call and every limit-blocked attempt is recorded with its provider, AI model, tokens, cost, duration and outcome. Feedback on results (accepted / edited / rejected) is attached to the call that produced them. Attributes ---------- .. autoapisummary:: api.services.ai.service.logger api.services.ai.service.AIRefused api.services.ai.service.SKIP_REASONS api.services.ai.service.MAX_WAIT_REASON api.services.ai.service.RECORDED_SKIPS Exceptions ---------- .. autoapisummary:: api.services.ai.service.AIBlocked api.services.ai.service.AIUnavailable Classes ------- .. autoapisummary:: api.services.ai.service.AIRun api.services.ai.service.AIService Functions --------- .. autoapisummary:: api.services.ai.service.is_configured Module Contents --------------- .. py:data:: logger .. py:exception:: AIBlocked(code: str) Bases: :py:obj:`Exception` No call was made for the feature: not_configured | disabled | daily_limit | budget. .. py:attribute:: MESSAGES .. py:attribute:: code .. py:attribute:: message .. py:exception:: AIUnavailable(attempts: list[dict[str, str]]) Bases: :py:obj:`Exception` Every AI model of the chain was skipped or failed. .. py:attribute:: attempts .. py:attribute:: message :value: '' .. py:data:: AIRefused .. py:data:: SKIP_REASONS .. py:data:: MAX_WAIT_REASON :value: 'max_wait' .. py:data:: RECORDED_SKIPS .. py:class:: AIRun .. py:attribute:: result :type: api.services.ai.providers.AIResult .. py:attribute:: call_id :type: int .. py:attribute:: profile :type: api.services.ai.settings.ModelProfile .. py:attribute:: attempts :type: list[dict[str, str]] :value: [] .. py:method:: json() -> Any .. py:class:: AIService(store: api.services.ai.store.AIStore | None = None, providers: dict[str, api.services.ai.providers.AIProvider] | None = None) .. py:attribute:: store .. py:attribute:: providers .. py:method:: settings() -> api.services.ai.settings.AISettings .. py:method:: save_settings(settings: api.services.ai.settings.AISettings) -> api.services.ai.settings.AISettings .. py:method:: configured_providers() -> dict[str, bool] .. py:method:: guard(feature: str) -> tuple[api.services.ai.settings.AISettings, api.services.ai.settings.FeatureSettings] Feature-level checks, before trying any AI model. .. py:method:: run(feature: str, request: api.services.ai.providers.AIRequest, *, subject: str | None = None, settings: tuple[api.services.ai.settings.AISettings, api.services.ai.settings.FeatureSettings] | None = None, expect_json: bool = False) -> AIRun Try the feature's chain until one AI model answers. Raises AIBlocked (feature-level) or AIUnavailable (nothing answered). A rate-limited AI model cools down (its own delay, else the general one) and the next usable model is tried at once; any other failure drops the model for this request. Once only cooling models are left, wait for the first one to be ready and try it again: up to ``rate_limit_retries`` new tries per model and ``max_wait_seconds`` of waiting in all. Cooldowns carry over between requests (call log). .. py:method:: feedback(call_id: int, outcome: str, detail: dict[str, Any] | None = None) -> int .. py:function:: is_configured() -> bool