Skip to main content
This document provides detailed documentation for the MultiMind SDK’s RAG system API endpoints.

Table of Contents

  1. Overview
  2. Authentication
  3. Client Library
  4. Endpoints
  5. Request/Response Models
  6. Examples
  7. Error Handling

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
Run it standalone with 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:
  1. API Key Authentication
    • Set the X-API-Key header with your API key
    • API keys are configured via the API_KEYS environment variable (comma-separated list)
  2. JWT Authentication
    • Get a token using the /token endpoint (requires JWT_SECRET and JWT_USERS_JSON, plus pip install "passlib[bcrypt]" for password verification)
    • Include the token in the Authorization: Bearer <token> header
    • Tokens are HS256 and expire after 30 minutes
When neither 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

Response:
Errors: 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

The RAGClient class provides the following methods:
  • login(username: str, password: str) -> str: Get JWT token
  • add_documents(documents: List[Document]) -> Dict: Add documents
  • add_file(file_path: Union[str, Path], metadata: Optional[Dict] = None) -> Dict: Add file
  • query(query: str, top_k: Optional[int] = 3, filter_metadata: Optional[Dict] = None) -> Dict: Query documents
  • generate(query: str, top_k: Optional[int] = 3, temperature: Optional[float] = 0.7, max_tokens: Optional[int] = None, filter_metadata: Optional[Dict] = None) -> Dict: Generate response
  • clear_documents() -> Dict: Clear all documents
  • get_document_count() -> int: Get document count
  • switch_model(model_type: str, model_name: str) -> Dict: Switch model
  • health_check() -> Dict: Check system health

Endpoints

Document Management

Add Documents

Add one or more documents to the RAG system. Request Body:
Response:

Add File

Add a file to the RAG system. Form Data:
  • file: File to upload (required)
  • metadata: JSON string of metadata (optional)
Response (one entry per document the loader produced from the file; filename is always added to the metadata):

Query Documents

Query the RAG system for relevant documents. Request Body:
Response:

Generate Response

Generate a response using the RAG system. Request Body:
Response:

Clear Documents

Clear all documents from the RAG system. Response:

Get Document Count

Get the number of documents in the RAG system. Response:

Model Management

Switch Model

Switch the model used by the RAG system. Form Data:
  • model_type: “openai” or “anthropic” (the matching *_API_KEY must be set, otherwise 400)
  • model_name: Model name (e.g., “gpt-3.5-turbo”, “claude-3-sonnet-20240229”)
Only the text-generation model is switched; the embedding model and vector store are unchanged. Response:

Health Check

Liveness

Does not require authentication and does not initialize the RAG backend. Response:

Readiness

Returns 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

  1. Add Documents:
  1. Query Documents:
  1. Generate Response:

Error Handling

The API uses standard HTTP status codes and returns error details in the response body:
Common error scenarios:
  1. 400 Bad Request
    • Unsupported model_type, or the matching provider key is not set
  2. 401 Unauthorized
    • Missing or invalid X-API-Key / bearer token when authentication is configured
    • Expired token, wrong /token credentials
  3. 404 Not Found
    • Endpoint not found
  4. 422 Unprocessable Entity
    • Invalid request body or missing required fields (FastAPI validation)
  5. 503 Service Unavailable
    • RAG backend could not be initialized (no provider keys and no local HuggingFace stack)
    • /token called without JWT_SECRET / JWT_USERS_JSON
    • /ready before initialization
  6. 500 Internal Server Error
    • Model API errors, processing errors (details are logged, a generic message is returned)
Example error response:

Rate Limiting

Currently, the API does not implement rate limiting. However, it’s recommended to:
  1. Implement appropriate rate limiting in production
  2. Monitor API usage
  3. Set up proper authentication
  4. Use appropriate timeouts for long-running operations

Best Practices

  1. Document Management
    • Use meaningful metadata
    • Clean documents before adding
    • Monitor document count
  2. Querying
    • Use appropriate top_k values
    • Leverage metadata filtering
    • Handle large result sets
  3. Generation
    • Adjust temperature based on use case
    • Set appropriate max_tokens
    • Monitor token usage
  4. Error Handling
    • Implement proper error handling
    • Use appropriate timeouts
    • Handle rate limits
  5. Security
    • Set up authentication
    • Use HTTPS in production
    • Validate input data