Deployment commited on
Commit
21629fd
·
1 Parent(s): 4b1daed

Automated deployment update

Browse files
Files changed (2) hide show
  1. README.md +22 -458
  2. scripts/deploy_hf.py +2 -5
README.md CHANGED
@@ -1,467 +1,31 @@
1
- # AmaniQuery
2
-
3
- **AI-Powered Legal & News Intelligence Platform for Kenya**
4
-
5
- [![Go Version](https://img.shields.io/badge/Go-1.21+-00ADD8?style=flat&logo=go)](https://go.dev/)
6
- [![License](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)
7
- [![Build Status](https://img.shields.io/badge/Build-Passing-success)](https://github.com/amaniquery/amaniquery)
8
-
9
- AmaniQuery is an AI agent framework designed to democratize access to legal information and civil education in Kenya. Built with a production-ready, Go-based RAG (Retrieval-Augmented Generation) architecture, it provides accurate, contextual answers about Kenya Law and current affairs.
10
-
11
- ---
12
-
13
- ## 🎯 Vision
14
-
15
- **Democratizing Access to Information and Civil Education**
16
-
17
- AmaniQuery aims to make legal knowledge accessible to every Kenyan citizen by:
18
- - Providing accurate answers to legal questions in plain language
19
- - Aggregating and contextualizing news relevant to civic matters
20
- - Offering a reliable, fast, and secure platform for information retrieval
21
-
22
- ---
23
-
24
- ## 📐 Architecture Overview
25
-
26
- ### High-Level System Architecture
27
-
28
- ```mermaid
29
- graph TB
30
- subgraph "Client Layer"
31
- UI[Web UI/React]
32
- API[REST/gRPC API]
33
- CLI[Command Line]
34
- end
35
-
36
- subgraph "Control Plane"
37
- AGENT[Agent Orchestrator<br/>Go-based Coordinator]
38
- ROUTER[Query Router<br/>Semantic Classifier]
39
- EVAL[Evaluation Engine<br/>Quality Scorer]
40
- GUARDRAILS[NeMo Guardrails<br/>Safety Filter]
41
- end
42
-
43
- subgraph "Data Plane"
44
- EMBED[Embedding Service<br/>GPU-Accelerated]
45
- RETRIEVER[Retriever Service<br/>Hybrid Search]
46
- RERANK[Reranker Service<br/>Cross-Encoder]
47
- GENERATOR[Generator Service<br/>LLM Gateway]
48
- end
49
-
50
- subgraph "Storage Layer"
51
- VDB[(Vector DB<br/>Qdrant)]
52
- CACHE[(Redis Cache)]
53
- OBJ[(Object Store<br/>MinIO/S3)]
54
- GRAPH[(Graph DB<br/>Neo4j)]
55
- end
56
-
57
- subgraph "Infrastructure"
58
- MONITOR[Monitoring<br/>Prometheus/Grafana]
59
- TRACING[Tracing<br/>Jaeger]
60
- VAULT[Secrets Vault<br/>HashiCorp Vault]
61
- end
62
-
63
- UI --> API
64
- CLI --> API
65
- API --> AGENT
66
- AGENT --> ROUTER
67
- ROUTER --> GUARDRAILS
68
- GUARDRAILS --> EMBED
69
- GUARDRAILS --> RETRIEVER
70
- RETRIEVER --> VDB
71
- RETRIEVER --> GRAPH
72
- EMBED --> VDB
73
- RETRIEVER --> RERANK
74
- RERANK --> GENERATOR
75
- GENERATOR --> EVAL
76
- EVAL --> CACHE
77
- CACHE --> API
78
- AGENT --> MONITOR
79
- AGENT --> TRACING
80
- VAULT --> AGENT
81
- ```
82
-
83
- ### Query Router & Semantic Classification
84
-
85
- ```mermaid
86
- graph LR
87
- Q[User Query] --> PREPROCESS[Preprocessor<br/>Cleaning/Normalization]
88
- PREPROCESS --> CLASSIFIER[Classifier<br/>Intent Detection]
89
- CLASSIFIER --> C1{Query Type}
90
- C1 -->|Factual| VSS[Vector Search]
91
- C1 -->|Keyword| BM25[BM25 Search]
92
- C1 -->|Relational| GRAPH[GraphRAG]
93
- C1 -->|Multi-hop| AGENT[Agentic Search]
94
-
95
- VSS --> HYBRID[Hybrid Results]
96
- BM25 --> HYBRID
97
- GRAPH --> HYBRID
98
- AGENT --> HYBRID
99
- HYBRID --> FUSION[Rank Fusion<br/>Reciprocal Rank]
100
- FUSION --> OUTPUT[Ranked Chunks]
101
- ```
102
-
103
- ### Security Architecture
104
-
105
- ```mermaid
106
- graph TB
107
- subgraph "Perimeter Security"
108
- WAF[Web App Firewall]
109
- API_GATEWAY[API Gateway<br/>Kong/Envoy]
110
- RATE_LIMIT[Rate Limiter<br/>Token Bucket]
111
- end
112
-
113
- subgraph "Authentication & Authorization"
114
- OIDC[OIDC Provider<br/>Keycloak]
115
- JWT[JWT Validator<br/>RS256]
116
- OPA[OPA Policy Agent]
117
- POLICY[Rego Policies]
118
- end
119
-
120
- subgraph "Data Security"
121
- TLS[TLS 1.3<br/>mTLS Internal]
122
- ENCRYPT[AES-256-GCM]
123
- VAULT[(HashiCorp Vault)]
124
- end
125
-
126
- subgraph "Compliance"
127
- AUDIT[Audit Logger]
128
- GUARDRAILS2[Content Filter]
129
- PII[PII Detector]
130
- end
131
-
132
- CLIENT[Client] --> WAF
133
- WAF --> RATE_LIMIT
134
- RATE_LIMIT --> API_GATEWAY
135
- API_GATEWAY --> OIDC
136
- OIDC --> JWT
137
- JWT --> OPA
138
- OPA --> POLICY
139
- POLICY --> SERVICE[Core Services]
140
- SERVICE --> TLS
141
- SERVICE --> VAULT
142
- SERVICE --> AUDIT
143
- SERVICE --> GUARDRAILS2
144
- SERVICE --> PII
145
- ```
146
-
147
- ### Synchronous Query Flow
148
-
149
- ```mermaid
150
- sequenceDiagram
151
- participant Client
152
- participant API_Gateway
153
- participant Agent
154
- participant Router
155
- participant Guardrails
156
- participant Retriever
157
- participant Generator
158
- participant Cache
159
-
160
- Client->>API_Gateway: POST /query
161
- API_Gateway->>Agent: Forward query
162
-
163
- Agent->>Cache: Check cache
164
- alt Cache Hit
165
- Cache-->>Agent: Cached result
166
- Agent-->>Client: Response 200ms
167
- else Cache Miss
168
- Agent->>Router: RouteQuery()
169
- Router-->>Agent: RoutingDecision
170
-
171
- Agent->>Guardrails: ValidateQuery()
172
- Guardrails-->>Agent: Safe/Unsafe
173
-
174
- Agent->>Retriever: HybridSearch()
175
- Retriever-->>Agent: Top-K chunks
176
-
177
- Agent->>Generator: GenerateResponse()
178
- Generator-->>Agent: Answer
179
-
180
- Agent->>Cache: Store result
181
- Agent-->>Client: Response 2-3s
182
- end
183
- ```
184
-
185
- ### Agentic Multi-Step Flow
186
-
187
- ```mermaid
188
- sequenceDiagram
189
- participant Client
190
- participant Agent
191
- participant Planner
192
- participant Tools
193
- participant Evaluator
194
-
195
- Client->>Agent: Complex query
196
- Agent->>Planner: Create plan
197
- Planner-->>Agent: Multi-step plan
198
-
199
- loop For each step
200
- Agent->>Tools: Execute tool
201
- Tools-->>Agent: Result
202
- Agent->>Planner: Update plan
203
- end
204
-
205
- Agent->>Evaluator: Validate answer
206
- Evaluator-->>Agent: Score
207
-
208
- alt Score >= threshold
209
- Agent-->>Client: Final answer
210
- else Score < threshold
211
- Agent->>Planner: Revise plan
212
- end
213
- ```
214
-
215
- ### Data Ingestion Pipeline
216
-
217
- ```mermaid
218
- graph TB
219
- SRC[Data Sources<br/>PDF/DB/API] --> INGEST[Ingestion API]
220
-
221
- INGEST --> QUEUE1[Message Queue<br/>Kafka/RabbitMQ]
222
-
223
- QUEUE1 --> PREPROC[Preprocessor<br/>Go Workers]
224
-
225
- PREPROC --> CHUNK[Chunking Engine<br/>Semantic Splitter]
226
-
227
- CHUNK --> ENRICH[Enrichment<br/>Metadata/NER]
228
-
229
- ENRICH --> EMBED2[Embedding Worker<br/>Batched GPU]
230
-
231
- EMBED2 --> INDEX[Indexing Worker]
232
-
233
- INDEX --> VDB2[(Vector DB)]
234
- INDEX --> GRAPH2[(Graph DB)]
235
- INDEX --> CACHE3[(Cache)]
236
-
237
- style PREPROC fill:#4A90D9
238
- style CHUNK fill:#50C878
239
- style EMBED2 fill:#FF6B6B
240
- style INDEX fill:#DA70D6
241
- ```
242
-
243
- ### Multi-Tier Caching Strategy
244
-
245
- ```mermaid
246
- graph LR
247
- CLIENT[Query] --> CACHE1[L1 Cache<br/>CDN/Edge]
248
-
249
- CACHE1 -->|Miss| CACHE2[L2 Cache<br/>Redis Cluster]
250
-
251
- CACHE2 -->|Miss| CACHE3[L3 Compute Cache]
252
-
253
- CACHE3 -->|Miss| EMBED[Embedding Cache<br/>Local LRU]
254
-
255
- EMBED -->|Miss| MODEL[Embedding Model]
256
-
257
- style CACHE1 fill:#FF69B4
258
- style CACHE2 fill:#6495ED
259
- style CACHE3 fill:#90EE90
260
- style EMBED fill:#FFB6C1
261
- ```
262
-
263
- ---
264
-
265
- ## 🗂️ Project Structure
266
-
267
- ```
268
- AmaniQuery/
269
- ├── cmd/ # Application entry points
270
- │ ├── agent-server/ # Agent Orchestrator service
271
- │ ├── retriever-server/ # Retrieval service
272
- │ └── generator-server/ # LLM Gateway service
273
- ├── internal/ # Private application code
274
- │ ├── agent/ # Agent orchestration logic
275
- │ ├── retriever/ # Hybrid search implementation
276
- │ │ ├── vector/ # Vector store clients
277
- │ │ ├── keyword/ # BM25/Bleve implementation
278
- │ │ └── graph/ # Neo4j GraphRAG
279
- │ ├── generator/ # LLM client and prompting
280
- │ ├── router/ # Query classification
281
- │ ├── guardrails/ # Content safety filters
282
- │ ├── cache/ # Multi-tier caching
283
- │ └── security/ # Auth, encryption, policies
284
- ├── pkg/ # Public shared libraries
285
- │ ├── proto/ # gRPC/protobuf definitions
286
- │ ├── config/ # Configuration management
287
- │ ├── observability/ # Metrics, tracing, logging
288
- │ └── errors/ # Custom error types
289
- ├── api/ # REST API layer
290
- ├── deployments/ # Kubernetes/Helm configs
291
- │ ├── docker/ # Dockerfiles
292
- │ └── k8s/ # Kubernetes manifests
293
- ├── scripts/ # Build and deployment scripts
294
- ├── docs/ # Documentation
295
- └── tests/ # Integration tests
296
- ```
297
-
298
- ---
299
-
300
- ## 🚀 Quick Start
301
-
302
- ### Prerequisites
303
-
304
- - Go 1.21+
305
- - Docker & Docker Compose
306
- - Make
307
-
308
- ### Local Development
309
-
310
- ```bash
311
- # Clone the repository
312
- git clone https://github.com/amaniquery/amaniquery.git
313
- cd amaniquery
314
-
315
- # Install dependencies
316
- go mod download
317
-
318
- # Generate protobuf code
319
- make proto
320
-
321
- # Start infrastructure (Qdrant, Redis)
322
- make infra-up
323
-
324
- # Run the agent server
325
- make run-agent
326
-
327
- # Run tests
328
- make test
329
- ```
330
-
331
- ### Docker Compose
332
-
333
- ```bash
334
- # Start all services
335
- docker-compose up -d
336
-
337
- # View logs
338
- docker-compose logs -f agent-server
339
-
340
- # Stop services
341
- docker-compose down
342
- ```
343
-
344
- ---
345
-
346
- ## 📡 API Usage
347
-
348
- ### gRPC
349
-
350
- ```bash
351
- # List available services
352
- grpcurl -plaintext localhost:9090 list
353
-
354
- # Process a query
355
- grpcurl -plaintext -d '{
356
- "query": "What does the Kenya Constitution say about land rights?",
357
- "session_id": "user-123"
358
- }' localhost:9090 rag.v1.AgentService/ProcessQuery
359
- ```
360
-
361
- ### REST API
362
-
363
- ```bash
364
- # Health check
365
- curl http://localhost:8080/health
366
-
367
- # Process query
368
- curl -X POST http://localhost:8080/api/v1/query \
369
- -H "Content-Type: application/json" \
370
- -d '{"query": "Explain the Bill of Rights in Kenya"}'
371
- ```
372
-
373
- ---
374
-
375
- ## ⚙️ Configuration
376
-
377
- Configuration is managed through environment variables and YAML files:
378
-
379
- ```yaml
380
- # config.yaml
381
- server:
382
- grpc_port: 9090
383
- http_port: 8080
384
-
385
- vector_store:
386
- type: qdrant
387
- host: localhost
388
- port: 6333
389
- collection: amaniquery
390
-
391
- llm:
392
- provider: openai # openai, anthropic, google
393
- model: gpt-4o
394
- max_tokens: 4096
395
- temperature: 0.7
396
-
397
- cache:
398
- redis_url: redis://localhost:6379
399
- local_size: 10000
400
- ttl: 3600
401
-
402
- security:
403
- jwt_secret: ${JWT_SECRET}
404
- vault_addr: ${VAULT_ADDR}
405
- ```
406
-
407
  ---
408
-
409
- ## 📊 Performance Targets
410
-
411
- | Metric | Target | Description |
412
- |--------|--------|-------------|
413
- | P95 Latency | < 3s | 95th percentile query latency |
414
- | P99 Latency | < 5s | 99th percentile query latency |
415
- | Cache Hit Rate | > 85% | Query cache effectiveness |
416
- | Throughput | 10k docs/min | Document ingestion rate |
417
- | Concurrent Users | 10,000+ | Simultaneous connections |
418
- | Availability | 99.95% | Uptime SLA |
419
-
420
  ---
421
 
422
- ## 🔒 Security
423
 
424
- AmaniQuery implements enterprise-grade security:
425
 
426
- - **mTLS**: Service-to-service encryption
427
- - **JWT + OIDC**: Token-based authentication
428
- - **OPA/Rego**: Fine-grained authorization policies
429
- - **HashiCorp Vault**: Secrets management
430
- - **PII Detection**: Automatic sensitive data handling
431
- - **Audit Logging**: Immutable activity logs
432
- - **Rate Limiting**: DDoS protection
433
 
434
- ---
435
-
436
- ## 📈 Observability
437
-
438
- - **Metrics**: Prometheus + Grafana dashboards
439
- - **Tracing**: Jaeger distributed tracing
440
- - **Logging**: Structured JSON logs with correlation IDs
441
- - **Alerting**: PagerDuty/Slack integration
442
-
443
- ---
444
 
445
- ## 🤝 Contributing
446
 
447
- Contributions are welcome! Please read our [Contributing Guide](CONTRIBUTING.md) for details.
448
-
449
- ---
450
-
451
- ## 📄 License
452
-
453
- This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
454
-
455
- ---
456
-
457
- ## 🙏 Acknowledgments
458
-
459
- - Built with inspiration from NVIDIA's RAG Blueprint
460
- - Kenya Law Reports for legal data access
461
- - The Go community for excellent tooling
462
-
463
- ---
464
 
465
- <p align="center">
466
- <b>AmaniQuery</b> - Democratizing Access to Legal Knowledge
467
- </p>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: AmaniQuery Agent
3
+ emoji: ⚖️
4
+ colorFrom: blue
5
+ colorTo: green
6
+ sdk: docker
7
+ app_port: 7860
8
+ pinned: false
 
 
 
 
 
9
  ---
10
 
11
+ ## AmaniQuery Agent on Hugging Face Spaces
12
 
13
+ This Space hosts the AmaniQuery Agent API, a Go-based RAG service for legal and civic information in Kenya.
14
 
15
+ ## Configuration
 
 
 
 
 
 
16
 
17
+ This Space is configured using the Docker SDK. The Dockerfile is located at root.
 
 
 
 
 
 
 
 
 
18
 
19
+ ### Environment Variables
20
 
21
+ The following secrets must be set in the Space settings:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
+ - `OPENAI_API_KEY`: API key for OpenAI (if using OpenAI models)
24
+ - `QDRANT_URL`: URL for Qdrant vector database (e.g., Qdrant Cloud)
25
+ - `QDRANT_API_KEY`: API key for Qdrant
26
+ - `REDIS_URL`: URL for Redis cache (e.g., Upstash)
27
+ - `DB_HOST`: PostgreSQL host (e.g., Neon.tech)
28
+ - `DB_PORT`: PostgreSQL port
29
+ - `DB_USER`: PostgreSQL user
30
+ - `DB_PASSWORD`: PostgreSQL password
31
+ - `DB_NAME`: PostgreSQL database name
scripts/deploy_hf.py CHANGED
@@ -113,11 +113,8 @@ def deploy_service(service_type):
113
  print("Configuring deployment artifacts...")
114
  shutil.copy2(project_root / dockerfile_src, Path(temp_dir) / "Dockerfile")
115
 
116
- # Only copy README if it doesn't exist or we want to force update (usually we want to keep Space metadata)
117
- # For first deploy we absolutely need it.
118
- target_readme = Path(temp_dir) / "README.md"
119
- if not target_readme.exists():
120
- shutil.copy2(project_root / readme_src, target_readme)
121
 
122
  # Commit and push
123
  print("Pushing changes...")
 
113
  print("Configuring deployment artifacts...")
114
  shutil.copy2(project_root / dockerfile_src, Path(temp_dir) / "Dockerfile")
115
 
116
+ # Always overwrite README to ensure correct configuration
117
+ shutil.copy2(project_root / readme_src, Path(temp_dir) / "README.md")
 
 
 
118
 
119
  # Commit and push
120
  print("Pushing changes...")