Table of Contents
Overview
The RAG API (multimind.gateway.rag_api, FastAPI) provides RESTful endpoints for interacting with the MultiMind SDK’s RAG system. It supports:
- Document management (add, query, delete)
- File uploads
- Semantic search
- Response generation
- Model management
- Health and readiness probes
python -m multimind.gateway.rag_api (port 8000) or uvicorn multimind.gateway.rag_api:app; the same routes are also mounted in the combined MultiMind API served by multimind backend (port 8080). Requires pip install "multimind-sdk[gateway,rag]". Step-by-step setup: rag_api_server_setup.md. Generated spec: openapi-rag.json.
The RAG backend (FAISS store, embedding model, text model) is initialized lazily on the first document/query request. If no provider can be set up the request fails with 503 "RAG backend is not available...".
Authentication
The API supports two authentication methods, checked at request time:-
API Key Authentication
- Set the
X-API-Keyheader with your API key - API keys are configured via the
API_KEYSenvironment variable (comma-separated list)
- Set the
-
JWT Authentication
- Get a token using the
/tokenendpoint (requiresJWT_SECRETandJWT_USERS_JSON, pluspip install "passlib[bcrypt]"for password verification) - Include the token in the
Authorization: Bearer <token>header - Tokens are HS256 and expire after 30 minutes
- Get a token using the
API_KEYS nor JWT_SECRET is set the API runs in open (development) mode and accepts anonymous requests. /health and /ready never require authentication.
Environment Variables
Getting a Token
503 when JWT_SECRET or JWT_USERS_JSON is not configured, 401 for a wrong username/password.
Scopes
Issued tokens carry"scopes": ["rag:read", "rag:write"], but the endpoints do not currently check scopes: any authenticated caller (API key or valid JWT) can use every endpoint.
Client Library
The MultiMind SDK includes a client library for easy interaction with the RAG API.Installation
Basic Usage
Client Methods
TheRAGClient class provides the following methods:
login(username: str, password: str) -> str: Get JWT tokenadd_documents(documents: List[Document]) -> Dict: Add documentsadd_file(file_path: Union[str, Path], metadata: Optional[Dict] = None) -> Dict: Add filequery(query: str, top_k: Optional[int] = 3, filter_metadata: Optional[Dict] = None) -> Dict: Query documentsgenerate(query: str, top_k: Optional[int] = 3, temperature: Optional[float] = 0.7, max_tokens: Optional[int] = None, filter_metadata: Optional[Dict] = None) -> Dict: Generate responseclear_documents() -> Dict: Clear all documentsget_document_count() -> int: Get document countswitch_model(model_type: str, model_name: str) -> Dict: Switch modelhealth_check() -> Dict: Check system health
Endpoints
Document Management
Add Documents
Add File
file: File to upload (required)metadata: JSON string of metadata (optional)
filename is always added to the metadata):
Query Documents
Generate Response
Clear Documents
Get Document Count
Model Management
Switch Model
model_type: “openai” or “anthropic” (the matching*_API_KEYmust be set, otherwise400)model_name: Model name (e.g., “gpt-3.5-turbo”, “claude-3-sonnet-20240229”)
Health Check
Liveness
Readiness
503 "RAG backend not initialized" until the first document/query request has initialized the backend.
Response:
Request/Response Models
DocumentRequest
QueryRequest
GenerateRequest
DocumentResponse
QueryResponse
GenerateResponse
Examples
Python Client Example
cURL Examples
- Add Documents:
- Query Documents:
- Generate Response:
Error Handling
The API uses standard HTTP status codes and returns error details in the response body:-
400 Bad Request
- Unsupported
model_type, or the matching provider key is not set
- Unsupported
-
401 Unauthorized
- Missing or invalid
X-API-Key/ bearer token when authentication is configured - Expired token, wrong
/tokencredentials
- Missing or invalid
-
404 Not Found
- Endpoint not found
-
422 Unprocessable Entity
- Invalid request body or missing required fields (FastAPI validation)
-
503 Service Unavailable
- RAG backend could not be initialized (no provider keys and no local HuggingFace stack)
/tokencalled withoutJWT_SECRET/JWT_USERS_JSON/readybefore initialization
-
500 Internal Server Error
- Model API errors, processing errors (details are logged, a generic message is returned)
Rate Limiting
Currently, the API does not implement rate limiting. However, it’s recommended to:- Implement appropriate rate limiting in production
- Monitor API usage
- Set up proper authentication
- Use appropriate timeouts for long-running operations
Best Practices
-
Document Management
- Use meaningful metadata
- Clean documents before adding
- Monitor document count
-
Querying
- Use appropriate top_k values
- Leverage metadata filtering
- Handle large result sets
-
Generation
- Adjust temperature based on use case
- Set appropriate max_tokens
- Monitor token usage
-
Error Handling
- Implement proper error handling
- Use appropriate timeouts
- Handle rate limits
-
Security
- Set up authentication
- Use HTTPS in production
- Validate input data
