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

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.0.tar.gz (30.0 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.0-py3-none-any.whl (28.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: muscles_ai-0.1.0.tar.gz
  • Upload date:
  • Size: 30.0 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.0.tar.gz
Algorithm Hash digest
SHA256 ec43d61c059b1152cd438fc2980866b25b0c8faa4d8e2475b17f427add34a755
MD5 9b31513736625feb860a678807e5df16
BLAKE2b-256 44d832a1de14c0196beeab167bd7b724ef0cde4d4f2b9059ee601f3fb4fd0e35

See more details on using hashes here.

Provenance

The following attestation bundles were made for muscles_ai-0.1.0.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.0-py3-none-any.whl.

File metadata

  • Download URL: muscles_ai-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 28.0 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 90384234ddf07e7c60c2d6bc14ab003338de7b8d73052d43ffe60292b22a3e73
MD5 72337b62e8ccb0c12eea9a540dd55c9a
BLAKE2b-256 fd59963e082324e7c3f833617addf1c6bc16bed5e11c8cabaaa72d8911659ab2

See more details on using hashes here.

Provenance

The following attestation bundles were made for muscles_ai-0.1.0-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