@@ -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 (
0 commit comments