Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import pickle
|
3 |
+
import streamlit as st
|
4 |
+
from langchain.chat_models import ChatOpenAI
|
5 |
+
from langchain.chains import ConversationalRetrievalChain
|
6 |
+
from langchain.prompts import PromptTemplate
|
7 |
+
from langchain.memory import ConversationSummaryBufferMemory
|
8 |
+
from langchain.vectorstores import FAISS
|
9 |
+
from langchain.embeddings import OpenAIEmbeddings
|
10 |
+
|
11 |
+
# Set up the OpenAI API key
|
12 |
+
os.environ["OPENAI_API_KEY"] = os.environ.get("OPENAI_API_KEY")
|
13 |
+
|
14 |
+
# Load the FAISS index
|
15 |
+
embeddings = OpenAIEmbeddings()
|
16 |
+
vectorstore = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
|
17 |
+
|
18 |
+
# Create a retriever from the loaded vector store
|
19 |
+
retriever = vectorstore.as_retriever(search_kwargs={"k": 5})
|
20 |
+
|
21 |
+
# Define a prompt template for course recommendations
|
22 |
+
prompt_template = """
|
23 |
+
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.
|
24 |
+
|
25 |
+
Summarized Chat History:
|
26 |
+
{chat_history}
|
27 |
+
|
28 |
+
User's Current Query: {question}
|
29 |
+
|
30 |
+
Based on the user's current query and chat history summary, here are some relevant courses from our database:
|
31 |
+
{context}
|
32 |
+
|
33 |
+
Please provide a personalized course recommendation. Your response should include:
|
34 |
+
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.
|
35 |
+
2. A summary of each recommended course, highlighting:
|
36 |
+
- The specific skills and knowledge the user will gain (from "What You Will Learn")
|
37 |
+
- Key topics covered in the syllabus
|
38 |
+
- Course level and language
|
39 |
+
- The institution offering the course
|
40 |
+
3. Mention the course ratings if available.
|
41 |
+
4. Any additional advice or suggestions for the user's learning journey, based on the syllabus progression and their conversation history.
|
42 |
+
5. Provide the course URLs for easy access.
|
43 |
+
|
44 |
+
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.
|
45 |
+
|
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 |
+
|
48 |
+
Recommendation:
|
49 |
+
"""
|
50 |
+
|
51 |
+
PROMPT = PromptTemplate(
|
52 |
+
template=prompt_template,
|
53 |
+
input_variables=["chat_history", "question", "context"]
|
54 |
+
)
|
55 |
+
|
56 |
+
# Initialize the language model
|
57 |
+
llm = ChatOpenAI(temperature=0.5, model_name="gpt-4-turbo")
|
58 |
+
|
59 |
+
# Set up conversation memory with summarization
|
60 |
+
memory = ConversationSummaryBufferMemory(llm=llm, max_token_limit=1000, memory_key="chat_history", return_messages=True)
|
61 |
+
|
62 |
+
# Create the conversational retrieval chain
|
63 |
+
qa_chain = ConversationalRetrievalChain.from_llm(
|
64 |
+
llm=llm,
|
65 |
+
retriever=retriever,
|
66 |
+
memory=memory,
|
67 |
+
combine_docs_chain_kwargs={"prompt": PROMPT}
|
68 |
+
)
|
69 |
+
|
70 |
+
# Streamlit app
|
71 |
+
st.title("AI Course Recommendation Chatbot")
|
72 |
+
|
73 |
+
if 'chat_history' not in st.session_state:
|
74 |
+
st.session_state.chat_history = []
|
75 |
+
|
76 |
+
user_query = st.text_input("What are you looking to learn?", "")
|
77 |
+
|
78 |
+
if st.button("Get Recommendation"):
|
79 |
+
if user_query:
|
80 |
+
response = qa_chain({"question": user_query})
|
81 |
+
recommendation = response["answer"]
|
82 |
+
|
83 |
+
# Update chat history
|
84 |
+
st.session_state.chat_history.append({"user": user_query, "bot": recommendation})
|
85 |
+
|
86 |
+
# Display chat history
|
87 |
+
for chat in st.session_state.chat_history:
|
88 |
+
st.write(f"**You:** {chat['user']}")
|
89 |
+
st.write(f"**HONEY BEE:** {chat['bot']}")
|
90 |
+
|
91 |
+
# Optional: Add a button to clear the chat history
|
92 |
+
if st.button("Clear Chat History"):
|
93 |
+
st.session_state.chat_history.clear()
|
94 |
+
st.experimental_rerun()
|