api.services.ai.service

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

Exceptions

AIBlocked

No call was made for the feature: not_configured | disabled |

AIUnavailable

Every AI model of the chain was skipped or failed.

Classes

Functions

is_configured() → bool

Module Contents

api.services.ai.service.logger
exception api.services.ai.service.AIBlocked(code: str)

Bases: Exception

No call was made for the feature: not_configured | disabled | daily_limit | budget.

MESSAGES
code
message
exception api.services.ai.service.AIUnavailable(attempts: list[dict[str, str]])

Bases: Exception

Every AI model of the chain was skipped or failed.

attempts
message = ''
api.services.ai.service.AIRefused
api.services.ai.service.SKIP_REASONS
api.services.ai.service.MAX_WAIT_REASON = 'max_wait'
api.services.ai.service.RECORDED_SKIPS
class api.services.ai.service.AIRun
result: api.services.ai.providers.AIResult
call_id: int
profile: api.services.ai.settings.ModelProfile
attempts: list[dict[str, str]] = []
json() → Any
class api.services.ai.service.AIService(store: api.services.ai.store.AIStore | None = None, providers: dict[str, api.services.ai.providers.AIProvider] | None = None)
store
providers
settings() → api.services.ai.settings.AISettings
save_settings(settings: api.services.ai.settings.AISettings) → api.services.ai.settings.AISettings
configured_providers() → dict[str, bool]
guard(feature: str) → tuple[api.services.ai.settings.AISettings, api.services.ai.settings.FeatureSettings]

Feature-level checks, before trying any AI model.

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).

feedback(call_id: int, outcome: str, detail: dict[str, Any] | None = None) → int
api.services.ai.service.is_configured() → bool