Spaces:
Running
Running
feat: added files and folders
Browse files- .env.example +16 -0
- .gitignore +39 -0
- README.md +289 -0
- app/__init__.py +0 -0
- app/agents/__init__.py +0 -0
- app/agents/case_manager.py +0 -0
- app/agents/investigator.py +0 -0
- app/agents/verifier.py +0 -0
- app/api/__init__.py +0 -0
- app/api/routes.py +0 -0
- app/main.py +0 -0
- app/models/__init__.py +0 -0
- app/models/schemas.py +0 -0
- app/observability/__init__.py +0 -0
- app/observability/metrics.py +0 -0
- app/services/__init__.py +0 -0
- app/services/context_manager.py +0 -0
- app/services/llm.py +0 -0
- app/workers/__init__.py +0 -0
- app/workers/celery_worker.py +0 -0
- docker-compose.yml +0 -0
- pyproject.toml +0 -0
- tests/__init__.py +0 -0
- tests/test_agents.py +0 -0
- tests/test_context.py +0 -0
.env.example
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# LLM Provider — choose "openai" or "gemini"
|
| 2 |
+
LLM_PROVIDER=openai
|
| 3 |
+
OPENAI_API_KEY=your-openai-api-key-here
|
| 4 |
+
|
| 5 |
+
# Gemini (only needed if LLM_PROVIDER=gemini)
|
| 6 |
+
GEMINI_API_KEY=your-gemini-api-key-here
|
| 7 |
+
|
| 8 |
+
# Redis
|
| 9 |
+
REDIS_URL=redis://localhost:6379/0
|
| 10 |
+
|
| 11 |
+
# PostgreSQL
|
| 12 |
+
DATABASE_URL=postgresql://postgres:password@localhost:5432/investigator_db
|
| 13 |
+
|
| 14 |
+
# Application
|
| 15 |
+
APP_ENV=development
|
| 16 |
+
LOG_LEVEL=info
|
.gitignore
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Python
|
| 2 |
+
__pycache__/
|
| 3 |
+
*.py[cod]
|
| 4 |
+
*.pyo
|
| 5 |
+
*.pyd
|
| 6 |
+
*.pyc
|
| 7 |
+
.Python
|
| 8 |
+
*.egg
|
| 9 |
+
*.egg-info/
|
| 10 |
+
dist/
|
| 11 |
+
build/
|
| 12 |
+
.eggs/
|
| 13 |
+
|
| 14 |
+
# Virtual environments
|
| 15 |
+
.venv/
|
| 16 |
+
venv/
|
| 17 |
+
env/
|
| 18 |
+
ENV/
|
| 19 |
+
|
| 20 |
+
# Environment variables
|
| 21 |
+
.env
|
| 22 |
+
|
| 23 |
+
# IDE
|
| 24 |
+
.vscode/
|
| 25 |
+
.idea/
|
| 26 |
+
*.swp
|
| 27 |
+
*.swo
|
| 28 |
+
|
| 29 |
+
# Pytest
|
| 30 |
+
.pytest_cache/
|
| 31 |
+
.coverage
|
| 32 |
+
htmlcov/
|
| 33 |
+
|
| 34 |
+
# Docker
|
| 35 |
+
*.log
|
| 36 |
+
|
| 37 |
+
# OS
|
| 38 |
+
.DS_Store
|
| 39 |
+
Thumbs.db
|
README.md
CHANGED
|
@@ -0,0 +1,289 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Multi-Agent Investigation Engine
|
| 2 |
+
|
| 3 |
+
A production-grade agent orchestration runtime built for collaborative, context-aware investigation of complex problems. The investigation use-case is the demo — the architecture underneath is a general-purpose agent runtime.
|
| 4 |
+
|
| 5 |
+
---
|
| 6 |
+
|
| 7 |
+
## What This Is
|
| 8 |
+
|
| 9 |
+
This system allows a user to submit an open-ended problem. A pipeline of specialized agents then collaborates to break down, investigate, and verify findings — all operating on a shared context store to prevent drift, hallucination, and out-of-scope reasoning.
|
| 10 |
+
|
| 11 |
+
This is not a chatbot wrapper. This is an agent orchestration system with:
|
| 12 |
+
|
| 13 |
+
- Structured inter-agent communication via a shared context store
|
| 14 |
+
- Asynchronous task execution using Celery and Redis
|
| 15 |
+
- A verification layer that scores agent output for hallucination and context drift
|
| 16 |
+
- Full observability via Prometheus metrics and Grafana dashboards
|
| 17 |
+
|
| 18 |
+
---
|
| 19 |
+
|
| 20 |
+
## System Architecture
|
| 21 |
+
|
| 22 |
+
```
|
| 23 |
+
User Input
|
| 24 |
+
|
|
| 25 |
+
FastAPI API Gateway
|
| 26 |
+
|
|
| 27 |
+
Case Manager Agent
|
| 28 |
+
|
|
| 29 |
+
Celery Task Queue <-- Redis Broker
|
| 30 |
+
|
|
| 31 |
+
Investigator Agent(s)
|
| 32 |
+
|
|
| 33 |
+
Shared Context Store (PostgreSQL + in-memory)
|
| 34 |
+
|
|
| 35 |
+
Verifier Agent
|
| 36 |
+
|
|
| 37 |
+
Observability Layer (Prometheus + Grafana)
|
| 38 |
+
|
|
| 39 |
+
PostgreSQL (persistent case storage)
|
| 40 |
+
```
|
| 41 |
+
|
| 42 |
+
---
|
| 43 |
+
|
| 44 |
+
## Agent Roles
|
| 45 |
+
|
| 46 |
+
### Case Manager Agent
|
| 47 |
+
Receives the raw problem statement and decomposes it into structured hypotheses.
|
| 48 |
+
|
| 49 |
+
**Input:**
|
| 50 |
+
```
|
| 51 |
+
"Why are my Instagram views dropping?"
|
| 52 |
+
```
|
| 53 |
+
|
| 54 |
+
**Output:**
|
| 55 |
+
```json
|
| 56 |
+
{
|
| 57 |
+
"possible_causes": [
|
| 58 |
+
"algorithm change",
|
| 59 |
+
"content fatigue",
|
| 60 |
+
"posting inconsistency"
|
| 61 |
+
]
|
| 62 |
+
}
|
| 63 |
+
```
|
| 64 |
+
|
| 65 |
+
---
|
| 66 |
+
|
| 67 |
+
### Investigator Agent
|
| 68 |
+
Takes a single hypothesis and returns supporting or refuting evidence based on the shared context.
|
| 69 |
+
|
| 70 |
+
---
|
| 71 |
+
|
| 72 |
+
### Verifier Agent
|
| 73 |
+
The most critical component. Evaluates investigator output for:
|
| 74 |
+
|
| 75 |
+
- Context alignment (did the agent stay in scope?)
|
| 76 |
+
- Hallucination detection (are claims grounded in facts?)
|
| 77 |
+
- Confidence scoring
|
| 78 |
+
|
| 79 |
+
**Output:**
|
| 80 |
+
```json
|
| 81 |
+
{
|
| 82 |
+
"valid": true,
|
| 83 |
+
"confidence": 0.82,
|
| 84 |
+
"reason": "Claim is consistent with established facts in context."
|
| 85 |
+
}
|
| 86 |
+
```
|
| 87 |
+
|
| 88 |
+
---
|
| 89 |
+
|
| 90 |
+
## Shared Context Store
|
| 91 |
+
|
| 92 |
+
All agents read from and write to a single shared context object per case. This is the core mechanism that prevents agent drift and ensures coherent multi-step reasoning.
|
| 93 |
+
|
| 94 |
+
**Context schema:**
|
| 95 |
+
```json
|
| 96 |
+
{
|
| 97 |
+
"case_id": 1,
|
| 98 |
+
"problem": "Instagram views dropping",
|
| 99 |
+
"constraints": ["creator niche = tech"],
|
| 100 |
+
"facts": ["posting reduced from 5/week to 2/week"],
|
| 101 |
+
"hypotheses": [],
|
| 102 |
+
"evidence": [],
|
| 103 |
+
"verifications": []
|
| 104 |
+
}
|
| 105 |
+
```
|
| 106 |
+
|
| 107 |
+
---
|
| 108 |
+
|
| 109 |
+
## Project Structure
|
| 110 |
+
|
| 111 |
+
```
|
| 112 |
+
investigator-ai/
|
| 113 |
+
|
|
| 114 |
+
├── app/
|
| 115 |
+
│ ├── api/
|
| 116 |
+
│ │ └── routes.py # FastAPI route definitions
|
| 117 |
+
│ |
|
| 118 |
+
│ ├── agents/
|
| 119 |
+
│ │ ├── case_manager.py # Decomposes the problem into hypotheses
|
| 120 |
+
│ │ ├── investigator.py # Investigates individual hypotheses
|
| 121 |
+
│ │ └── verifier.py # Validates investigator output
|
| 122 |
+
│ |
|
| 123 |
+
│ ├── services/
|
| 124 |
+
│ │ ├── llm.py # LLM client abstraction (OpenAI / Gemini)
|
| 125 |
+
│ │ └── context_manager.py # Shared context read/write operations
|
| 126 |
+
│ |
|
| 127 |
+
│ ├── workers/
|
| 128 |
+
│ │ └── celery_worker.py # Celery app and task definitions
|
| 129 |
+
│ |
|
| 130 |
+
│ ├── observability/
|
| 131 |
+
│ │ └── metrics.py # Prometheus metric definitions
|
| 132 |
+
│ |
|
| 133 |
+
│ ├── models/
|
| 134 |
+
│ │ └── schemas.py # Pydantic models for all data contracts
|
| 135 |
+
│ |
|
| 136 |
+
│ └── main.py # FastAPI app entry point
|
| 137 |
+
|
|
| 138 |
+
├── tests/
|
| 139 |
+
│ ├── test_agents.py # Unit tests for agent logic
|
| 140 |
+
│ └── test_context.py # Unit tests for context manager
|
| 141 |
+
|
|
| 142 |
+
├── docker-compose.yml # Orchestrates Redis, Postgres, Grafana, Prometheus
|
| 143 |
+
├── pyproject.toml # Project metadata and dependencies (managed by uv)
|
| 144 |
+
├── .env.example # Environment variable template
|
| 145 |
+
└── README.md
|
| 146 |
+
```
|
| 147 |
+
|
| 148 |
+
---
|
| 149 |
+
|
| 150 |
+
## Phase Roadmap
|
| 151 |
+
|
| 152 |
+
### Phase 1 — Core Engine
|
| 153 |
+
- FastAPI server running
|
| 154 |
+
- Three agents implemented with LLM calls
|
| 155 |
+
- Shared context store operational
|
| 156 |
+
- Synchronous pipeline working end to end
|
| 157 |
+
|
| 158 |
+
### Phase 2 — Async with Celery
|
| 159 |
+
- Agents execute as Celery tasks
|
| 160 |
+
- Redis as message broker
|
| 161 |
+
- Retry logic and task state tracking
|
| 162 |
+
|
| 163 |
+
### Phase 3 — Observability
|
| 164 |
+
- Prometheus metrics: agent latency, failure rate, context drift score
|
| 165 |
+
- Grafana dashboards: Agent Health, Context Tracking, Cost Monitoring
|
| 166 |
+
- `context_alignment_score` metric — measures when an agent's output diverges from the established investigation scope
|
| 167 |
+
|
| 168 |
+
### Phase 4 — Memory Experiments
|
| 169 |
+
- Compare memory strategies: Sliding Window vs Shared Context vs Summary Memory
|
| 170 |
+
- Measure: accuracy, context retention, hallucination rate
|
| 171 |
+
- Forms the research/analysis angle of the project
|
| 172 |
+
|
| 173 |
+
### Phase 5 — Frontend
|
| 174 |
+
- Minimal chat UI
|
| 175 |
+
- Backend remains the primary artifact
|
| 176 |
+
|
| 177 |
+
---
|
| 178 |
+
|
| 179 |
+
## Tech Stack
|
| 180 |
+
|
| 181 |
+
| Layer | Technology |
|
| 182 |
+
|--------------------|--------------------------|
|
| 183 |
+
| API Framework | FastAPI |
|
| 184 |
+
| Task Queue | Celery |
|
| 185 |
+
| Message Broker | Redis |
|
| 186 |
+
| Database | PostgreSQL + SQLAlchemy |
|
| 187 |
+
| Data Validation | Pydantic |
|
| 188 |
+
| LLM Provider | OpenAI / Google Gemini |
|
| 189 |
+
| Metrics | Prometheus |
|
| 190 |
+
| Dashboards | Grafana |
|
| 191 |
+
| Package Manager | uv |
|
| 192 |
+
| Dependency File | pyproject.toml |
|
| 193 |
+
|
| 194 |
+
No LangChain. Orchestration is implemented directly to maximize learning and architectural clarity.
|
| 195 |
+
|
| 196 |
+
---
|
| 197 |
+
|
| 198 |
+
## Getting Started
|
| 199 |
+
|
| 200 |
+
### Prerequisites
|
| 201 |
+
|
| 202 |
+
- Docker and Docker Compose installed (for infrastructure services only)
|
| 203 |
+
- Python 3.11+
|
| 204 |
+
- [uv](https://docs.astral.sh/uv/) installed
|
| 205 |
+
- An OpenAI or Gemini API key
|
| 206 |
+
|
| 207 |
+
### Setup
|
| 208 |
+
|
| 209 |
+
1. Copy the environment template and fill in your values:
|
| 210 |
+
```bash
|
| 211 |
+
cp .env.example .env
|
| 212 |
+
```
|
| 213 |
+
|
| 214 |
+
2. Create a virtual environment and install dependencies using uv:
|
| 215 |
+
```bash
|
| 216 |
+
uv venv
|
| 217 |
+
uv sync
|
| 218 |
+
```
|
| 219 |
+
|
| 220 |
+
3. Activate the virtual environment:
|
| 221 |
+
```bash
|
| 222 |
+
# Windows
|
| 223 |
+
.venv\Scripts\activate
|
| 224 |
+
|
| 225 |
+
# macOS / Linux
|
| 226 |
+
source .venv/bin/activate
|
| 227 |
+
```
|
| 228 |
+
|
| 229 |
+
4. Start infrastructure services:
|
| 230 |
+
```bash
|
| 231 |
+
docker-compose up -d
|
| 232 |
+
```
|
| 233 |
+
|
| 234 |
+
5. Run the API server:
|
| 235 |
+
```bash
|
| 236 |
+
uvicorn app.main:app --reload
|
| 237 |
+
```
|
| 238 |
+
|
| 239 |
+
6. Start the Celery worker (in a separate terminal):
|
| 240 |
+
```bash
|
| 241 |
+
celery -A app.workers.celery_worker worker --loglevel=info
|
| 242 |
+
```
|
| 243 |
+
|
| 244 |
+
---
|
| 245 |
+
|
| 246 |
+
## Environment Variables
|
| 247 |
+
|
| 248 |
+
Copy `.env.example` to `.env` and fill in your values. Key entries:
|
| 249 |
+
|
| 250 |
+
| Variable | Description |
|
| 251 |
+
|--------------------|------------------------------------|
|
| 252 |
+
| `OPENAI_API_KEY` | API key for the LLM provider |
|
| 253 |
+
| `REDIS_URL` | Redis connection string |
|
| 254 |
+
| `DATABASE_URL` | PostgreSQL connection string |
|
| 255 |
+
| `LLM_PROVIDER` | `openai` or `gemini` |
|
| 256 |
+
|
| 257 |
+
---
|
| 258 |
+
|
| 259 |
+
## Observability
|
| 260 |
+
|
| 261 |
+
Once running, access dashboards at:
|
| 262 |
+
|
| 263 |
+
- **Grafana:** `http://localhost:3000` (default credentials: `admin / admin`)
|
| 264 |
+
- **Prometheus:** `http://localhost:9090`
|
| 265 |
+
|
| 266 |
+
Pre-built dashboards:
|
| 267 |
+
|
| 268 |
+
1. **Agent Health** — success rate, failure rate, retries, latency per agent
|
| 269 |
+
2. **Context Tracking** — context drift score, memory size, hallucination count, verification failures
|
| 270 |
+
3. **Cost Monitoring** — tokens used, estimated LLM cost, case runtime
|
| 271 |
+
|
| 272 |
+
---
|
| 273 |
+
|
| 274 |
+
## Design Decisions
|
| 275 |
+
|
| 276 |
+
**Why no LangChain?**
|
| 277 |
+
Building orchestration from scratch provides a deeper understanding of agent coordination, context propagation, and failure handling. It also produces a cleaner, more interview-demonstrable codebase.
|
| 278 |
+
|
| 279 |
+
**Why a Verifier Agent?**
|
| 280 |
+
In multi-agent systems, downstream agents inherit errors from upstream agents. A dedicated verification step with a quantified confidence score makes the system self-auditing and significantly reduces hallucination propagation.
|
| 281 |
+
|
| 282 |
+
**Why a shared context store instead of individual agent memory?**
|
| 283 |
+
Individual agent memory leads to context drift — each agent developing a slightly different model of the problem. A single authoritative context object forces all agents to operate on the same ground truth.
|
| 284 |
+
|
| 285 |
+
---
|
| 286 |
+
|
| 287 |
+
## License
|
| 288 |
+
|
| 289 |
+
MIT
|
app/__init__.py
ADDED
|
File without changes
|
app/agents/__init__.py
ADDED
|
File without changes
|
app/agents/case_manager.py
ADDED
|
File without changes
|
app/agents/investigator.py
ADDED
|
File without changes
|
app/agents/verifier.py
ADDED
|
File without changes
|
app/api/__init__.py
ADDED
|
File without changes
|
app/api/routes.py
ADDED
|
File without changes
|
app/main.py
ADDED
|
File without changes
|
app/models/__init__.py
ADDED
|
File without changes
|
app/models/schemas.py
ADDED
|
File without changes
|
app/observability/__init__.py
ADDED
|
File without changes
|
app/observability/metrics.py
ADDED
|
File without changes
|
app/services/__init__.py
ADDED
|
File without changes
|
app/services/context_manager.py
ADDED
|
File without changes
|
app/services/llm.py
ADDED
|
File without changes
|
app/workers/__init__.py
ADDED
|
File without changes
|
app/workers/celery_worker.py
ADDED
|
File without changes
|
docker-compose.yml
ADDED
|
File without changes
|
pyproject.toml
ADDED
|
File without changes
|
tests/__init__.py
ADDED
|
File without changes
|
tests/test_agents.py
ADDED
|
File without changes
|
tests/test_context.py
ADDED
|
File without changes
|