|
import streamlit as st |
|
from langserve.client import RemoteRunnable |
|
|
|
def get_response(user_input, url, username): |
|
response_placeholder = st.empty() |
|
full_response = "" |
|
chain = RemoteRunnable(url) |
|
stream = chain.stream(input={'question': user_input, 'username': username}) |
|
for chunk in stream: |
|
full_response += chunk |
|
response_placeholder.markdown(full_response) |
|
|
|
return full_response |
|
|
|
def chat_interface(chat_title, page_hash ,url): |
|
st.title(chat_title) |
|
|
|
|
|
username = st.text_input("Enter your username:", key="username_input", value="Guest") |
|
|
|
|
|
if "chat_histories" not in st.session_state: |
|
st.session_state.chat_histories = {} |
|
|
|
if page_hash not in st.session_state.chat_histories: |
|
st.session_state.chat_histories[page_hash] = [] |
|
|
|
|
|
for message in st.session_state.chat_histories[page_hash]: |
|
with st.chat_message(message["role"]): |
|
st.markdown(message["content"]) |
|
|
|
|
|
if prompt := st.chat_input("What is your message?"): |
|
|
|
st.chat_message("user").markdown(prompt) |
|
|
|
st.session_state.chat_histories[page_hash].append({"role": "user", "content": prompt}) |
|
|
|
|
|
with st.chat_message("assistant"): |
|
full_response = get_response(prompt, url, username) |
|
|
|
|
|
st.session_state.chat_histories[page_hash].append({"role": "assistant", "content": full_response}) |
|
|
|
if __name__ == "__main__": |
|
chat_interface() |