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 keeps them readable while the old key is in SM_SECRET_KEY_PREVIOUS (docs/secret-key-rotation.md); without it they surface as unreadable and must be re-entered. 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 the framework's csrfToken shared 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.17

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.17
File Size Uploaded Provenance Actions
simple_module_ai-0.0.17.tar.gz 114.8 kB With provenance Details for simple_module_ai-0.0.17.tar.gz

Built distribution (wheel)

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

Total release size: 190.3 kB

Release files / simple_module_ai-0.0.17.tar.gz

Download URL simple_module_ai-0.0.17.tar.gz
Size 114.8 kB
Tags Source
SHA-256 checksum
How to use checksums
51d234bff44370b41551d50329550cc2d57924eedc09ffb8280c280e7b6ecdd1
BLAKE2b-256 checksum
How to use checksums
99dfb7be61a4c0f5e8c5246a622d10caf06c8b5663c10c76a29d7aad4a160b2a
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 29, 2026.

Transparency log

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

Download URL simple_module_ai-0.0.17-py3-none-any.whl
Size 75.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
07fa7512fc896928964227fc97a91ac0a6bb82c9b7a32a427569e08e0cebc1c6
BLAKE2b-256 checksum
How to use checksums
f59d4689c28b39d466e82d65b7394d60915d0e816b131ec40ae5067bcc0e6a40
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 29, 2026.

Transparency log

Security for simple-module-ai 0.0.17

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.17.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 29, 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