-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
144 lines (123 loc) · 4.37 KB
/
main.py
File metadata and controls
144 lines (123 loc) · 4.37 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
from fastapi import Request, FastAPI, File, UploadFile, Form, HTTPException, Depends
import json
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import RedirectResponse, JSONResponse
from models.models import Question, GraphRAGResponse, FileUpload, PageRange
from services.graph_rag import GraphRAGService
from services.pdf_processor import PDFProcessor
import logging
import sys
from pathlib import Path
app = FastAPI()
graph_rag_service = GraphRAGService()
# Create logs directory
Path("logs").mkdir(exist_ok=True)
# Configure error logging
logging.basicConfig(
level=logging.ERROR,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('logs/error.log'),
logging.StreamHandler(sys.stdout)
]
)
# Add error handler
@app.exception_handler(Exception)
async def global_exception_handler(request: Request, exc: Exception):
logging.error(f"Error processing request: {str(exc)}", exc_info=True)
return JSONResponse(
status_code=500,
content={"detail": str(exc)}
)
@app.exception_handler(Exception)
async def global_exception_handler(request: Request, exc: Exception):
error_msg = f"Exception type: {type(exc).__name__}\nError: {str(exc)}\nPath: {request.url.path}"
if hasattr(exc, '__traceback__'):
import traceback
error_msg += f"\nTraceback:\n{''.join(traceback.format_tb(exc.__traceback__))}"
logging.error(error_msg)
return JSONResponse(
status_code=500,
content={"detail": str(exc)}
)
# Add for async errors
@app.middleware("http")
async def catch_exceptions_middleware(request: Request, call_next):
try:
return await call_next(request)
except Exception as exc:
error_msg = f"Async Exception type: {type(exc).__name__}\nError: {str(exc)}\nPath: {request.url.path}"
if hasattr(exc, '__traceback__'):
import traceback
error_msg += f"\nTraceback:\n{''.join(traceback.format_tb(exc.__traceback__))}"
logging.error(error_msg)
return JSONResponse(status_code=500, content={"detail": str(exc)})
async def process_file_upload(
file: UploadFile,
page_range: PageRange
) -> str:
content = await file.read()
if file.filename.endswith('.pdf'):
return PDFProcessor.process_pdf(
content,
start_page=None if page_range.all_pages else page_range.start,
end_page=None if page_range.all_pages else page_range.end
)
elif file.filename.endswith('.txt'):
return content.decode('utf-8')
else:
raise HTTPException(400, "Unsupported file format")
# CORS middleware configuration
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/")
async def read_root():
"""Root endpoint that redirects to the API documentation."""
return RedirectResponse(url="/docs")
@app.post("/upload/")
async def upload_file(
file: UploadFile = File(...),
page_range: str = Form(default='{"all_pages": true}')
):
"""
Upload a PDF or TXT file to create a knowledge graph.
Specify page range for PDF files:
- all_pages=true for all pages
- start and end for specific range
"""
try:
page_range_dict = json.loads(page_range)
file_upload = FileUpload(page_range=PageRange(**page_range_dict))
content = await process_file_upload(file, file_upload.page_range)
await graph_rag_service.process_document(content)
return {
"message": "File processed successfully",
"pages_processed": file_upload.page_range
}
except Exception as e:
raise HTTPException(500, str(e))
@app.post("/ask/", response_model=GraphRAGResponse)
async def ask_question(question: Question):
"""
Ask a question and get a response using GraphRAG.
"""
try:
response = await graph_rag_service.get_answer(question.text)
return GraphRAGResponse(response=response)
except ValueError as e:
raise HTTPException(400, str(e))
@app.post("/similar/")
async def get_similar(question: Question):
"""
Get similar nodes from the knowledge graph.
"""
try:
nodes = await graph_rag_service.get_similar_nodes(question.text)
return {"similar_nodes": nodes}
except ValueError as e:
raise HTTPException(400, str(e))