Skip to main content

Framework-level AI and RAG primitives for Muscles

Project description

muscles-ai

Framework-level AI package for the Muscles ecosystem.

Purpose

  • Keep reusable AI/RAG contracts, runtimes, and actions in one framework package.
  • Provide read-only primitives for question answering and retrieval.
  • Stay transport-agnostic: transport adapters (HTTP/CLI/MCP/JSON-RPC/SSE) call Muscles actions.

Ecosystem Position

muscles-ai is a framework extension, not an application template and not a transport adapter. It registers AI-oriented actions in a Muscles app and lets other packages project those actions through HTTP, CLI, MCP, JSON-RPC or SSE.

Related repositories:

  • muscles - core action contracts, dispatcher, inspect contract and canonical documentation.
  • muscles-documents - document loading/parsing/chunking actions that AI flows can inspect or compose with.
  • muscles-mcp - MCP projection for AI tools.
  • muscles-sse - streaming projection for long AI output.
  • muscles-benchmarks - regression coverage for AI extension contracts.

Installation

pip install git+https://github.com/butkoden/muscles-ai.git

The canonical ecosystem install matrix lives in muscles/docs/installation.md.

The package expects to be loaded as a Muscles module:

modules:
  ai:
    package: muscles_ai
    provider: "noop"
    transports: ["http", "cli", "mcp"]

Optional in-process providers are installed only when needed:

pip install "muscles-ai[openai]"
pip install "muscles-ai[llama-cpp]"

The application does not need Ollama or another model server. An adapter can call an external SDK/API directly or load a local model library in the current process.

Public API

Importing package symbols uses lazy __getattr__ to keep package startup lightweight:

  • AiPackage — package installer for init_package integration.
  • AiRuntime — runtime container for RAG execution.
  • SearchQuery, RetrievedChunk, ContextBlock, Citation, RetrievalPolicy.
  • SearchResult, ContextResult, AskResult, SearchHit, SourceChunk.
  • VectorSearchPort, KeywordSearchPort, ParentFetchPort, IndexRequestPort, LLMProvider.
  • InMemoryRagSource, FakeLLMProvider, NoopLLMProvider for tests and examples.
  • ModelGateway, ModelProviderCatalog, ModelCapability and typed requests/results for text, image and embedding capabilities.
  • PythonModelAdapter for project-owned models and openai/llama_cpp optional providers.
  • AiConfig.
  • init_package(app, config) entry point for Muscles.

Default actions

The package registers the following actions:

  • ai.ask
  • ai.search
  • ai.retrieve_context
  • ai.sources.list
  • ai.source.inspect
  • ai.documents.inspect
  • ai.index.request
  • ai.inspect
  • ai.doctor

AI architecture contract

inspect_application(app)["capabilities"]["ai"]["architecture"] publishes a machine-readable implementation contract for developers and coding agents. It contains the package role, preferred/allowed/forbidden patterns and stable deterministic rules with id, severity and summary fields.

Every ai.* action also declares three metadata namespaces:

  • metadata["ai"] describes the AI/RAG operation;
  • metadata["architecture"] describes side effects, state changes and confirmation;
  • metadata["mcp"] describes MCP exposure and read-only safety.

ai.index.request is state-changing and requires explicit confirmation. The package doctor validates these invariants without loading an AI model or making provider network calls. An AI model may consume the contract to guide code generation, but it is not used to enforce the rules.

RAG Toolkit

muscles-ai owns RAG orchestration, not project data storage:

query -> retrieval -> merge -> deterministic rerank -> context -> prompt -> LLM -> answer + citations

Projects register sources/adapters on AiRuntime:

from muscles_ai import InMemoryRagSource, RetrievedChunk

runtime.register_source(
    "kb",
    InMemoryRagSource(
        "kb",
        chunks=[
            RetrievedChunk(
                chunk_id="flowwow-1",
                text="Flowwow backend used PostgreSQL and Kafka.",
                source="kb",
                parent_id="flowwow",
                title="Flowwow",
            )
        ],
    ),
)

Real projects can implement the same ports over Qdrant, PostgreSQL, Elasticsearch or another store. This package does not own DSNs, collections, mappings, migrations or document parsing.

