-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathKnowledge_API.py
More file actions
382 lines (324 loc) · 13.8 KB
/
Copy pathKnowledge_API.py
File metadata and controls
382 lines (324 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
"""
Knowledge API — Active InferAnts knowledge management service.
FastAPI service for managing knowledge items across multiple databases with
automatic synchronization. Written to be parse-correct and internally coherent:
database access is consistently synchronous (SQLAlchemy + sync clients), CORS is
restricted to configured origins, and every credential comes from the
environment rather than being hardcoded in source.
"""
import hmac
import logging
import os
from typing import Any
from fastapi import Depends, FastAPI, HTTPException, Query
from fastapi.middleware.cors import CORSMiddleware
from fastapi.openapi.utils import get_openapi
from fastapi.responses import RedirectResponse
from fastapi.security import APIKeyHeader
from pydantic import BaseModel, ConfigDict, Field, field_validator
from sqlalchemy import JSON, Column, DateTime, Integer, String, create_engine, func
from sqlalchemy.orm import Session, declarative_base, sessionmaker
# --------------------------------------------------------------------------- #
# Configuration (all from environment; no secrets in source)
# --------------------------------------------------------------------------- #
def _env(name: str, default: str = "") -> str:
return os.environ.get(name, default)
class Config:
SQLALCHEMY_DATABASE_URL = _env(
"KNOWLEDGE_DB_URL",
"sqlite:///./knowledge.db", # safe local default; override in production
)
MONGODB_URL = _env("KNOWLEDGE_MONGO_URL", "mongodb://localhost:27017/")
MONGO_DB = _env("KNOWLEDGE_MONGO_DB", "knowledge_db")
REDIS_URL = _env("KNOWLEDGE_REDIS_URL", "redis://localhost:6379/0")
ELASTICSEARCH_URL = _env("KNOWLEDGE_ES_URL", "http://localhost:9200")
ES_INDEX = _env("KNOWLEDGE_ES_INDEX", "knowledge")
NEO4J_URL = _env("KNOWLEDGE_NEO4J_URL", "bolt://localhost:7687")
NEO4J_USER = _env("KNOWLEDGE_NEO4J_USER", "neo4j")
NEO4J_PASSWORD = _env("KNOWLEDGE_NEO4J_PASSWORD", "")
# No default API key: authentication is enabled only when X-API-KEY is set in
# the environment. When unset the service still starts (local/dev use).
API_KEY = _env("KNOWLEDGE_API_KEY", "")
LOG_LEVEL = _env("KNOWLEDGE_LOG_LEVEL", "INFO")
# Comma-separated list of allowed CORS origins; never '*'.
ALLOW_ORIGINS = _env("KNOWLEDGE_ALLOW_ORIGINS", "http://localhost:8000")
config = Config()
logging.basicConfig(
level=getattr(logging, config.LOG_LEVEL.upper(), logging.INFO),
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)
logger = logging.getLogger("knowledge_api")
# --------------------------------------------------------------------------- #
# App + CORS
# --------------------------------------------------------------------------- #
app = FastAPI(
title="Knowledge API",
description="API for managing knowledge across multiple databases",
version="3.1.0",
docs_url="/api/docs",
redoc_url="/api/redoc",
openapi_url="/api/openapi.json",
)
app.add_middleware(
CORSMiddleware,
allow_origins=[o.strip() for o in config.ALLOW_ORIGINS.split(",") if o.strip()],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# --------------------------------------------------------------------------- #
# SQLAlchemy (consistently synchronous)
# --------------------------------------------------------------------------- #
engine = create_engine(
config.SQLALCHEMY_DATABASE_URL,
pool_size=20,
max_overflow=0,
connect_args={"check_same_thread": False}
if config.SQLALCHEMY_DATABASE_URL.startswith("sqlite")
else {},
)
SessionLocal = sessionmaker(bind=engine, autocommit=False, autoflush=False)
Base = declarative_base()
class KnowledgeItem(Base):
__tablename__ = "knowledge_items"
id = Column(Integer, primary_key=True, index=True)
source = Column(String, index=True, unique=True)
content = Column(JSON)
created_at = Column(DateTime, default=func.now())
updated_at = Column(DateTime, default=func.now(), onupdate=func.now())
Base.metadata.create_all(bind=engine)
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
# --------------------------------------------------------------------------- #
# API key authentication (constant-time compare; enabled only when configured)
# --------------------------------------------------------------------------- #
api_key_header = APIKeyHeader(name="X-API-Key", auto_error=False)
def get_api_key(api_key_header: str | None = Depends(api_key_header)) -> str | None:
if not config.API_KEY:
# No key configured -> open (local/dev) mode.
return None
if api_key_header and hmac.compare_digest(api_key_header, config.API_KEY):
return api_key_header
raise HTTPException(status_code=403, detail="Could not validate API key")
# --------------------------------------------------------------------------- #
# Pydantic models
# --------------------------------------------------------------------------- #
class KnowledgeBase(BaseModel):
source: str = Field(..., description="Unique identifier for the knowledge item")
content: dict[str, Any] = Field(..., description="Content of the knowledge item")
@field_validator("source")
@classmethod
def source_must_be_valid(cls, v):
if not v.strip():
raise ValueError("Source must not be empty")
return v
model_config = ConfigDict(
json_schema_extra={"example": {"source": "example_source", "content": {"key": "value"}}}
)
# --------------------------------------------------------------------------- #
# Optional sync clients for the secondary stores (gracefully Optional)
# --------------------------------------------------------------------------- #
def _get_clients():
"""Return sync clients for the configured secondary stores.
Each is lazy/optional: if the driver is unavailable or the store unreachable
the corresponding entry is None, and the sync helper ignores it. This keeps a
single-source installation (SQLite only) fully usable.
"""
clients = {"mongo": None, "redis": None, "es": None, "neo4j": None}
if config.MONGODB_URL:
try:
from pymongo import MongoClient
clients["mongo"] = MongoClient(config.MONGODB_URL, serverSelectionTimeoutMS=1000)
except Exception as e: # pragma: no cover - env dependent
logger.warning("Mongo unavailable: %s", e)
if config.REDIS_URL:
try:
import redis
clients["redis"] = redis.from_url(config.REDIS_URL, socket_connect_timeout=1)
except Exception as e: # pragma: no cover
logger.warning("Redis unavailable: %s", e)
if config.ELASTICSEARCH_URL:
try:
from elasticsearch import Elasticsearch
clients["es"] = Elasticsearch(
config.ELASTICSEARCH_URL, request_timeout=1, max_retries=0
)
except Exception as e: # pragma: no cover
logger.warning("Elasticsearch unavailable: %s", e)
if config.NEO4J_URL:
try:
from neo4j import GraphDatabase
clients["neo4j"] = GraphDatabase.driver(
config.NEO4J_URL,
auth=(config.NEO4J_USER, config.NEO4J_PASSWORD),
)
except Exception as e: # pragma: no cover
logger.warning("Neo4j unavailable: %s", e)
return clients
def _sync_to_secondary(clients, knowledge: KnowledgeBase) -> None:
"""Best-effort synchronous write fan-out to the configured secondary stores."""
payload = knowledge.model_dump()
if clients["mongo"] is not None:
try:
clients["mongo"][config.MONGO_DB]["knowledge_items"].replace_one(
{"source": knowledge.source}, payload, upsert=True
)
except Exception as e:
logger.warning("Mongo write failed: %s", e)
if clients["redis"] is not None:
try:
import json
clients["redis"].set(f"knowledge:{knowledge.source}", json.dumps(payload))
except Exception as e:
logger.warning("Redis write failed: %s", e)
if clients["es"] is not None:
try:
clients["es"].index(
index=config.ES_INDEX, id=knowledge.source, document=payload
)
except Exception as e:
logger.warning("Elasticsearch write failed: %s", e)
if clients["neo4j"] is not None:
try:
with clients["neo4j"].session() as session:
session.run(
"MERGE (k:Knowledge {source: $source}) SET k.content = $content",
source=knowledge.source, content=str(knowledge.content),
)
except Exception as e:
logger.warning("Neo4j write failed: %s", e)
# --------------------------------------------------------------------------- #
# Endpoints
# --------------------------------------------------------------------------- #
@app.post("/api/knowledge/", response_model=dict[str, Any], status_code=201)
def create_knowledge(
knowledge: KnowledgeBase,
db: Session = Depends(get_db),
_: str | None = Depends(get_api_key),
):
try:
existing = (
db.query(KnowledgeItem).filter(KnowledgeItem.source == knowledge.source).first()
)
if existing:
raise HTTPException(status_code=409, detail="Knowledge source already exists")
item = KnowledgeItem(source=knowledge.source, content=knowledge.content)
db.add(item)
db.commit()
db.refresh(item)
_sync_to_secondary(_get_clients(), knowledge)
logger.info("Knowledge created: %s", knowledge.source)
return {"message": "Knowledge created successfully", "id": item.id}
except HTTPException:
raise
except Exception as e:
logger.error("Error creating knowledge: %s", e)
raise HTTPException(status_code=500, detail="Error creating knowledge") from None
@app.get("/api/knowledge/{source}", response_model=dict[str, Any])
def read_knowledge(
source: str,
db: Session = Depends(get_db),
_: str | None = Depends(get_api_key),
):
try:
item = db.query(KnowledgeItem).filter(KnowledgeItem.source == source).first()
if not item:
raise HTTPException(status_code=404, detail="Knowledge not found")
return {"source": item.source, "content": item.content}
except HTTPException:
raise
except Exception as e:
logger.error("Error retrieving knowledge: %s", e)
raise HTTPException(status_code=500, detail="Error retrieving knowledge") from None
@app.put("/api/knowledge/{source}", response_model=dict[str, str])
def update_knowledge(
source: str,
knowledge: KnowledgeBase,
db: Session = Depends(get_db),
_: str | None = Depends(get_api_key),
):
try:
item = db.query(KnowledgeItem).filter(KnowledgeItem.source == source).first()
if not item:
raise HTTPException(status_code=404, detail="Knowledge not found")
item.content = knowledge.content
item.updated_at = func.now()
db.commit()
_sync_to_secondary(_get_clients(), knowledge)
logger.info("Knowledge updated: %s", source)
return {"message": "Knowledge updated successfully"}
except HTTPException:
raise
except Exception as e:
logger.error("Error updating knowledge: %s", e)
raise HTTPException(status_code=500, detail="Error updating knowledge") from None
@app.delete("/api/knowledge/{source}", response_model=dict[str, str])
def delete_knowledge(
source: str,
db: Session = Depends(get_db),
_: str | None = Depends(get_api_key),
):
try:
item = db.query(KnowledgeItem).filter(KnowledgeItem.source == source).first()
if not item:
raise HTTPException(status_code=404, detail="Knowledge not found")
db.delete(item)
db.commit()
logger.info("Knowledge deleted: %s", source)
return {"message": "Knowledge deleted successfully"}
except HTTPException:
raise
except Exception as e:
logger.error("Error deleting knowledge: %s", e)
raise HTTPException(status_code=500, detail="Error deleting knowledge") from None
@app.get("/api/knowledge/", response_model=list[dict[str, Any]])
def list_knowledge(
skip: int = Query(0, description="Number of items to skip"),
limit: int = Query(10, description="Number of items to return"),
db: Session = Depends(get_db),
_: str | None = Depends(get_api_key),
):
try:
items = db.query(KnowledgeItem).order_by(KnowledgeItem.id).offset(skip).limit(limit).all()
return [
{
"id": item.id,
"source": item.source,
"content": item.content,
"created_at": item.created_at.isoformat() if item.created_at else None,
"updated_at": item.updated_at.isoformat() if item.updated_at else None,
}
for item in items
]
except Exception as e:
logger.error("Error listing knowledge: %s", e)
raise HTTPException(status_code=500, detail="Error listing knowledge") from None
@app.get("/", include_in_schema=False)
def root_redirect():
return RedirectResponse(url="/api/docs")
@app.get("/api/health", include_in_schema=False)
def health_check():
return {"status": "ok"}
# --------------------------------------------------------------------------- #
# Custom OpenAPI + entries
# --------------------------------------------------------------------------- #
def custom_openapi():
if app.openapi_schema:
return app.openapi_schema
schema = get_openapi(
title=app.title,
version=app.version,
description=app.description,
routes=app.routes,
)
schema["servers"] = [{"url": "/"}]
app.openapi_schema = schema
return schema
app.openapi = custom_openapi
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)