modified: blueprints/chat.py
modified: services/rag_service.py
This commit is contained in:
@@ -63,6 +63,36 @@ def ping_llm():
|
||||
return jsonify({"status": "error", "error": str(e), "url": url, "model": model, "embed_model": embed_model}), 502
|
||||
|
||||
|
||||
@chat_bp.route("/debug-rag", methods=["GET"])
|
||||
@login_required
|
||||
def debug_rag():
|
||||
"""Diagnose RAG: show collection size, run a test query, return raw distances."""
|
||||
query = request.args.get("q", "test")
|
||||
source_id = request.args.get("source_id", type=int)
|
||||
source_type = request.args.get("source_type", "doc")
|
||||
try:
|
||||
collection = rag_service._get_collection()
|
||||
total_chunks = collection.count()
|
||||
source_ids = [source_id] if source_id else None
|
||||
raw = collection.query(
|
||||
query_texts=[query],
|
||||
n_results=min(5, max(1, total_chunks)),
|
||||
where=rag_service._build_where(current_user.id, source_ids, source_type if source_id else None),
|
||||
include=["documents", "distances", "ids"],
|
||||
)
|
||||
hits = [
|
||||
{"id": i, "distance": round(d, 4), "preview": t[:120]}
|
||||
for i, d, t in zip(
|
||||
(raw.get("ids") or [[]])[0],
|
||||
(raw.get("distances") or [[]])[0],
|
||||
(raw.get("documents") or [[]])[0],
|
||||
)
|
||||
]
|
||||
return jsonify({"total_chunks": total_chunks, "query": query, "hits": hits})
|
||||
except Exception as e:
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
||||
|
||||
# ── Main chat ────────────────────────────────────────────────────────────────
|
||||
|
||||
@chat_bp.route("/sessions/<int:session_id>/ask", methods=["POST"])
|
||||
@@ -108,6 +138,8 @@ def ask(session_id):
|
||||
except Exception as e:
|
||||
current_app.logger.warning(f"RAG lookup failed, continuing without context: {e}")
|
||||
|
||||
current_app.logger.info(f"RAG: found {len(chunks)} chunks for session {session_id}")
|
||||
|
||||
# Build history (last 10 messages for context window)
|
||||
history = [
|
||||
{"role": m.role, "content": m.content}
|
||||
|
||||
Reference in New Issue
Block a user