Model Gateway

ModelGateway is the universal in-process model facade. It routes typed requests to named models without exposing provider SDKs to business actions:

from muscles_ai import ImageGenerationRequest, ModelGateway, TextGenerationRequest

gateway = ModelGateway.from_config(
    providers={
        "local": {"type": "llama_cpp", "options": {"model_path": "models/model.gguf"}},
        "images": {"type": "openai", "options": {"api_key_env": "OPENAI_API_KEY"}},
    },
    models={
        "text.local": {
            "provider": "local",
            "model": "local-text",
            "capabilities": ["text.generate"],
        },
        "image.remote": {
            "provider": "images",
            "model": "dall-e-3",
            "capabilities": ["image.generate"],
        },
    },
    defaults={"text.generate": "text.local"},
)

text = gateway.invoke(TextGenerationRequest(prompt="Explain RAG"))
image = gateway.invoke(
    ImageGenerationRequest(prompt="A simple framework diagram"),
    model="image.remote",
)

The public facade is shared, while request and response types remain specific to the capability. New local runtimes can be registered through ModelProviderCatalog or PythonModelAdapter; no model server is required. Images and other binary results are returned as Artifact values containing bytes or a provider reference. The project decides whether to persist them.

Notes

  • muscles-ai intentionally does not open HTTP routes.
  • Runtime clients must be registered in DI and used from actions/context, not kept in ApplicationRegistry.
  • Transport packages should discover ai.* actions through inspect_application(app) and execute through ActionDispatcher.
  • Document ingestion belongs in muscles-documents; AI should consume document contracts instead of duplicating parsers.
  • Telemetry is resolved through the neutral Muscles TelemetryProvider; this package does not import muscles-otel directly.

Telemetry

When a project registers a TelemetryProvider, muscles-ai emits safe spans:

  • muscles.ai.embed
  • muscles.ai.retrieve
  • muscles.ai.rerank
  • muscles.ai.prompt.build
  • muscles.ai.generate
  • muscles.ai.answer

Allowed attributes include provider/model names, retriever name, retrieved document count and citation count. Raw queries, prompts, answers, excerpts, chunks, request bodies and API keys must not be stored in span attributes.

Examples

Direct package initialization

Run an end-to-end smoke scenario with muscles_ai actions:

PYTHONPATH=src python examples/run_ai_smoke.py

Action calls through a dispatcher

PYTHONPATH=src python examples/run_ai_configured.py

Both examples:

  • initialize the package via init_package(app, config);
  • register all ai actions;
  • call actions through ActionDispatcher;
  • demonstrate the neutral telemetry provider hook without requiring muscles-otel.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

muscles_ai-0.1.1.tar.gz (33.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

muscles_ai-0.1.1-py3-none-any.whl (30.9 kB view details)

Uploaded Python 3

File details

Details for the file muscles_ai-0.1.1.tar.gz.

File metadata

  • Download URL: muscles_ai-0.1.1.tar.gz
  • Upload date:
  • Size: 33.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for muscles_ai-0.1.1.tar.gz
Algorithm Hash digest
SHA256 6ab0ed16fb216a9de877a3a15880bb79e6177ebf3ba9a2dbd63801071d597582
MD5 ec5b9064439388c7a2c4398f8bafb0ea
BLAKE2b-256 8bbf3b03801d7415c1a1588c79695f580a75c803b047f03a9dfb576606c68913

See more details on using hashes here.

Provenance

The following attestation bundles were made for muscles_ai-0.1.1.tar.gz:

Publisher: release.yml on butkoden/muscles-ai

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file muscles_ai-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: muscles_ai-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 30.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for muscles_ai-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 699b2b2af231055a73bad268b9019b5e2d01315536c8c62022d64e208ece1b9f
MD5 dfb28794084618272abb786e8c03ff54
BLAKE2b-256 c1e4f55e216737070629b922f06866ba19a0e61209764a2bca904c94ee6572a1

See more details on using hashes here.

Provenance

The following attestation bundles were made for muscles_ai-0.1.1-py3-none-any.whl:

Publisher: release.yml on butkoden/muscles-ai

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page