Skip to content

Commit 0a24e21

Browse files
DevMacDevMac
authored andcommitted
fix(api): keep /ask-safe metrics isolated from /ask
1 parent b1fa76f commit 0a24e21

2 files changed

Lines changed: 20 additions & 6 deletions

File tree

src/rag_pipeline/api.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -838,35 +838,47 @@ def ingest(req: IngestRequest) -> IngestResponse:
838838
chunks_indexed=len(chunks), index_path=str(state["index_path"])
839839
)
840840

841-
@app.post("/ask", response_model=AskResponse)
842-
def ask(req: AskRequest) -> AskResponse:
843-
state["ask_requests_total"] += 1
841+
def _answer(req: AskRequest, *, record_ask_metrics: bool) -> AskResponse:
844842
started = time.perf_counter()
845843
rag = state["rag"]
846844
if rag is None:
847-
state["ask_errors_total"] += 1
845+
if record_ask_metrics:
846+
state["ask_errors_total"] += 1
848847
raise HTTPException(
849848
status_code=400, detail="index not loaded; run ingest first"
850849
)
850+
851851
result = rag.answer_with_citations(
852852
req.query,
853853
top_k=req.top_k,
854854
min_score=req.min_score,
855855
doc_id=req.doc_id,
856856
doc_id_contains=req.doc_id_contains,
857857
)
858-
_record_latency(state["ask_latency_seconds"], time.perf_counter() - started)
858+
if record_ask_metrics:
859+
_record_latency(state["ask_latency_seconds"], time.perf_counter() - started)
860+
859861
citations = citations_to_dict(result.citations)
860862
return AskResponse(
861863
answer=result.answer,
862864
citations=citations,
863865
citations_count=len(citations),
864866
)
865867

868+
@app.post("/ask", response_model=AskResponse)
869+
def ask(req: AskRequest) -> AskResponse:
870+
state["ask_requests_total"] += 1
871+
return _answer(req, record_ask_metrics=True)
872+
866873
@app.post("/ask-safe", response_model=AskResponse)
867874
def ask_safe(req: AskRequest) -> AskResponse:
868875
state["ask_safe_requests_total"] += 1
869-
response = ask(req)
876+
try:
877+
response = _answer(req, record_ask_metrics=False)
878+
except HTTPException:
879+
state["ask_safe_errors_total"] += 1
880+
raise
881+
870882
if not response.citations:
871883
state["ask_safe_errors_total"] += 1
872884
raise HTTPException(

tests/test_api_cli.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,8 @@ def test_api_ask_safe_requires_citations(tmp_path: Path):
402402
stats = client.get("/stats")
403403
assert stats.status_code == 200
404404
stats_payload = stats.json()
405+
assert stats_payload["counters"]["ask"] == 0
406+
assert stats_payload["counters"]["ask_errors"] == 0
405407
assert stats_payload["counters"]["ask_safe"] == 2
406408
assert stats_payload["counters"]["ask_safe_errors"] == 1
407409

0 commit comments

Comments
 (0)