Kashish commited on
Commit ·
7f32096
1
Parent(s): 5d3d3bd
segregated response keys
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ from __future__ import annotations
|
|
| 3 |
from fastapi import FastAPI, HTTPException
|
| 4 |
from fastapi.responses import RedirectResponse
|
| 5 |
from pydantic import BaseModel, Field
|
|
|
|
| 6 |
|
| 7 |
from rag.chain import aanswer
|
| 8 |
from rag.retriever import get_retriever
|
|
@@ -24,6 +25,7 @@ class ChatRequest(BaseModel):
|
|
| 24 |
class ChatResponse(BaseModel):
|
| 25 |
question: str
|
| 26 |
answer: str
|
|
|
|
| 27 |
|
| 28 |
|
| 29 |
@app.get("/health")
|
|
@@ -44,4 +46,15 @@ async def chat(payload: ChatRequest) -> ChatResponse:
|
|
| 44 |
except Exception as error:
|
| 45 |
raise HTTPException(status_code=500, detail=str(error))
|
| 46 |
|
| 47 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from fastapi import FastAPI, HTTPException
|
| 4 |
from fastapi.responses import RedirectResponse
|
| 5 |
from pydantic import BaseModel, Field
|
| 6 |
+
import re
|
| 7 |
|
| 8 |
from rag.chain import aanswer
|
| 9 |
from rag.retriever import get_retriever
|
|
|
|
| 25 |
class ChatResponse(BaseModel):
|
| 26 |
question: str
|
| 27 |
answer: str
|
| 28 |
+
sources: list[str] = Field(default_factory=list)
|
| 29 |
|
| 30 |
|
| 31 |
@app.get("/health")
|
|
|
|
| 46 |
except Exception as error:
|
| 47 |
raise HTTPException(status_code=500, detail=str(error))
|
| 48 |
|
| 49 |
+
# Extract trailing "Sources:" block if present and return as separate list
|
| 50 |
+
m = re.search(r"(?i)\bSources\s*:?\s*(.*)$", answer, flags=re.DOTALL)
|
| 51 |
+
sources: list[str] = []
|
| 52 |
+
if m:
|
| 53 |
+
sources_text = m.group(1).strip()
|
| 54 |
+
# Remove the Sources block from the answer text
|
| 55 |
+
answer = answer[: m.start()].strip()
|
| 56 |
+
# Split on commas and newlines, strip whitespace, ignore empties
|
| 57 |
+
parts = re.split(r"[,\n;]+", sources_text)
|
| 58 |
+
sources = [p.strip() for p in parts if p.strip()]
|
| 59 |
+
|
| 60 |
+
return ChatResponse(question=payload.question, answer=answer, sources=sources)
|