56 lines
2.8 KiB
Python
56 lines
2.8 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):
|
|
for idx, msg in enumerate(st.session_state.messages):
|
|
with st.chat_message(msg["role"]):
|
|
if msg["role"] == "assistant":
|
|
intent = msg.get("intent", "UNKNOWN")
|
|
st.markdown(f'<div class="intent-badge">Intent: {intent}</div>', unsafe_allow_html=True)
|
|
|
|
with st.expander("🐞 Payload", expanded=False):
|
|
st.json(msg)
|
|
|
|
if intent == "INTERVIEW":
|
|
render_draft_editor(msg)
|
|
else:
|
|
st.markdown(msg["content"])
|
|
|
|
if "sources" in msg and msg["sources"]:
|
|
for hit in msg["sources"]:
|
|
with st.expander(f"📄 {hit.get('note_id', '?')} ({hit.get('total_score', 0):.2f})"):
|
|
st.markdown(f"_{hit.get('source', {}).get('text', '')[:300]}..._")
|
|
if hit.get('explanation'):
|
|
st.caption(f"Grund: {hit['explanation']['reasons'][0]['message']}")
|
|
|
|
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)
|
|
|
|
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:
|
|
st.markdown(msg["content"])
|
|
|
|
if prompt := st.chat_input("Frage Mindnet..."):
|
|
st.session_state.messages.append({"role": "user", "content": prompt})
|
|
st.rerun()
|
|
|
|
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() |