modified: blueprints/chat.py
modified: services/llm_service.py modified: services/rag_service.py modified: static/js/chat.js
This commit is contained in:
@@ -47,6 +47,22 @@ def get_messages(session_id):
|
||||
return jsonify([m.to_dict() for m in session.messages])
|
||||
|
||||
|
||||
# ── Diagnostics ─────────────────────────────────────────────────────────────
|
||||
|
||||
@chat_bp.route("/ping-llm", methods=["GET"])
|
||||
@login_required
|
||||
def ping_llm():
|
||||
"""Test LM Studio connectivity. Returns config + a short completion."""
|
||||
url = current_app.config.get("LM_STUDIO_URL", "")
|
||||
model = current_app.config.get("LM_STUDIO_MODEL", "")
|
||||
embed_model = current_app.config.get("LM_STUDIO_EMBEDDING_MODEL", "")
|
||||
try:
|
||||
reply = llm_service.ask(user_message="Reply with exactly: OK", context_chunks=[], history=[])
|
||||
return jsonify({"status": "ok", "reply": reply, "url": url, "model": model, "embed_model": embed_model})
|
||||
except Exception as e:
|
||||
return jsonify({"status": "error", "error": str(e), "url": url, "model": model, "embed_model": embed_model}), 502
|
||||
|
||||
|
||||
# ── Main chat ────────────────────────────────────────────────────────────────
|
||||
|
||||
@chat_bp.route("/sessions/<int:session_id>/ask", methods=["POST"])
|
||||
@@ -64,31 +80,33 @@ def ask(session_id):
|
||||
doc_ids = [r["id"] for r in context_refs if r.get("type") == "doc"]
|
||||
url_ids = [r["id"] for r in context_refs if r.get("type") == "url"]
|
||||
|
||||
# RAG lookup
|
||||
# RAG lookup — failures are non-fatal (chat continues without context)
|
||||
chunks = []
|
||||
if doc_ids:
|
||||
chunks += rag_service.similarity_search(
|
||||
query=message,
|
||||
user_id=current_user.id,
|
||||
source_ids=doc_ids,
|
||||
source_type="doc",
|
||||
top_k=current_app.config["RAG_TOP_K"],
|
||||
)
|
||||
if url_ids:
|
||||
chunks += rag_service.similarity_search(
|
||||
query=message,
|
||||
user_id=current_user.id,
|
||||
source_ids=url_ids,
|
||||
source_type="url",
|
||||
top_k=current_app.config["RAG_TOP_K"],
|
||||
)
|
||||
# If no specific ids given, search all user context
|
||||
if not context_refs:
|
||||
chunks = rag_service.similarity_search(
|
||||
query=message,
|
||||
user_id=current_user.id,
|
||||
top_k=current_app.config["RAG_TOP_K"],
|
||||
)
|
||||
try:
|
||||
if doc_ids:
|
||||
chunks += rag_service.similarity_search(
|
||||
query=message,
|
||||
user_id=current_user.id,
|
||||
source_ids=doc_ids,
|
||||
source_type="doc",
|
||||
top_k=current_app.config["RAG_TOP_K"],
|
||||
)
|
||||
if url_ids:
|
||||
chunks += rag_service.similarity_search(
|
||||
query=message,
|
||||
user_id=current_user.id,
|
||||
source_ids=url_ids,
|
||||
source_type="url",
|
||||
top_k=current_app.config["RAG_TOP_K"],
|
||||
)
|
||||
if not context_refs:
|
||||
chunks = rag_service.similarity_search(
|
||||
query=message,
|
||||
user_id=current_user.id,
|
||||
top_k=current_app.config["RAG_TOP_K"],
|
||||
)
|
||||
except Exception as e:
|
||||
current_app.logger.warning(f"RAG lookup failed, continuing without context: {e}")
|
||||
|
||||
# Build history (last 10 messages for context window)
|
||||
history = [
|
||||
|
||||
Reference in New Issue
Block a user