Feedback UI

This commit is contained in:
Lars 2025-12-09 22:27:56 +01:00
parent 76ea8e3350
commit 2d0913cf3b
2 changed files with 121 additions and 127 deletions

View File

@ -2,22 +2,17 @@ import streamlit as st
import requests import requests
import uuid import uuid
import os import os
import json import time
from datetime import datetime
from dotenv import load_dotenv from dotenv import load_dotenv
# --- CONFIGURATION --- # --- CONFIGURATION ---
# Load .env file explicitly to get timeouts and URLs
load_dotenv() load_dotenv()
API_BASE_URL = os.getenv("MINDNET_API_URL", "http://localhost:8002") API_BASE_URL = os.getenv("MINDNET_API_URL", "http://localhost:8002")
CHAT_ENDPOINT = f"{API_BASE_URL}/chat" CHAT_ENDPOINT = f"{API_BASE_URL}/chat"
FEEDBACK_ENDPOINT = f"{API_BASE_URL}/feedback" FEEDBACK_ENDPOINT = f"{API_BASE_URL}/feedback"
# Timeout strategy: # Timeout-Strategie
# 1. Try MINDNET_API_TIMEOUT (specific for frontend)
# 2. Try MINDNET_LLM_TIMEOUT (backend setting)
# 3. Default to 300 seconds (5 minutes) for local inference safety
timeout_setting = os.getenv("MINDNET_API_TIMEOUT") or os.getenv("MINDNET_LLM_TIMEOUT") timeout_setting = os.getenv("MINDNET_API_TIMEOUT") or os.getenv("MINDNET_LLM_TIMEOUT")
API_TIMEOUT = float(timeout_setting) if timeout_setting else 300.0 API_TIMEOUT = float(timeout_setting) if timeout_setting else 300.0
@ -28,16 +23,10 @@ st.set_page_config(
layout="centered" layout="centered"
) )
# Custom CSS for cleaner look
st.markdown(""" st.markdown("""
<style> <style>
.reportview-container { margin-top: -2em; } .reportview-container { margin-top: -2em; }
.stDeployButton {display:none;} .stDeployButton {display:none;}
.stMainMenu {visibility: hidden;}
div[data-testid="stExpander"] div[role="button"] p {
font-size: 0.9rem;
font-weight: 600;
}
.intent-badge { .intent-badge {
background-color: #f0f2f6; background-color: #f0f2f6;
border-radius: 5px; border-radius: 5px;
@ -48,202 +37,202 @@ st.markdown("""
display: inline-block; display: inline-block;
border: 1px solid #e0e0e0; border: 1px solid #e0e0e0;
} }
.source-feedback {
font-size: 0.8em;
color: #888;
}
</style> </style>
""", unsafe_allow_html=True) """, unsafe_allow_html=True)
# --- SESSION STATE INITIALIZATION --- # --- SESSION STATE ---
if "messages" not in st.session_state: if "messages" not in st.session_state:
st.session_state.messages = [] st.session_state.messages = []
if "user_id" not in st.session_state: if "user_id" not in st.session_state:
st.session_state.user_id = str(uuid.uuid4()) st.session_state.user_id = str(uuid.uuid4())
# --- API CLIENT FUNCTIONS --- # --- API FUNCTIONS ---
def send_chat_message(message: str, top_k: int, explain: bool): def send_chat_message(message: str, top_k: int, explain: bool):
"""Sends the user message to the FastAPI backend.""" payload = {"message": message, "top_k": top_k, "explain": explain}
payload = {
"message": message,
"top_k": top_k,
"explain": explain
}
try: try:
# Use the configured timeout from .env
response = requests.post(CHAT_ENDPOINT, json=payload, timeout=API_TIMEOUT) response = requests.post(CHAT_ENDPOINT, json=payload, timeout=API_TIMEOUT)
response.raise_for_status() response.raise_for_status()
return response.json() return response.json()
except requests.exceptions.ReadTimeout: except requests.exceptions.ReadTimeout:
return {"error": f"Timeout: Das Backend hat nicht innerhalb von {int(API_TIMEOUT)} Sekunden geantwortet. (Local LLM is busy)."} return {"error": f"Timeout ({int(API_TIMEOUT)}s). Das lokale LLM rechnet noch."}
except requests.exceptions.ConnectionError:
return {"error": f"Backend nicht erreichbar unter {API_BASE_URL}. Läuft der Server?"}
except Exception as e: except Exception as e:
return {"error": str(e)} return {"error": str(e)}
def send_feedback(query_id: str, score: int): def submit_feedback(query_id: str, node_id: str, score: int, comment: str = None):
"""Sends feedback to the backend.""" """Sendet Feedback asynchron."""
# Note: We rate the overall answer. API expects node_id.
# We use 'generated_answer' as a convention for the full response.
payload = { payload = {
"query_id": query_id, "query_id": query_id,
"node_id": "generated_answer", "node_id": node_id,
"score": score, "score": score,
"comment": "User feedback via Streamlit UI" "comment": comment
} }
try: try:
requests.post(FEEDBACK_ENDPOINT, json=payload, timeout=5) requests.post(FEEDBACK_ENDPOINT, json=payload, timeout=5)
return True # Wir nutzen st.toast für dezentes Feedback ohne Rerun
except: target = "Antwort" if node_id == "generated_answer" else "Quelle"
return False st.toast(f"Feedback für {target} gespeichert! (Score: {score})")
except Exception as e:
st.error(f"Feedback-Fehler: {e}")
# --- UI COMPONENTS --- # --- UI COMPONENTS ---
def render_sidebar(): def render_sidebar():
with st.sidebar: with st.sidebar:
st.header("⚙️ Konfiguration") st.header("⚙️ Konfiguration")
st.markdown(f"**Backend:** `{API_BASE_URL}`") st.caption(f"Backend: `{API_BASE_URL}`")
st.caption(f"⏱️ Timeout: {int(API_TIMEOUT)}s")
st.markdown("---") st.subheader("Retrieval")
st.subheader("Retrieval Settings") top_k = st.slider("Quellen Anzahl", 1, 10, 5)
top_k = st.slider("Quellen (Top-K)", min_value=1, max_value=10, value=5) explain_mode = st.toggle("Explanation Layer", value=True)
explain_mode = st.checkbox("Explanation Layer", value=True, help="Zeigt an, warum Quellen gewählt wurden.")
st.markdown("---") st.divider()
st.markdown("### 🧠 System Status") st.info("WP-10: Advanced Feedback Loop Active")
st.info(f"**Version:** v2.3.1\n\n**Modules:**\n- Decision Engine: ✅\n- Hybrid Router: ✅\n- Feedback Loop: ✅") if st.button("Reset Chat"):
if st.button("Clear Chat History"):
st.session_state.messages = [] st.session_state.messages = []
st.rerun() st.rerun()
return top_k, explain_mode return top_k, explain_mode
def render_intent_badge(intent, source): def render_intent_badge(intent, source):
"""Visualizes the Decision Engine state."""
icon = "🧠" icon = "🧠"
if intent == "EMPATHY": icon = "❤️" if intent == "EMPATHY": icon = "❤️"
elif intent == "DECISION": icon = "⚖️" elif intent == "DECISION": icon = "⚖️"
elif intent == "CODING": icon = "💻" elif intent == "CODING": icon = "💻"
elif intent == "FACT": icon = "📚" elif intent == "FACT": icon = "📚"
return f"""<div class="intent-badge">{icon} <b>Intent:</b> {intent} <span style="color:#999">({source})</span></div>"""
return f""" def render_sources(sources, query_id):
<div class="intent-badge"> """
{icon} <b>Intent:</b> {intent} <span style="color:#999">({source})</span> Rendert Quellen inklusive granularem Feedback-Mechanismus.
</div>
""" """
def render_sources(sources):
"""Renders the retrieved sources in expandable cards."""
if not sources: if not sources:
return return
st.markdown("#### 📚 Verwendete Quellen") st.markdown("#### 📚 Verwendete Quellen")
for idx, hit in enumerate(sources): for idx, hit in enumerate(sources):
score = hit.get('total_score', 0) score = hit.get('total_score', 0)
node_id = hit.get('node_id')
title = hit.get('note_id', 'Unbekannt')
payload = hit.get('payload', {}) payload = hit.get('payload', {})
note_type = payload.get('type', 'unknown') note_type = payload.get('type', 'unknown')
title = hit.get('note_id', 'Unbekannt')
# Determine Header Color/Icon based on score # Icon basierend auf Score
score_icon = "🟢" if score > 0.8 else "🟡" if score > 0.5 else "" score_icon = "🟢" if score > 0.8 else "🟡" if score > 0.5 else ""
expander_title = f"{score_icon} {title} (Typ: {note_type}, Score: {score:.2f})"
with st.expander(f"{score_icon} {title} (Typ: {note_type}, Score: {score:.2f})"): with st.expander(expander_title):
# Content # 1. Inhalt
content = hit.get('source', {}).get('text', 'Kein Text verfügbar.') text = hit.get('source', {}).get('text', 'Kein Text')
st.markdown(f"_{content[:300]}..._") st.markdown(f"_{text[:300]}..._")
# Explanation (WP-04b) # 2. Explanation (Why-Layer)
explanation = hit.get('explanation') if 'explanation' in hit and hit['explanation']:
if explanation: st.caption("**Warum gefunden?**")
st.markdown("---") for r in hit['explanation'].get('reasons', []):
st.caption("**Warum wurde das gefunden?**")
reasons = explanation.get('reasons', [])
for r in reasons:
st.caption(f"- {r.get('message')}") st.caption(f"- {r.get('message')}")
# --- MAIN APP LOGIC --- # 3. Granulares Feedback (Source Level)
st.markdown("---")
c1, c2 = st.columns([3, 1])
with c1:
st.caption("War diese Quelle hilfreich für die Antwort?")
with c2:
# Callback Wrapper für Source-Feedback
def on_source_fb(qid=query_id, nid=node_id, k=f"fb_src_{node_id}"):
val = st.session_state.get(k)
# Mapping: Thumbs Up (1) -> Score 5, Thumbs Down (0) -> Score 1
mapped_score = 5 if val == 1 else 1
submit_feedback(qid, nid, mapped_score, comment="Source Feedback via UI")
top_k_setting, explain_setting = render_sidebar() st.feedback(
"thumbs",
key=f"fb_src_{query_id}_{node_id}", # Unique Key pro Query/Node
on_change=on_source_fb,
kwargs={"qid": query_id, "nid": node_id, "k": f"fb_src_{query_id}_{node_id}"}
)
# --- MAIN APP ---
top_k, show_explain = render_sidebar()
st.title("mindnet v2.3.1") st.title("mindnet v2.3.1")
st.caption("Lead Frontend Architect Edition | WP-10 Chat Interface")
# 1. Render History # 1. Chat History Rendern
for msg in st.session_state.messages: for msg in st.session_state.messages:
with st.chat_message(msg["role"]): with st.chat_message(msg["role"]):
if msg["role"] == "assistant": if msg["role"] == "assistant":
# Render Meta-Data first # Meta-Daten
if "intent" in msg: if "intent" in msg:
st.markdown(render_intent_badge(msg["intent"], msg.get("intent_source", "?")), unsafe_allow_html=True) st.markdown(render_intent_badge(msg["intent"], msg.get("intent_source", "?")), unsafe_allow_html=True)
# Antwort-Text
st.markdown(msg["content"]) st.markdown(msg["content"])
# Render Sources # Quellen (mit Feedback-Option, aber Status ist readonly für alte Nachrichten in Streamlit oft schwierig,
# daher rendern wir Feedback-Controls idealerweise nur für die letzte Nachricht oder speichern Status)
# In dieser Version rendern wir sie immer, Streamlit State managed das.
if "sources" in msg: if "sources" in msg:
render_sources(msg["sources"]) render_sources(msg["sources"], msg["query_id"])
# Render Latency info # Globales Feedback (Sterne)
if "latency_ms" in msg: qid = msg["query_id"]
st.caption(f"⏱️ Antwortzeit: {msg['latency_ms']}ms | Query-ID: `{msg.get('query_id')}`")
def on_global_fb(q=qid, k=f"fb_glob_{qid}"):
val = st.session_state.get(k) # Liefert 0-4
if val is not None:
submit_feedback(q, "generated_answer", val + 1, comment="Global Star Rating")
st.caption("Wie gut war diese Antwort?")
st.feedback(
"stars",
key=f"fb_glob_{qid}",
on_change=on_global_fb
)
else: else:
st.markdown(msg["content"]) st.markdown(msg["content"])
# 2. Handle User Input # 2. User Input
if prompt := st.chat_input("Was beschäftigt dich?"): if prompt := st.chat_input("Deine Frage an das System..."):
# Add User Message # User Message anzeigen
st.session_state.messages.append({"role": "user", "content": prompt}) st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"): with st.chat_message("user"):
st.markdown(prompt) st.markdown(prompt)
# Generate Response # API Call
with st.chat_message("assistant"): with st.chat_message("assistant"):
message_placeholder = st.empty() with st.spinner("Thinking..."):
status_placeholder = st.empty() resp = send_chat_message(prompt, top_k, show_explain)
with st.spinner("Thinking... (Decision Engine Active)"): if "error" in resp:
api_response = send_chat_message(prompt, top_k_setting, explain_setting) st.error(resp["error"])
if "error" in api_response:
st.error(api_response["error"])
else: else:
# Extract data # Daten extrahieren
answer = api_response.get("answer", "") answer = resp.get("answer", "")
intent = api_response.get("intent", "FACT") intent = resp.get("intent", "FACT")
source = api_response.get("intent_source", "Unknown") source = resp.get("intent_source", "Unknown")
query_id = api_response.get("query_id") query_id = resp.get("query_id")
hits = api_response.get("sources", []) hits = resp.get("sources", [])
latency = api_response.get("latency_ms", 0)
# 1. Show Intent # Sofort rendern (damit User nicht auf Rerun warten muss)
status_placeholder.markdown(render_intent_badge(intent, source), unsafe_allow_html=True) st.markdown(render_intent_badge(intent, source), unsafe_allow_html=True)
st.markdown(answer)
render_sources(hits, query_id)
# 2. Show Answer # Feedback Slot für die NEUE Nachricht vorbereiten
message_placeholder.markdown(answer) st.caption("Wie gut war diese Antwort?")
st.feedback("stars", key=f"fb_glob_{query_id}", on_change=lambda: submit_feedback(query_id, "generated_answer", st.session_state[f"fb_glob_{query_id}"] + 1))
# 3. Show Sources # In History speichern
render_sources(hits)
# 4. Show Latency & Feedback UI
st.caption(f"⏱️ {latency}ms | ID: `{query_id}`")
# Feedback Buttons
col1, col2, col3, col4 = st.columns([1,1,1,4])
with col1:
if st.button("👍", key=f"up_{query_id}"):
send_feedback(query_id, 5)
st.toast("Feedback gesendet: Positiv!")
with col2:
if st.button("👎", key=f"down_{query_id}"):
send_feedback(query_id, 1)
st.toast("Feedback gesendet: Negativ.")
# Save to history
st.session_state.messages.append({ st.session_state.messages.append({
"role": "assistant", "role": "assistant",
"content": answer, "content": answer,
"intent": intent, "intent": intent,
"intent_source": source, "intent_source": source,
"sources": hits, "sources": hits,
"query_id": query_id, "query_id": query_id
"latency_ms": latency
}) })

View File

@ -6,7 +6,7 @@ Zweck:
WP-06 Update: Intent & Intent-Source in ChatResponse. WP-06 Update: Intent & Intent-Source in ChatResponse.
Version: Version:
0.6.1 (WP-06: Decision Engine Transparency) 0.6.2 (WP-06: Decision Engine Transparency, Erweiterung des Feeback Request)
Stand: Stand:
2025-12-09 2025-12-09
""" """
@ -64,11 +64,14 @@ class QueryRequest(BaseModel):
class FeedbackRequest(BaseModel): class FeedbackRequest(BaseModel):
""" """
User-Feedback zu einem spezifischen Treffer. User-Feedback zu einem spezifischen Treffer oder der Gesamtantwort.
""" """
query_id: str = Field(..., description="ID der ursprünglichen Suche") query_id: str = Field(..., description="ID der ursprünglichen Suche")
node_id: str = Field(..., description="ID des bewerteten Treffers") # node_id ist optional: Wenn leer oder "generated_answer", gilt es für die Antwort.
score: int = Field(..., ge=0, le=1, description="1 (Positiv) oder 0 (Negativ/Irrelevant)") # Wenn eine echte Chunk-ID, gilt es für die Quelle.
node_id: str = Field(..., description="ID des bewerteten Treffers oder 'generated_answer'")
# Update: Range auf 1-5 erweitert für differenziertes Tuning
score: int = Field(..., ge=1, le=5, description="1 (Irrelevant/Falsch) bis 5 (Perfekt)")
comment: Optional[str] = None comment: Optional[str] = None
@ -153,3 +156,5 @@ class ChatResponse(BaseModel):
latency_ms: int latency_ms: int
intent: Optional[str] = Field("FACT", description="WP-06: Erkannter Intent (FACT/DECISION)") intent: Optional[str] = Field("FACT", description="WP-06: Erkannter Intent (FACT/DECISION)")
intent_source: Optional[str] = Field("Unknown", description="WP-06: Quelle der Intent-Erkennung (Keyword vs. LLM)") intent_source: Optional[str] = Field("Unknown", description="WP-06: Quelle der Intent-Erkennung (Keyword vs. LLM)")