-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
323 lines (266 loc) · 10.4 KB
/
main.py
File metadata and controls
323 lines (266 loc) · 10.4 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
import contextlib
from fastapi import FastAPI, HTTPException, BackgroundTasks
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from contextlib import asynccontextmanager
from schemas import QuestionRequest, AnswerResponse ,UrlInjectionRequest , MetadataQueryResponse, DeleteRequest
from vectordb import add_urls_to_vectorstore ,get_metadata_counts, delete_by_metadata ,get_qdrant_client
from graph import GraphBuilder
from tools import get_retriever_tool, refresh_retriever
from config import settings
from typing import Any
import os
import time
import io
import logging
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO").upper()
logging.basicConfig(
level=LOG_LEVEL,
format="%(asctime)s %(levelname)s [%(name)s] %(message)s",
)
logger = logging.getLogger("agentic_rag_api")
@asynccontextmanager
async def lifespan(app: FastAPI):
# Startup logic
_init_locks()
logger.info("Starting Agentic RAG API...")
try:
_compile_global_graph(refresh_tools=False)
logger.info("Startup complete.")
except Exception as e:
logger.exception("Failed to build initial graph: %s", e)
# Optionally re-raise if you want to fail fast
# raise
yield # App runs here
# Shutdown logic
logger.info("Shutting down Agentic RAG API.")
app = FastAPI(title="Agentic RAG API" , lifespan=lifespan)
app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:80"], # uncomment when using embedded dist
# allow_origins=["*"], # comment when using embedded dist
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# ------------------------------------------------------------------------------
# Global Graph State (hot-swappable)
# ------------------------------------------------------------------------------
_graph_lock = None
_graph = None
_tools = None
_last_refresh_ts = 0.0
_refresh_cooldown_sec = 5.0 # Debounce to avoid frequent recompiles
def _init_locks():
global _graph_lock
import threading
if _graph_lock is None:
_graph_lock = threading.RLock()
def _build_graph(tools) -> Any:
"""
Compile and return a graph using the provided tools and selected LLM.
"""
graph_builder = GraphBuilder(tools, llm_model=settings.LLM_MODEL)
return graph_builder.compile()
def _ensure_graph_visualization(graph) -> None:
"""
Generate and persist a visualization image of the graph.
Best-effort; errors are logged but not raised.
"""
try:
os.makedirs(settings.DOCUMENTS_DIR, exist_ok=True)
graph_image_path = os.path.join(settings.DOCUMENTS_DIR, "graph.png")
png_bytes = graph.get_graph(xray=True).draw_mermaid_png()
with open(graph_image_path, "wb") as f:
f.write(png_bytes)
logger.info("Saved graph visualization to %s", graph_image_path)
except Exception as e:
logger.warning("Failed to generate graph visualization: %s", e)
def _compile_global_graph(refresh_tools: bool = False) -> None:
"""
Build or rebuild the global tools and graph. Optionally refresh tools first.
"""
global _graph, _tools
if refresh_tools:
_tools = get_retriever_tool(refresh=True)
else:
_tools = _tools or get_retriever_tool(refresh=False)
_graph = _build_graph(_tools)
_ensure_graph_visualization(_graph)
logger.info("Graph compiled with model=%s", settings.LLM_MODEL)
def _maybe_refresh_graph_debounced(force: bool = False) -> bool:
"""
Recompile the graph if enough time has elapsed since the last refresh, or if forced.
Returns True if a refresh occurred.
"""
global _last_refresh_ts
now = time.perf_counter()
if not force and (now - _last_refresh_ts) < _refresh_cooldown_sec:
return False
_compile_global_graph(refresh_tools=True)
_last_refresh_ts = now
return True
# ------------------------------------------------------------------------------
# Background tasks
# ------------------------------------------------------------------------------
def refresh_retriever_background(force_graph_refresh: bool = True):
"""
Refresh retriever and optionally recompile the graph.
Safe to call from a background thread.
"""
try:
logger.info("Refreshing retriever in background...")
refresh_retriever()
if force_graph_refresh:
_maybe_refresh_graph_debounced(force=True)
logger.info("Retriever refresh complete.")
except Exception as e:
logger.exception("Background retriever refresh failed: %s", e)
# ------------------------------------------------------------------------------
# Helpers
# ------------------------------------------------------------------------------
def _stream_graph_answer(graph, user_question: str) -> str:
"""
Streams LangGraph updates to a buffer and returns the full formatted text.
This preserves your existing UX (pretty_print) while returning as text.
"""
buf = io.StringIO()
with contextlib.redirect_stdout(buf):
for chunk in graph.stream(
{
"messages": [
{
"role": "user",
"content": user_question,
}
]
}
):
for node, update in chunk.items():
print("Update from node", node)
# Guard in case structure changes
try:
update["messages"][-1].pretty_print()
except Exception:
print(update)
print("\n\n")
return buf.getvalue()
# ------------------------------------------------------------------------------
# Routes
# ------------------------------------------------------------------------------
@app.post("/ask", response_model=AnswerResponse)
async def ask_question(request: QuestionRequest):
"""
Ask a question to the Agentic RAG pipeline.
Returns the pretty-printed execution trace as the answer for transparency.
"""
start = time.perf_counter()
try:
# Defensive: build once if not available (e.g., lazy import scenarios)
global _graph
if _graph is None:
_compile_global_graph(refresh_tools=False)
answer_text = _stream_graph_answer(_graph, request.question)
elapsed = time.perf_counter() - start
return AnswerResponse(answer=answer_text, processing_time=f"{elapsed:.2f}")
except Exception as e:
logger.exception("Error in /ask: %s", e)
raise HTTPException(status_code=500, detail="Internal server error")
@app.post("/inject")
async def inject_urls(request: UrlInjectionRequest, background_tasks: BackgroundTasks):
"""
Inject URLs into the vector store, then schedule a retriever refresh in the background.
Returns partial success info if some URLs fail.
"""
try:
if not request.urls:
raise HTTPException(status_code=400, detail="No URLs provided")
added_count, errors = add_urls_to_vectorstore(request.urls)
# Schedule refresh even if partial failures occurred
background_tasks.add_task(refresh_retriever_background, True)
if errors:
return {
"message": f"Added {added_count} chunks with {len(errors)} errors",
"errors": errors,
"status": "partial_success",
"added_count": added_count,
}
return {
"message": f"Successfully added {added_count} chunks",
"status": "success",
"added_count": added_count,
}
except HTTPException:
raise
except Exception as e:
logger.exception("Unexpected error in /inject: %s", e)
raise HTTPException(status_code=500, detail="Internal server error")
@app.post("/delete_by_metadata")
async def delete_by_metadata_endpoint(request: DeleteRequest, background_tasks: BackgroundTasks):
"""
Delete vectors by metadata value (e.g., URL).
Always schedules a retriever refresh in the background.
"""
try:
if not request.url:
raise HTTPException(status_code=400, detail="Missing 'url' in request")
deleted_count = delete_by_metadata(request.url)
# Refresh retriever in background
background_tasks.add_task(refresh_retriever_background, True)
if deleted_count > 0:
return {
"message": f"Successfully deleted {deleted_count} chunks with metadata '{request.url}'",
"deleted_count": deleted_count,
"status": "success",
}
else:
return {
"message": f"No chunks found with metadata '{request.url}'",
"deleted_count": 0,
"status": "no_match",
}
except HTTPException:
raise
except Exception as e:
logger.exception("Error in /delete_by_metadata: %s", e)
raise HTTPException(status_code=500, detail="Internal server error")
@app.get("/metadata/counts", response_model=MetadataQueryResponse)
async def get_metadata_counts_endpoint():
"""Get counts of chunks by metadata"""
try:
counts = get_metadata_counts()
return MetadataQueryResponse(metadata_counts=counts)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/debug/points")
async def debug_points(limit: int = 1000):
"""Debug endpoint to see what's stored in the database"""
try:
client = get_qdrant_client()
points, _ = client.scroll(
collection_name=settings.COLLECTION_NAME,
limit=limit,
with_payload=True,
with_vectors=False
)
debug_points = []
for point in points:
debug_points.append({
"id": str(point.id),
"payload": point.payload,
# "vector": point.vector[:5] if point.vector else [] # First 5 elements
})
return points
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/api/config")
async def get_config():
ss = settings.EMBEDDINGS_MODEL
return {
"llm_model": settings.LLM_MODEL,
"embeddings_model": ss.rsplit("/", 1)[-1]
}
app.mount("/", StaticFiles(directory="dist", html=True), name="static") # uncomment when using embedded dist
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8001)