| from streamlit.runtime.scriptrunner import get_script_run_ctx |
| |
| import streamlit as st |
| from langserve import RemoteRunnable |
| |
| 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 |
| }) |
| |
| with st.chat_message("assistant"): |
| full_response = get_response(prompt, url, username) |
| |
| st.session_state.messages.append({ |
| "role": "assistant", |
| "content": full_response |
| }) |
| |
|
|
| |