A sophisticated multi-agent system that leverages Knowledge Graphs and Google's Gemini API to process natural language queries against insurance policy documents. The system provides intelligent, transparent, and highly accurate decision-making for insurance claim processing.
- Multi-Agent Architecture: Specialized agents for query parsing, knowledge graph analysis, policy reasoning, financial calculations, and decision synthesis
- Knowledge Graph Integration: Neo4j-based knowledge representation for superior reasoning and relationship understanding
- Google Gemini API: Advanced LLM capabilities for natural language processing and reasoning
- RESTful API: FastAPI-based backend with comprehensive documentation
- Transparent Decision Making: Clear justification and clause references for all decisions
- Scalable Design: Modular architecture supporting easy extension and customization
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β β β β β β
β FastAPI App βββββΊβ Multi-Agent βββββΊβ Neo4j KG β
β (main.py) β β Orchestrator β β (kg_manager) β
β β β (agents.py) β β β
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β β β
β ββββββββββΌβββββββββ β
β β β β
ββββββββββββββββΊβ Google Gemini βββββββββββββββ
β API β
βββββββββββββββββββ
- QueryParsingAgent: Extracts structured entities from natural language
- GraphQueryGenerationAgent: Generates Cypher queries for Knowledge Graph
- KnowledgeGraphAnalysisAgent: Executes queries and analyzes results
- PolicyReasoningAgent: Performs logical inference and decision-making
- FinancialCalculationAgent: Calculates payout amounts and financial details
- DecisionSynthesisAgent: Synthesizes final decision with justification
- Python 3.9 or higher
- Neo4j Database (Community or Enterprise Edition)
- Google Gemini API key
- Git (for cloning the repository)
git clone <repository-url>
cd RagAgentpython -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activatepip install -r requirements.txtdocker run \
--name neo4j-insurance \
-p7474:7474 -p7687:7687 \
-d \
-v $HOME/neo4j/data:/data \
-v $HOME/neo4j/logs:/logs \
-v $HOME/neo4j/import:/var/lib/neo4j/import \
-v $HOME/neo4j/plugins:/plugins \
--env NEO4J_AUTH=neo4j/your_password_here \
neo4j:latest- Download Neo4j from https://neo4j.com/download/
- Install and start Neo4j
- Access Neo4j Browser at
http://localhost:7474 - Set initial password
Copy the .env file and update with your credentials:
cp .env .env.local # Optional: create local copyEdit .env and set:
GEMINI_API_KEY=your_actual_gemini_api_key
NEO4J_PASSWORD=your_actual_neo4j_password- Visit Google AI Studio
- Create a new API key
- Copy the API key to your
.envfile
python main.pyThe system will:
- Initialize the Neo4j Knowledge Graph
- Set up multi-agent orchestrator
- Load sample policy documents
- Start the FastAPI server on
http://localhost:8000
POST /process_query
Content-Type: application/json
{
"query": "46-year-old male, knee surgery in Pune, 3-month-old insurance policy"
}Response:
{
"Decision": "Approved",
"Amount": 75000,
"Justification": "Coverage approved for knee surgery. Patient meets age eligibility (46 years), procedure is covered under orthopedic benefits, and 90-day waiting period is satisfied for 3-month-old policy.",
"Relevant_Clauses": [
{
"clause_text": "Orthopedic procedures including knee surgery are covered with a maximum limit of Rs. 75,000 per procedure",
"document_id": "sample_policy.txt",
"page_section": "Section 4.2 - Surgical Procedures"
}
]
}GET /statusPOST /load_document
Content-Type: application/json
{
"document_path": "documents/new_policy.txt",
"document_id": "policy_002"
}GET /kg_schemaAccess the interactive API documentation at:
- Swagger UI:
http://localhost:8000/docs - ReDoc:
http://localhost:8000/redoc
{
"query": "Can a 35-year-old female get heart surgery covered in Mumbai with a 6-month-old policy?"
}{
"query": "What is the payout for dental surgery for a 45-year-old in Delhi?"
}{
"query": "Is cataract surgery covered for a 60-year-old person with 2-year-old insurance?"
}| Variable | Description | Default |
|---|---|---|
GEMINI_API_KEY |
Google Gemini API key | Required |
NEO4J_URI |
Neo4j database URI | bolt://localhost:7687 |
NEO4J_USERNAME |
Neo4j username | neo4j |
NEO4J_PASSWORD |
Neo4j password | Required |
HOST |
FastAPI server host | 127.0.0.1 |
PORT |
FastAPI server port | 8000 |
LOG_LEVEL |
Logging level | INFO |
Each agent can be customized by modifying the respective class in agents.py:
class CustomQueryParsingAgent(BaseAgent):
def process(self, input_data: Dict[str, Any]) -> AgentResponse:
# Custom implementation
pass- Place documents in the
documents/directory - Use the
/load_documentendpoint to process them - The system will automatically extract entities and relationships
pytest tests/Use the provided example queries or test with curl:
curl -X POST "http://localhost:8000/process_query" \
-H "Content-Type: application/json" \
-d '{"query": "knee surgery for 30-year-old in Mumbai with 4-month policy"}'The system provides comprehensive logging:
- Application Logs: General system operations
- Agent Logs: Individual agent processing steps
- Neo4j Logs: Database operations and queries
- API Logs: Request/response logging
Logs are output to console by default. Configure logging in main.py for file output.
- API Keys: Store securely in environment variables
- Database: Use strong passwords and secure connections
- CORS: Configure appropriately for production
- Input Validation: All inputs are validated using Pydantic models
Create a Dockerfile:
FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
EXPOSE 8000
CMD ["python", "main.py"]Create docker-compose.yml:
version: '3.8'
services:
app:
build: .
ports:
- "8000:8000"
depends_on:
- neo4j
environment:
- NEO4J_URI=bolt://neo4j:7687
neo4j:
image: neo4j:latest
ports:
- "7474:7474"
- "7687:7687"
environment:
- NEO4J_AUTH=neo4j/password- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Ensure Neo4j is running
- Check connection credentials
- Verify port 7687 is accessible
- Verify API key is correct
- Check API quotas and limits
- Ensure internet connectivity
- Check input query format
- Verify Knowledge Graph has data
- Review agent logs for specific errors
- Create an issue on GitHub
- Check the logs for detailed error messages
- Verify all environment variables are set correctly