|
import os |
|
import streamlit as st |
|
import time |
|
from langchain.chat_models import ChatOpenAI |
|
from langchain.chains import ConversationalRetrievalChain |
|
from langchain.prompts import PromptTemplate |
|
from langchain.memory import ConversationSummaryBufferMemory |
|
from langchain.vectorstores import FAISS |
|
from langchain.embeddings import OpenAIEmbeddings |
|
|
|
|
|
os.environ["OPENAI_API_KEY"] = os.environ.get("OPENAI_API_KEY") |
|
|
|
|
|
embeddings = OpenAIEmbeddings() |
|
vectorstore = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True) |
|
|
|
|
|
retriever = vectorstore.as_retriever(search_kwargs={"k": 5}) |
|
|
|
|
|
prompt_template = """ |
|
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. |
|
Summarized Chat History: |
|
{chat_history} |
|
|
|
User's Current Query: {query} |
|
Based on the user's current query and chat history summary, here are some relevant courses from our database: |
|
{context} |
|
|
|
Please provide a personalized course recommendation. Your response should include: |
|
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. |
|
2. A summary of each recommended course, highlighting: |
|
- The specific skills and knowledge the user will gain (from "What You Will Learn") |
|
- Key topics covered in the syllabus |
|
- Course level and language |
|
- The institution offering the course |
|
3. Mention the course ratings if available. |
|
4. Any additional advice or suggestions for the user's learning journey, based on the syllabus progression and their conversation history. |
|
5. Provide the course URLs for easy access. |
|
|
|
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. |
|
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. |
|
Recommendation: |
|
""" |
|
|
|
PROMPT = PromptTemplate( |
|
template=prompt_template, |
|
input_variables=["chat_history", "query", "context"] |
|
) |
|
|
|
|
|
llm = ChatOpenAI(temperature=0.5, model_name="gpt-4-turbo") |
|
|
|
|
|
memory = ConversationSummaryBufferMemory(llm=llm, max_token_limit=1000, memory_key="chat_history", return_messages=True) |
|
|
|
|
|
qa_chain = ConversationalRetrievalChain.from_llm( |
|
llm=llm, |
|
retriever=retriever, |
|
memory=memory, |
|
combine_docs_chain_kwargs={"prompt": PROMPT} |
|
) |
|
|
|
|
|
st.set_page_config(page_title="AI Course Recommendation Chatbot", page_icon=":book:") |
|
st.title("AI Course Recommendation Chatbot") |
|
|
|
|
|
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 are you looking to learn?"): |
|
|
|
st.session_state.messages.append({"role": "user", "content": prompt}) |
|
|
|
with st.chat_message("user"): |
|
st.markdown(prompt) |
|
|
|
|
|
with st.chat_message("assistant"): |
|
response = qa_chain({"query": prompt}) |
|
response_text = response["response"] |
|
|
|
|
|
for accumulated_response in response_text: |
|
st.markdown(accumulated_response, unsafe_allow_html=True) |
|
|
|
|
|
st.session_state.messages.append({"role": "assistant", "content": response_text}) |
|
|