Update app.py
Browse files
app.py
CHANGED
@@ -1,18 +1,13 @@
|
|
1 |
import os
|
2 |
import streamlit as st
|
3 |
-
import random
|
4 |
import time
|
|
|
5 |
from langchain.chat_models import ChatOpenAI
|
6 |
from langchain.chains import ConversationalRetrievalChain
|
7 |
from langchain.prompts import PromptTemplate
|
8 |
from langchain.memory import ConversationSummaryBufferMemory
|
9 |
from langchain.vectorstores import FAISS
|
10 |
from langchain.embeddings import OpenAIEmbeddings
|
11 |
-
import re
|
12 |
-
|
13 |
-
# Function to split text into sentences
|
14 |
-
def split_into_sentences(text):
|
15 |
-
return re.split(r'(?<=[.!?]) +', text)
|
16 |
|
17 |
# Set up the OpenAI API key
|
18 |
os.environ["OPENAI_API_KEY"] = os.environ.get("OPENAI_API_KEY")
|
@@ -115,21 +110,15 @@ if prompt := st.chat_input("What are you looking to learn?"):
|
|
115 |
with st.chat_message("user"):
|
116 |
st.markdown(prompt)
|
117 |
|
118 |
-
# Assistant response
|
119 |
with st.chat_message("assistant"):
|
120 |
response = qa_chain({"question": prompt})
|
121 |
response_text = response["answer"]
|
122 |
|
123 |
-
#
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
time.sleep(1) # Delay between sentences (adjust as needed)
|
128 |
|
129 |
# Add assistant response to chat history
|
130 |
-
st.session_state.messages.append({"role": "assistant", "content": response_text})
|
131 |
-
|
132 |
-
# Optional: Add a button to clear the chat history
|
133 |
-
if st.button("Clear Chat History"):
|
134 |
-
st.session_state.messages.clear()
|
135 |
-
st.experimental_rerun()
|
|
|
1 |
import os
|
2 |
import streamlit as st
|
|
|
3 |
import time
|
4 |
+
import re
|
5 |
from langchain.chat_models import ChatOpenAI
|
6 |
from langchain.chains import ConversationalRetrievalChain
|
7 |
from langchain.prompts import PromptTemplate
|
8 |
from langchain.memory import ConversationSummaryBufferMemory
|
9 |
from langchain.vectorstores import FAISS
|
10 |
from langchain.embeddings import OpenAIEmbeddings
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
# Set up the OpenAI API key
|
13 |
os.environ["OPENAI_API_KEY"] = os.environ.get("OPENAI_API_KEY")
|
|
|
110 |
with st.chat_message("user"):
|
111 |
st.markdown(prompt)
|
112 |
|
113 |
+
# Assistant response generation with a streaming effect
|
114 |
with st.chat_message("assistant"):
|
115 |
response = qa_chain({"question": prompt})
|
116 |
response_text = response["answer"]
|
117 |
|
118 |
+
# Stream the response character by character
|
119 |
+
for char in response_text:
|
120 |
+
st.markdown(char, unsafe_allow_html=True)
|
121 |
+
time.sleep(0.01) # Adjust this delay to control typing speed
|
|
|
122 |
|
123 |
# Add assistant response to chat history
|
124 |
+
st.session_state.messages.append({"role": "assistant", "content": response_text})
|
|
|
|
|
|
|
|
|
|