MaMAI v4 è una libreria RAG stateless basata su LangChain con context esplicito e storage dinamico.
Project description
MaMAI v4.0.0
MaMAI is a stateless Python RAG library.
It is designed for service-oriented systems where the caller owns state and storage decisions.
Core principles
- No internal
db.jsonor hidden persistence. - No implicit global runtime configuration.
- Every operation receives a full
ExecutionContext. - Vector store backend and storage location are selected at runtime by the caller.
Installation
python3 -m venv .venv
. .venv/bin/activate
pip install -U pip
pip install -e .
Quick start
from Mama import (
ExecutionContext,
LlmConfig,
RetrievalConfig,
StorageConfig,
default_runtime,
ingest_pdf_directory,
query,
)
ctx = ExecutionContext(
tenant_id="acme",
user_id="user-1",
session_id="session-1",
kb_id="kb-support",
llm=LlmConfig(provider="dummy", model="dummy"),
retrieval=RetrievalConfig(search_type="similarity", k=3),
storage=StorageConfig(
backend="faiss_local",
params={
"base_path": "./data/vectorstores",
"embeddings_backend": "simple",
},
),
)
runtime = default_runtime()
ingest_pdf_directory(ctx, "./docs_to_index", runtime)
result = query(
ctx,
"What is this document about?",
runtime,
prompt=(
"You are the KB assistant. Follow global KB rules.\n"
"Apply instructions from the active knowledge branch."
),
)
print(result.answer)
print(result.documents)
# Caller-owned state persistence
ctx.conversation = result.updated_conversation_state
ExecutionContext
ExecutionContext is the contract between your wrapper/service and MaMAI core.
Required identity fields:
tenant_iduser_idsession_idkb_id
Config sections:
llm: provider, model, parametersretrieval: search type andkstorage: backend + dynamic backend paramsconversation: in-memory message state passed in/out
Per-query prompt input (required by query(...)):
prompt: fully resolved prompt string for the current query
You can build it programmatically or from JSON payload with:
from Mama.config import build_execution_context
ctx = build_execution_context(payload)
Identity Model (tenant_id, user_id, session_id, kb_id)
These 4 fields are not interchangeable. They model different scopes.
tenant_id (organization boundary)
- Purpose: hard isolation boundary between customers/workspaces.
- Typical value: company/workspace slug or UUID (
acme,tenant-42). - Effect in MaMAI:
- used by storage naming for
faiss_local(<base_path>/<tenant_id>/<kb_id>) - should also be propagated to metadata and external stores.
- used by storage naming for
- Rule: never mix data from different tenants in one index.
user_id (end-user identity)
- Purpose: actor identity (who is asking / who ingested).
- Typical value: authenticated user id (
user-123, email hash, UUID). - Effect in MaMAI:
- not used for index partitioning by default
- available for metadata, auditing, policy decisions in wrappers.
- Rule: treat as identity/audit field, not as storage key unless your design needs per-user KBs.
session_id (conversation instance)
- Purpose: one chat/conversation thread.
- Typical value: request/session UUID (
sess-2026-...). - Effect in MaMAI:
- identifies the conversation memory passed in/out via
ctx.conversation - does not create separate vector indexes by default.
- identifies the conversation memory passed in/out via
- Rule: rotate
session_idper new chat thread; persist chat state in your own store.
kb_id (knowledge base identity)
- Purpose: logical knowledge corpus id.
- Typical value: domain/corpus key (
kb-support,kb-legal-v2). - Effect in MaMAI:
- primary key for vector index selection together with
tenant_id - same
kb_idmeans same index namespace (inside tenant).
- primary key for vector index selection together with
- Rule: change
kb_idwhen you want a different corpus/index.
Practical composition
Think in scopes:
- Tenant scope:
tenant_id - Corpus scope:
kb_id - User scope:
user_id - Conversation scope:
session_id
For faiss_local, effective index path is:
<base_path>/<tenant_id>/<kb_id>
So:
- changing
session_iddoes not change index - changing
user_iddoes not change index (unless you encode it inkb_id) - changing
kb_iddoes change index - changing
tenant_iddoes change isolation boundary and index location
Recommended patterns
Shared team KB + per-user chats:
- same
tenant_id+kb_id - different
user_id/session_id
Versioned KB rollout:
kb_id=kb-support-v1, thenkb-support-v2- run A/B or migration safely
Per-user private KB:
- set
kb_idlikekb-user-<user_id>(or explicit mapping in wrapper)
Anti-patterns to avoid
- Using
session_idaskb_idaccidentally (creates fragmented indexes). - Reusing one global
tenant_idfor all customers (breaks isolation). - Expecting
user_idalone to separate vector stores (it does not by default). - Keeping
kb_idconstant across unrelated corpora.
Caller-defined vector store storage
MaMAI does not decide where your index is stored. The caller does.
faiss_local
Persistent FAISS index on caller-provided filesystem location.
Supported params:
base_path: root directory; final path is<base_path>/<tenant_id>/<kb_id>index_path: full explicit path (overridesbase_pathlogic)embeddings_backend:simple,hf, orauto
Example with explicit per-request storage path:
ctx.storage = StorageConfig(
backend="faiss_local",
params={
"index_path": "/mnt/customer-a/rag/kb-support-v1",
"embeddings_backend": "simple",
},
)
faiss_memory
In-memory FAISS store (non-persistent), useful for ephemeral jobs and tests.
ctx.storage = StorageConfig(
backend="faiss_memory",
params={"embeddings_backend": "simple"},
)
Register your own vector store backend
You can plug a custom backend (Qdrant, Pinecone, Weaviate, Milvus, custom S3 adapter).
from Mama.ports import RuntimePorts
from Mama.runtime import DefaultEmbeddingsProvider, DefaultLlmProvider
from Mama.vectorstores import DynamicVectorStoreFactory
factory = DynamicVectorStoreFactory()
# builder signature: (ctx, embeddings) -> VectorStorePort
factory.register_backend("qdrant", my_qdrant_builder)
runtime = RuntimePorts(
llm_provider=DefaultLlmProvider(),
embeddings_provider=DefaultEmbeddingsProvider(),
vector_store_factory=factory,
)
ctx.storage.backend = "qdrant"
ctx.storage.params = {
"url": "https://qdrant.example.com",
"api_key": "...",
"collection": "kb-support",
"tenant": ctx.tenant_id,
}
State ownership model
MaMAI core is stateless by design:
- it reads conversation state from
ctx.conversation - it returns updated state in
QueryResult.updated_conversation_state - it does not persist sessions/history internally
Typical wrapper responsibilities:
- load context from request + tenant config
- call MaMAI
- persist updated conversation state in your DB/cache
- enforce auth, rate limits, and policy
Main modules
Mama/models.py: data contracts (ExecutionContext,QueryResult,IngestResult)Mama/ports.py: extension contracts for runtime providersMama/runtime.py: default runtime providersMama/vectorstores.py: dynamic vector store factory and FAISS adaptersMama/ingestion.py: stateless ingestion use casesMama/query.py: stateless RAG query pipelineMama/admin.py: document listing and vector store statsMama/webscraper.py: URL crawl/load/ingest helpers
Testing
Run smoke test:
make test
Run web demo:
make web
Health endpoint:
curl http://127.0.0.1:5000/health
Notes
The sample tests generate a minimal PDF file and may show pypdf parsing warnings. If the test ends with TEST OK, the pipeline worked as expected.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mamai-4.3.0.tar.gz.
File metadata
- Download URL: mamai-4.3.0.tar.gz
- Upload date:
- Size: 21.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a5a797e32ca2eda1ad1ffa85bd1505833ebbe83cf3d25ba8b408f86f3c7404ab
|
|
| MD5 |
ab4450cd8e24ac21fec97dd2f570d54e
|
|
| BLAKE2b-256 |
ad38727feafb2aca2b33512b39d5c6580f3f3c812a3c8dedc5c70bae6448baa5
|
File details
Details for the file mamai-4.3.0-py3-none-any.whl.
File metadata
- Download URL: mamai-4.3.0-py3-none-any.whl
- Upload date:
- Size: 21.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2f7b6fe756669cfd84701b6e67372a359718bcf86893986203b9ccb713769fa7
|
|
| MD5 |
a18ddda7faadbc7e3221b4ec623290d7
|
|
| BLAKE2b-256 |
40079ea7ed16e57549e3659363b52a68ef17dfcfd09089259995e82575753666
|