Update app.py
Browse files
app.py
CHANGED
@@ -8,6 +8,11 @@ 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")
|
@@ -22,15 +27,11 @@ retriever = vectorstore.as_retriever(search_kwargs={"k": 5})
|
|
22 |
# Define a prompt template for course recommendations
|
23 |
prompt_template = """
|
24 |
You are an AI course recommendation system. Your task is to recommend courses based on the user's description of their interests and goals, with a strong emphasis on matching the learning outcomes and syllabus content. Consider the summarized chat history to provide more relevant and personalized recommendations.
|
25 |
-
|
26 |
Summarized Chat History:
|
27 |
{chat_history}
|
28 |
-
|
29 |
User's Current Query: {question}
|
30 |
-
|
31 |
Based on the user's current query and chat history summary, here are some relevant courses from our database:
|
32 |
{context}
|
33 |
-
|
34 |
Please provide a personalized course recommendation. Your response should include:
|
35 |
1. A detailed explanation of how the recommended courses match the user's interests and previous queries, focusing primarily on the "What You Will Learn" section and the syllabus content.
|
36 |
2. A summary of each recommended course, highlighting:
|
@@ -41,11 +42,8 @@ Please provide a personalized course recommendation. Your response should includ
|
|
41 |
3. Mention the course ratings if available.
|
42 |
4. Any additional advice or suggestions for the user's learning journey, based on the syllabus progression and their conversation history.
|
43 |
5. Provide the course URLs for easy access.
|
44 |
-
|
45 |
Prioritize courses that have the most relevant learning outcomes and syllabus content matching the user's description and previous interactions. If multiple courses are similarly relevant, you may suggest a learning path combining complementary courses.
|
46 |
-
|
47 |
Remember to be encouraging and supportive in your recommendation, and relate your suggestions to any preferences or constraints the user has mentioned in previous messages.
|
48 |
-
|
49 |
Recommendation:
|
50 |
"""
|
51 |
|
@@ -117,15 +115,16 @@ if prompt := st.chat_input("What are you looking to learn?"):
|
|
117 |
with st.chat_message("user"):
|
118 |
st.markdown(prompt)
|
119 |
|
120 |
-
# Assistant response generator with streaming
|
121 |
with st.chat_message("assistant"):
|
122 |
response = qa_chain({"question": prompt})
|
123 |
response_text = response["answer"]
|
124 |
-
|
125 |
-
#
|
126 |
-
|
127 |
-
|
128 |
-
|
|
|
129 |
|
130 |
# Add assistant response to chat history
|
131 |
st.session_state.messages.append({"role": "assistant", "content": response_text})
|
|
|
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")
|
|
|
27 |
# Define a prompt template for course recommendations
|
28 |
prompt_template = """
|
29 |
You are an AI course recommendation system. Your task is to recommend courses based on the user's description of their interests and goals, with a strong emphasis on matching the learning outcomes and syllabus content. Consider the summarized chat history to provide more relevant and personalized recommendations.
|
|
|
30 |
Summarized Chat History:
|
31 |
{chat_history}
|
|
|
32 |
User's Current Query: {question}
|
|
|
33 |
Based on the user's current query and chat history summary, here are some relevant courses from our database:
|
34 |
{context}
|
|
|
35 |
Please provide a personalized course recommendation. Your response should include:
|
36 |
1. A detailed explanation of how the recommended courses match the user's interests and previous queries, focusing primarily on the "What You Will Learn" section and the syllabus content.
|
37 |
2. A summary of each recommended course, highlighting:
|
|
|
42 |
3. Mention the course ratings if available.
|
43 |
4. Any additional advice or suggestions for the user's learning journey, based on the syllabus progression and their conversation history.
|
44 |
5. Provide the course URLs for easy access.
|
|
|
45 |
Prioritize courses that have the most relevant learning outcomes and syllabus content matching the user's description and previous interactions. If multiple courses are similarly relevant, you may suggest a learning path combining complementary courses.
|
|
|
46 |
Remember to be encouraging and supportive in your recommendation, and relate your suggestions to any preferences or constraints the user has mentioned in previous messages.
|
|
|
47 |
Recommendation:
|
48 |
"""
|
49 |
|
|
|
115 |
with st.chat_message("user"):
|
116 |
st.markdown(prompt)
|
117 |
|
118 |
+
# Assistant response generator with sentence streaming
|
119 |
with st.chat_message("assistant"):
|
120 |
response = qa_chain({"question": prompt})
|
121 |
response_text = response["answer"]
|
122 |
+
|
123 |
+
# Split response into sentences and display one by one
|
124 |
+
sentences = split_into_sentences(response_text)
|
125 |
+
for sentence in sentences:
|
126 |
+
st.markdown(sentence)
|
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})
|