# https://docs.streamlit.io/knowledge-base/tutorials/build-conversational-apps#build-a-chatgpt-like-app
import streamlit as st
from streamlit_chat import message
from langchain.document_loaders import CSVLoader
from langchain_openai import OpenAIEmbeddings
from langchain.chains import RetrievalQA
from langchain.chains import ConversationalRetrievalChain
from langchain_openai import ChatOpenAI
from langchain_community.vectorstores import Chroma
st.set_page_config(page_title="KaggleX AI Course Coordinator", page_icon=":robot_face:")
#####################################################
##################### Sidebar #######################
st.sidebar.title("Introduction")
st.sidebar.markdown("""
KaggleX AI Course Coordinator is an advanced conversational AI interface, expertly crafted to solve the data scientist learners' problems.
- What is the Learning Path Index Project?: To consolidate a data base of a collection of byte-sized courses/material for Data Science and Machine Learning so that it is
easy to search, filter
- Why Do We Need It?: Addresses problems like long course durations, difficulty in finding specific topics, and the absence of a centralized index.
KaggleX AI Course Coordinator is developed by Lorentz Yeung
""", unsafe_allow_html=True)
#####################################################
##################### UI ############################
# Setting page title and header
st.markdown("KaggleX AI Course Coordinator
", unsafe_allow_html=True)
# st.markdown("A cutting-edge language model
", unsafe_allow_html=True)
st.markdown("By Lorentz Yeung
", unsafe_allow_html=True)
# st.session_state['API_Key']= st.text_input("First, to get it work, put your OpenAI API Key here please, the system will enter for you automatically.",type="password")
#if 'API_Key' not in st.session_state:
# st.session_state['API_Key'] =''
#st.session_state['API_Key']= st.text_input("First, to get it work, put your OpenAI API Key here please, the system will enter for you automatically.",type="password")
# uploaded_file = st.sidebar.file_uploader("upload", type="csv")
persist_directory = "chroma/db"
if persist_directory :
embeddings = OpenAIEmbeddings()
KaggleX_courses_db = Chroma(persist_directory = persist_directory, embedding_function=embeddings)
retriever = KaggleX_courses_db.as_retriever() # search_kwargs={"k": 4}
chain = ConversationalRetrievalChain.from_llm(llm = ChatOpenAI(temperature=0.0,model_name='gpt-3.5-turbo',
),
retriever = retriever)
def conversational_chat(query):
result = chain({"question": query, "chat_history": st.session_state['history']})
st.session_state['history'].append((query, result["answer"]))
return result["answer"]
if 'history' not in st.session_state:
st.session_state['history'] = []
if 'ai_history' not in st.session_state:
st.session_state['ai_history'] = ["Hi!"]
if 'user_history' not in st.session_state:
st.session_state['user_history'] = ["Hi, I would like to know more about the courses in KaggleX!"]
#container for the chat history
response_container = st.container()
#container for the user's text input
container = st.container()
with container:
with st.form(key='my_form', clear_on_submit=True):
user_input = st.text_input("Your question:", placeholder="Learn more about the courses in KaggleX:", key='input')
submit_button = st.form_submit_button(label='Ask')
if submit_button and user_input:
output = conversational_chat(user_input) # if the button is clicked, then submit he query to the Chain, and take the history from session_state.
st.session_state['user_history'].append(user_input) # store the user input to user history
st.session_state['ai_history'].append(output) # store the AI prediction to ai history
# the chat interface.
if st.session_state['ai_history']:
with response_container:
for i in range(len(st.session_state['ai_history'])):
# https://docs.streamlit.io/library/api-reference/chat/st.chat_message
message(st.session_state["user_history"][i], is_user=True, key=str(i) + '_user', png="user")
# message(st.session_state["ai_history"][i], key=str(i), avatar_style="KaggleX.jpg")
message(st.session_state["ai_history"][i], key=str(i), avatar='KaggleX.png')