-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamlit_chat.py
More file actions
59 lines (43 loc) · 1.65 KB
/
Copy pathstreamlit_chat.py
File metadata and controls
59 lines (43 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Created by 'sandeep.mishra@digital.ai' at 9:21 pm 12/05/25 using PyCharm
import streamlit as st
import requests
import uuid
import os
from dotenv import load_dotenv
load_dotenv()
# API endpoint
API_URL = os.getenv("API_URL", "http://localhost:8003/query")
st.set_page_config(page_title="Chat with Agent", layout="centered")
# Session ID management
if "session_id" not in st.session_state:
st.session_state.session_id = str(uuid.uuid4())
st.title("MCP Client: How can I help you?")
# Display the conversation history at the top
if "conversation" not in st.session_state:
st.session_state.conversation = []
for msg in st.session_state.conversation:
role, content = msg
with st.chat_message(role):
st.markdown(content)
# Input: chat-style, auto-submits on Enter
user_input = st.chat_input("Type your message here...")
if user_input:
# Display user message immediately
st.session_state.conversation.append(("user", f"**You**: {user_input}"))
res = requests.post("http://localhost:8003/query", json={
"message": user_input,
"session_id": st.session_state.session_id
})
data = res.json()
st.session_state.session_id = data.get("session_id")
# Append assistant response
response = data.get("response")
if isinstance(response, dict) and "output" in response:
content = response["output"]
else:
content = str(response)
st.session_state.conversation.append(("assistant", f"**Assistant**: {content}"))
# Rerun to update UI
st.rerun()
# Scroll to the latest message
st.markdown('<script>window.scrollTo(0, document.body.scrollHeight);</script>', unsafe_allow_html=True)