This document provides detailed documentation for the MultiMind SDK's RAG system API endpoints.
The RAG API 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 monitoring
The API supports two authentication methods:
-
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 - Include the token in the
Authorization: Bearer <token>header - Tokens expire after 30 minutes
- Get a token using the
# API Keys (comma-separated)
export API_KEYS="key1,key2,key3"
# JWT Secret (change in production)
export JWT_SECRET="your-secret-key"
# Model API Keys
export OPENAI_API_KEY="your-openai-key"
export ANTHROPIC_API_KEY="your-anthropic-key"POST /token
Content-Type: application/x-www-form-urlencoded
username=testuser&password=secretResponse:
{
"access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9...",
"token_type": "bearer"
}The API uses scopes to control access:
rag:read: Required for querying and generatingrag:write: Required for adding documents and managing models
The MultiMind SDK includes a client library for easy interaction with the RAG API.
pip install multimind-sdkfrom multimind.client.rag_client import RAGClient, Document
import asyncio
async def main():
# Initialize client with API key
client = RAGClient(
base_url="http://localhost:8000",
api_key="your-api-key"
)
# Or use JWT authentication
# client = RAGClient(base_url="http://localhost:8000")
# token = await client.login("username", "password")
# Add documents
docs = [
Document(
text="The RAG system provides powerful document processing.",
metadata={"type": "introduction"}
)
]
await client.add_documents(docs)
# Query
results = await client.query("What is the RAG system?")
print("Query results:", results)
# Generate
response = await client.generate(
"Explain the RAG system",
temperature=0.7
)
print("Generated response:", response)
# Run example
asyncio.run(main())The RAGClient 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
POST /documentsAdd one or more documents to the RAG system.
Request Body:
{
"documents": [
{
"text": "Document text content",
"metadata": {
"source": "example",
"type": "documentation"
}
}
]
}Response:
{
"documents": [
{
"text": "Document text content",
"metadata": {
"source": "example",
"type": "documentation"
}
}
],
"total": 1
}POST /filesAdd a file to the RAG system.
Form Data:
file: File to upload (required)metadata: JSON string of metadata (optional)
Response:
{
"documents": [
{
"text": "Added file: example.md",
"metadata": {
"source": "file",
"type": "markdown"
}
}
],
"total": 1
}POST /queryQuery the RAG system for relevant documents.
Request Body:
{
"query": "What is the RAG system?",
"top_k": 3,
"filter_metadata": {
"type": "documentation"
}
}Response:
{
"documents": [
{
"text": "The RAG system provides...",
"metadata": {
"type": "documentation"
},
"score": 0.85
}
],
"total": 1
}POST /generateGenerate a response using the RAG system.
Request Body:
{
"query": "Explain the RAG system",
"top_k": 3,
"temperature": 0.7,
"max_tokens": 500,
"filter_metadata": {
"type": "documentation"
}
}Response:
{
"text": "The RAG (Retrieval Augmented Generation) system...",
"documents": [
{
"text": "The RAG system provides...",
"metadata": {
"type": "documentation"
},
"score": 0.85
}
]
}DELETE /documentsClear all documents from the RAG system.
Response:
{
"message": "All documents cleared successfully"
}GET /documents/countGet the number of documents in the RAG system.
Response:
{
"count": 42
}POST /models/switchSwitch the model used by the RAG system.
Form Data:
model_type: "openai" or "anthropic"model_name: Model name (e.g., "gpt-3.5-turbo", "claude-3-sonnet-20240229")
Response:
{
"message": "Switched to openai model: gpt-3.5-turbo"
}GET /healthCheck the health of the RAG system.
Response:
{
"status": "healthy",
"document_count": 42
}class DocumentRequest(BaseModel):
text: str
metadata: Optional[Dict[str, Any]] = Field(default_factory=dict)class QueryRequest(BaseModel):
query: str
top_k: Optional[int] = 3
filter_metadata: Optional[Dict[str, Any]] = Noneclass GenerateRequest(BaseModel):
query: str
top_k: Optional[int] = 3
temperature: Optional[float] = 0.7
max_tokens: Optional[int] = None
filter_metadata: Optional[Dict[str, Any]] = Noneclass DocumentResponse(BaseModel):
text: str
metadata: Dict[str, Any]
score: Optional[float] = Noneclass QueryResponse(BaseModel):
documents: List[DocumentResponse]
total: intclass GenerateResponse(BaseModel):
text: str
documents: List[DocumentResponse]import aiohttp
import json
async def rag_api_example():
async with aiohttp.ClientSession() as session:
# Add documents
documents = [
{
"text": "The RAG system provides powerful document processing.",
"metadata": {"type": "introduction"}
}
]
async with session.post(
"http://localhost:8000/documents",
json={"documents": documents}
) as response:
result = await response.json()
print("Added documents:", result)
# Query documents
query = {
"query": "What is the RAG system?",
"top_k": 3
}
async with session.post(
"http://localhost:8000/query",
json=query
) as response:
result = await response.json()
print("Query results:", result)
# Generate response
generate = {
"query": "Explain the RAG system",
"temperature": 0.7
}
async with session.post(
"http://localhost:8000/generate",
json=generate
) as response:
result = await response.json()
print("Generated response:", result)
# Run example
import asyncio
asyncio.run(rag_api_example())- Add Documents:
curl -X POST http://localhost:8000/documents \
-H "Content-Type: application/json" \
-d '{
"documents": [
{
"text": "The RAG system provides powerful document processing.",
"metadata": {"type": "introduction"}
}
]
}'- Query Documents:
curl -X POST http://localhost:8000/query \
-H "Content-Type: application/json" \
-d '{
"query": "What is the RAG system?",
"top_k": 3
}'- Generate Response:
curl -X POST http://localhost:8000/generate \
-H "Content-Type: application/json" \
-d '{
"query": "Explain the RAG system",
"temperature": 0.7
}'The API uses standard HTTP status codes and returns error details in the response body:
{
"detail": "Error message describing what went wrong"
}Common error scenarios:
-
400 Bad Request
- Invalid request body
- Missing required fields
- Invalid model type
-
404 Not Found
- Endpoint not found
- Document not found
-
500 Internal Server Error
- Model API errors
- Processing errors
- System errors
Example error response:
{
"detail": "Failed to process document: Invalid format"
}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
-
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