78 lines
3.6 KiB
Python
78 lines
3.6 KiB
Python
import streamlit as st
|
|
from ui_api import send_chat_message, submit_feedback
|
|
from ui_editor import render_draft_editor
|
|
|
|
def render_chat_interface(top_k, explain):
|
|
"""
|
|
Rendert das Chat-Interface.
|
|
Zeigt Nachrichten an und behandelt User-Input.
|
|
"""
|
|
# 1. Verlauf anzeigen
|
|
for idx, msg in enumerate(st.session_state.messages):
|
|
with st.chat_message(msg["role"]):
|
|
if msg["role"] == "assistant":
|
|
# Intent Badge
|
|
intent = msg.get("intent", "UNKNOWN")
|
|
st.markdown(f'<div class="intent-badge">Intent: {intent}</div>', unsafe_allow_html=True)
|
|
|
|
# Debugging (optional, gut für Entwicklung)
|
|
with st.expander("🐞 Payload", expanded=False):
|
|
st.json(msg)
|
|
|
|
# Unterscheidung: Normaler Text oder Editor-Modus (Interview)
|
|
if intent == "INTERVIEW":
|
|
render_draft_editor(msg)
|
|
else:
|
|
st.markdown(msg["content"])
|
|
|
|
# Quellen anzeigen
|
|
if "sources" in msg and msg["sources"]:
|
|
for hit in msg["sources"]:
|
|
score = hit.get('total_score', 0)
|
|
# Wenn score None ist, 0.0 annehmen
|
|
if score is None: score = 0.0
|
|
|
|
with st.expander(f"📄 {hit.get('note_id', '?')} ({score:.2f})"):
|
|
st.markdown(f"_{hit.get('source', {}).get('text', '')[:300]}..._")
|
|
|
|
# Explanation Layer
|
|
if hit.get('explanation'):
|
|
st.caption(f"Grund: {hit['explanation']['reasons'][0]['message']}")
|
|
|
|
# Feedback Buttons pro Source
|
|
def _cb(qid=msg.get("query_id"), nid=hit.get('node_id')):
|
|
val = st.session_state.get(f"fb_src_{qid}_{nid}")
|
|
if val is not None: submit_feedback(qid, nid, val+1)
|
|
|
|
st.feedback("faces", key=f"fb_src_{msg.get('query_id')}_{hit.get('node_id')}", on_change=_cb)
|
|
|
|
# Globales Feedback für die Antwort
|
|
if "query_id" in msg:
|
|
qid = msg["query_id"]
|
|
st.feedback("stars", key=f"fb_glob_{qid}", on_change=lambda: submit_feedback(qid, "generated_answer", st.session_state[f"fb_glob_{qid}"]+1))
|
|
else:
|
|
# User Nachricht
|
|
st.markdown(msg["content"])
|
|
|
|
# 2. Input Feld
|
|
if prompt := st.chat_input("Frage Mindnet..."):
|
|
st.session_state.messages.append({"role": "user", "content": prompt})
|
|
st.rerun()
|
|
|
|
# 3. Antwort generieren (wenn letzte Nachricht vom User ist)
|
|
if len(st.session_state.messages) > 0 and st.session_state.messages[-1]["role"] == "user":
|
|
with st.chat_message("assistant"):
|
|
with st.spinner("Thinking..."):
|
|
resp = send_chat_message(st.session_state.messages[-1]["content"], top_k, explain)
|
|
|
|
if "error" in resp:
|
|
st.error(resp["error"])
|
|
else:
|
|
st.session_state.messages.append({
|
|
"role": "assistant",
|
|
"content": resp.get("answer"),
|
|
"intent": resp.get("intent", "FACT"),
|
|
"sources": resp.get("sources", []),
|
|
"query_id": resp.get("query_id")
|
|
})
|
|
st.rerun() |