Frontend1 / pages /rag_page.py
wang16888's picture
Update pages/rag_page.py
e5d9eeb verified
from streamlit.runtime.scriptrunner import get_script_run_ctx
#from pages.page_base import chat_interface
import streamlit as st
from langserve import RemoteRunnable
#chat_title = "RAG Chat App"
st.title('RAG Chat App')
username = st.text_input(
"Enter your username:",
key="username_input",
value="Guest"
)
url = "https://wang16888-backend1.hf.space/rag"
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
if "messages" not in st.session_state:
st.session_state.messages = []
for message in st.session_state.messages:
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.messages.append({
"role": "user", "content": prompt
})
# Get streaming response
with st.chat_message("assistant"):
full_response = get_response(prompt, url, username)
# Add assistant response to chat history
st.session_state.messages.append({
"role": "assistant",
"content": full_response
})
#page_hash = get_script_run_ctx().page_script_hash
#chat_interface(username, page_hash, url)