Skip to main content

simple_module_ai

AI base module for SimpleModule apps: one provider-configurable service layer (chat + embeddings) that every other module shares, with DB-backed settings, encrypted keys and an admin page.

The module owns connection configuration — provider, endpoint, key, model name. Consuming modules own behaviour — agents, prompts, temperature, retries, streaming.

Full documentation lives in docs/: user guide (the AI settings page), API reference (the sm_ai.contracts API and the HTTP routes), operations (install, settings, key rotation, troubleshooting) and architecture.

Install

pip install simple_module_ai

Add simple_module_ai to your host's dependencies; it is discovered at boot via the simple_module entry point. There are no tables and no migrations.

Usage (consuming from another module)

Declare the dependency in your module's ModuleMeta:

meta = ModuleMeta(name="MyModule", depends_on=["Ai"], ...)

Then resolve connections wherever you need them:

from pydantic_ai import Agent
from sm_ai.contracts import AiNotConfiguredError, resolve_model, resolve_embedder

agent = Agent(instructions="...", output_type=MySchema)
result = await agent.run(text, model=resolve_model())

embedder = resolve_embedder()  # raises AiNotConfiguredError until the
result = await embedder.embed_documents(chunks)  # embedding slot is set up

resolve_model(model_name=...) overrides the configured model name for same-endpoint multi-model setups. embedding_dim() returns the configured vector width. All errors subclass AiError.

The import package is sm_ai, not ai — the bare name belongs to another PyPI project and would collide in hosts that install it.

Usage metering

With the billing module installed, every AI request a consumer serves can count against the organisation's plan as the ai.requests meter, a counter that resets each billing period. Without billing nothing is metered or limited, and nothing is imported: ai has no dependency on billing.

On startup the module registers ai.requests with billing's meter registry, so the plan editor suggests it (absent is unlimited, 0 forbids). The module serves no chat of its own, so the consumer guards the route that calls the model:

from sm_ai.contracts import RequiresAiRequest, resolve_model

@router.post("/summarise", dependencies=[RequiresAiRequest()])
async def summarise(...):
    result = await agent.run(text, model=resolve_model())

It is billing's RequiresQuota("ai.requests"): past the limit the route answers 402 quota_exceeded, and the charge is refunded only when the handler raises, so let a provider error propagate rather than return an error response. A streaming response is charged when the handler returns it; a failure mid-stream refunds nothing. utils/quotaError.ts turns the 402 into a translated message, and this module's API client uses it. See the API reference.

Settings

Two slots — chat and embeddings — each with provider (anthropic, openai, google, openai_compatible), model, base URL and API key. The openai_compatible provider covers vLLM, Ollama and LM Studio (base URL required; a blank key is replaced by a protocol-satisfying placeholder).

Configure at /ai/ (permission ai.manage) or headlessly with scripts/set_setting.py sm_ai <field> <value>. The generic Settings screen links to /ai/ rather than editing these fields itself, so the page's validation and key encryption apply to every save. The module reads no SM_AI_* environment variables: settings live in the database at SYSTEM scope, so every tenant shares one provider configuration and one API key per slot. API keys are encrypted at rest with a key derived from SM_SECRET_KEY — rotating that secret means re-entering provider keys (the old ciphertexts surface as unreadable). A settings save hot-reloads the worker that handled it; other workers pick the change up on restart. Celery workers and scripts that never run the web host read the same settings themselves, re-reading at most once a minute.

Routes

Route Purpose
GET /ai/ admin settings page
GET/PUT /api/ai/settings read (secrets masked) / update
POST /api/ai/test probe the chat or embedding slot
POST /api/ai/models list the model ids the slot's saved endpoint serves (first 500, sorted)

Writes require the framework's X-CSRF-Token header bound to the session (the admin page sends it automatically; it is shared as the csrf_token prop).

The package follows the module layout; see its code map for the feature areas and test folders.

Metadata

Release files for simple-module-ai 0.0.14

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for simple-module-ai 0.0.14
File Size Uploaded Provenance Actions
simple_module_ai-0.0.14.tar.gz 77.1 kB With provenance Details for simple_module_ai-0.0.14.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for simple-module-ai 0.0.14
File Interpreter ABI Platform Actions
simple_module_ai-0.0.14-py3-none-any.whl Python 3 none any Details for simple_module_ai-0.0.14-py3-none-any.whl

Total release size: 126.2 kB

Release files / simple_module_ai-0.0.14.tar.gz

Download URL simple_module_ai-0.0.14.tar.gz
Size 77.1 kB
Tags Source
SHA-256 checksum
How to use checksums
665a54cc1ab28229b7f994e9cb9554518f6d75e62b39928aad3606327a27ed08
BLAKE2b-256 checksum
How to use checksums
90f4526e08c6623cbc9892c0a1c69cd3f9d8b451eec149bd12b6c9423948f789
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 7, 2026.

Transparency log

Release files / simple_module_ai-0.0.14-py3-none-any.whl

Download URL simple_module_ai-0.0.14-py3-none-any.whl
Size 49.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dd355eaffb57eb89805595c1499f03de3a515de30b7d1341abb7c0c60abc30a6
BLAKE2b-256 checksum
How to use checksums
0fd9f1d61c1800d5ea9530776667d9394223f58ab8e80e7d22727bca9cc1c01d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 7, 2026.

Transparency log

Security for simple-module-ai 0.0.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for simple_module_ai-0.0.14.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 7, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page