A FastAPI-based gateway service that automatically routes queries to topic-specific RAG agents using AI-powered topic detection.
- Automatic topic detection using OpenAI embeddings
- Configurable routing to topic-specific agents
- Fallback agent for unknown topics
- Environment variable configuration
- Docker support
- Comprehensive test coverage
rag-gateway/
βββ app/
β βββ main.py # FastAPI entrypoint
β βββ router.py # /query endpoint
β βββ config.py # YAML config loader
β βββ schema.py # Request/response models
β βββ client.py # HTTP client to call agents
β βββ utils.py # Helper functions
βββ config.yaml # Routing config
βββ tests/
β βββ test_routing.py # Unit + integration tests
βββ scripts/
β βββ start.sh # Script to start the service
β βββ test.sh # Script to run tests
βββ Dockerfile # For containerizing the gateway
βββ docker-compose.yml # For local testing with mock agents
βββ requirements.txt
βββ README.md
-
Clone the repository
-
Install dependencies:
pip install -r requirements.txt
-
Run the tests:
./scripts/test.sh
-
Start the services using Docker Compose:
./scripts/start.sh
Send a POST request to /api/v1/query
with the following JSON payload:
{
"message": "How do I reset my password?"
}
The service will:
- Detect the topic using AI embeddings
- Route the query to the appropriate agent
- Return a response with the detected topic and confidence score
Example response:
{
"response": "To reset your password, click the 'Forgot Password' link.",
"detected_topic": "password_reset",
"metadata": {
"confidence": 0.95,
"agent_metadata": {
"source": "password_reset_guide"
}
}
}
The service uses a YAML configuration file (config.yaml
) to define topics and their corresponding agent URLs. Each topic includes:
- Route: The URL of the agent service
- Descriptions: Example queries and descriptions for topic detection
Example configuration:
topics:
password_reset:
route: ${AGENT_PASSWORD_RESET_URL}/query
descriptions:
- "How to reset password"
- "Forgot password"
- "Change password"
billing:
route: ${AGENT_BILLING_URL}/query
descriptions:
- "Billing questions"
- "Payment issues"
- "Invoice problems"
default_agent: ${AGENT_FALLBACK_URL}/query
Required environment variables:
OPENAI_API_KEY
: Your OpenAI API keyAGENT_PASSWORD_RESET_URL
: URL for password reset agentAGENT_BILLING_URL
: URL for billing agentAGENT_FALLBACK_URL
: URL for fallback agent
Optional environment variables:
ENVIRONMENT
: Deployment environment (default: "development")REQUEST_TIMEOUT
: Request timeout in seconds (default: 30.0)CORS_ORIGINS
: Allowed CORS origins (default: ["*"])CORS_METHODS
: Allowed CORS methods (default: ["*"])CORS_HEADERS
: Allowed CORS headers (default: ["*"])
- Create a virtual environment:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
- Install dependencies:
pip install -r requirements.txt
- Run tests:
pytest tests/ -v
- Start the service:
uvicorn app.main:app --reload
- Build the image:
docker build -t rag-gateway .
- Run the container:
docker run -p 8000:8000 --env-file .env rag-gateway
The service includes comprehensive tests for:
- Health check endpoint
- Configuration loading
- Topic detection
- Query routing
- Error handling
Run tests with:
pytest tests/ -v
Once the server is running, visit:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc