import streamlit as st import os from langchain_community.document_loaders import WebBaseLoader from langchain_community.embeddings import OllamaEmbeddings from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain_core.messages import AIMessage, HumanMessage from langchain.chains.combine_documents import create_stuff_documents_chain from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder from langchain_community.chat_message_histories import ChatMessageHistory from langchain_community.embeddings import HuggingFaceInferenceAPIEmbeddings from langchain_core.chat_history import BaseChatMessageHistory from langchain.chains import create_history_aware_retriever, create_retrieval_chain from langchain.retrievers import EnsembleRetriever from langchain_community.retrievers import BM25Retriever from langchain_community.vectorstores import FAISS from langchain_groq import ChatGroq from dotenv import load_dotenv from langchain_community.embeddings import OllamaEmbeddings import time import pprint from langchain_core.runnables.history import RunnableWithMessageHistory import json import os from langchain_community.chat_models import ChatOllama load_dotenv() qroq_key = os.environ['GROQ'] hf_key = os.environ['HF_TOKEN'] #Creating LLM qroq_key = os.environ.get('GROQ') hf_key = os.environ.get('HF_TOKEN') llm_groq = ChatGroq(api_key=qroq_key, model='llama3-8b-8192') #llm_ollama = ChatOllama(model='llama3.1:8b-instruct-q2_K', num_gpu=2) hf_embeddings = HuggingFaceInferenceAPIEmbeddings(api_key=hf_key, model_name="sentence-transformers/all-MiniLM-L12-v2") #ollama_embeddings = OllamaEmbeddings(model="nomic-embed-text:latest",num_gpu=1) #Links to add docs from links1 = [ "https://wiki.healthygamer.gg/en/Self-Improvement", "https://wiki.healthygamer.gg/en/Motivation", "https://wiki.healthygamer.gg/en/Depression", "https://wiki.healthygamer.gg/en/Anxiety", "https://wiki.healthygamer.gg/en/Stress", "https://wiki.healthygamer.gg/en/Failure", "https://wiki.healthygamer.gg/en/Mental_Health", "https://wiki.healthygamer.gg/en/Social_Anxiety", "https://wiki.healthygamer.gg/en/Confidence", "https://wiki.healthygamer.gg/en/Loneliness", "https://wiki.healthygamer.gg/en/Insecurity", "https://wiki.healthygamer.gg/en/Intrusive_Thoughts", "https://wiki.healthygamer.gg/en/Emotions", "https://wiki.healthygamer.gg/en/Imposter_Syndrome", "https://wiki.healthygamer.gg/en/Samskaras", "https://wiki.healthygamer.gg/en/Addiction", "https://wiki.healthygamer.gg/en/Video_Game_Addiction", "https://wiki.healthygamer.gg/en/Borderline_Personality_Disorder", "https://wiki.healthygamer.gg/en/Personality", "https://wiki.healthygamer.gg/en/Personality", "https://wiki.healthygamer.gg/en/Relationships", "https://wiki.healthygamer.gg/en/Communication", "https://wiki.healthygamer.gg/en/Psychology", "https://wiki.healthygamer.gg/en/Learning", "https://wiki.healthygamer.gg/en/Productivity", "https://wiki.healthygamer.gg/en/Career", "https://wiki.healthygamer.gg/en/Spirituality", "https://wiki.healthygamer.gg/en/Dharma", "https://wiki.healthygamer.gg/en/Karma", "https://wiki.healthygamer.gg/en/Suffering", "https://wiki.healthygamer.gg/en/Meditation", "https://wiki.healthygamer.gg/en/Meditation_Techniques", "https://wiki.healthygamer.gg/en/Ayurveda", "https://wiki.healthygamer.gg/en/Vata", "https://wiki.healthygamer.gg/en/Pitta", "https://wiki.healthygamer.gg/en/Kapha", "https://neurosciencenews.com/empathy-logic-wisdom-27581/", "https://neurosciencenews.com/placebo-stress-anxiety-27576/", "https://neurosciencenews.com/memory-alcohol-use-27574/", "https://neurosciencenews.com/psychosis-cognitive-network-27571/", "https://neurosciencenews.com/psilocybin-antidepressant-psychopharmacology-27567/", "https://neurosciencenews.com/childhood-inflammation-mental-health-27566/", "https://neurosciencenews.com/neuroticism-self-control-27565/", "https://neurosciencenews.com/digital-swiping-boredom-27560/", "https://neurosciencenews.com/psychology-death-disaster-27552/", "https://neurosciencenews.com/moral-values-seasons-psychology-37540/", "https://neurosciencenews.com/classical-music-depression-sync-brain-27538/", "https://neurosciencenews.com/knowlege-group-welfare-psychology-27535/", "https://neurosciencenews.com/loneliness-nightmares-psychology-27522/", "https://neurosciencenews.com/mental-effort-stress-27515/", "https://neurosciencenews.com/happiness-psychology-satisfaction-27514/", "https://neurosciencenews.com/environment-genetics-schizophrenia-27511/", "https://neurosciencenews.com/exercise-motivation-depression-27495/", "https://neurosciencenews.com/curiosity-memory-neurodevelopment-27584/", "https://neurosciencenews.com/neuroinflammation-long-covid-27586/", "https://neurosciencenews.com/visual-object-memory-neuroscience-27585/", "https://neurosciencenews.com/non-cognitive-intelligence-27582/", "https://neurosciencenews.com/love-brain-neuroscience-27581/", "https://neurosciencenews.com/kyrurenine-pathway-alzheimers-memory-27579/", "https://neurosciencenews.com/bipolar-mental-health-26296/", "https://neurosciencenews.com/asd-genetics-sleep-bipolar-26233/", "https://neurosciencenews.com/bartonella-psychosis-genetics-26267/", "https://neurosciencenews.com/psychosis-brain-systems-25910/", "https://neurosciencenews.com/genetic-left-handed-neurology-25864/", "https://www.psychologytoday.com/us/blog/the-mysteries-of-love/202408/understanding-chronic-loneliness-in-adolescence", "https://www.psychologytoday.com/us/blog/the-happiness-project/201702/7-types-loneliness-and-why-it-matters", "https://www.psychologytoday.com/us/blog/the-path-to-passionate-happiness/202407/why-are-we-so-lonely", "https://www.psychologytoday.com/us/blog/a-unified-theory-of-happiness/201612/5-ways-to-cope-with-ostracism", "https://www.psychologytoday.com/us/blog/the-mysteries-of-love/202408/why-we-crave-unrequited-love-the-allure-of-the-unavailable", "https://www.psychologytoday.com/us/blog/social-instincts/202404/the-concerning-global-rise-in-hikikomori-syndrome-explained", "https://www.psychologytoday.com/us/blog/are-we-done-fighting/202408/the-fine-line-between-confidence-and-closed-mindedness", "https://www.psychologytoday.com/us/blog/the-attraction-doctor/202408/behaviors-that-signal-romantic-or-sexual-interest", "https://www.psychologytoday.com/us/blog/living-with-emotional-intensity/202408/the-rollercoaster-of-love-growing-up-with-an-unstable" "https://www.psychologytoday.com/us/basics/addiction", "https://www.psychologytoday.com/us/basics/anxiety", "https://www.psychologytoday.com/us/basics/adhd", "https://www.psychologytoday.com/us/basics/aspergers-syndrome", "https://www.psychologytoday.com/us/basics/autism", "https://www.psychologytoday.com/us/basics/bipolar-disorder", "https://www.psychologytoday.com/us/basics/chronic-pain", "https://www.psychologytoday.com/us/basics/depression", "https://www.psychologytoday.com/us/basics/eating-disorders", "https://www.psychologytoday.com/us/basics/passive-aggression", "https://www.psychologytoday.com/us/basics/personality", "https://www.psychologytoday.com/us/basics/shyness", "https://www.psychologytoday.com/us/basics/motivation", "https://www.psychologytoday.com/us/basics/happiness", "https://www.psychologytoday.com/us/basics/positive-psychology", "https://www.psychologytoday.com/us/basics/smoking", "https://www.psychologytoday.com/us/basics/low-sexual-desire", "https://www.psychologytoday.com/us/basics/relationships", "https://www.psychologytoday.com/us/basics/sex", "https://www.psychologytoday.com/us/basics/child-development", "https://www.psychologytoday.com/us/basics/parenting", "https://www.psychologytoday.com/us/conditions/tinnitus", "https://www.psychologytoday.com/us/blog/the-mysteries-of-love/202408/are-you-secretly-being-controlled-by-tone-or-nonverbal-cues", "https://www.psychologytoday.com/us/blog/the-attraction-doctor/202406/why-people-manipulate-and-play-games-in-relationships", "https://www.psychologytoday.com/us/blog/the-attraction-doctor/202405/3-flirting-errors-are-you-a-bad-flirt-and-dont-know-it", "https://www.psychologytoday.com/us/blog/the-attraction-doctor/202403/how-do-nice-guys-and-gals-finish-first", "https://www.psychologytoday.com/us/blog/a-funny-bone-to-pick/202406/pebbling-a-new-term-for-an-established-dating-trend", "https://www.psychologytoday.com/us/blog/magnetic-partners/201608/how-couples-deal-with-the-loss-of-physical-attraction", "https://www.psychologytoday.com/us/blog/the-state-of-our-unions/202208/whats-behind-the-rise-of-lonely-single-men", ] links2 =["https://www.psychologytoday.com/us/blog/the-mysteries-of-love/202408/understanding-chronic-loneliness-in-adolescence","" "https://www.psychologytoday.com/us/blog/the-happiness-project/201702/7-types-loneliness-and-why-it-matters", "https://www.psychologytoday.com/us/blog/the-path-to-passionate-happiness/202407/why-are-we-so-lonely", "https://www.psychologytoday.com/us/blog/a-unified-theory-of-happiness/201612/5-ways-to-cope-with-ostracism", "https://www.psychologytoday.com/us/blog/the-mysteries-of-love/202408/why-we-crave-unrequited-love-the-allure-of-the-unavailable", "https://www.psychologytoday.com/us/blog/social-instincts/202404/the-concerning-global-rise-in-hikikomori-syndrome-explained", "https://www.psychologytoday.com/us/blog/are-we-done-fighting/202408/the-fine-line-between-confidence-and-closed-mindedness", "https://www.psychologytoday.com/us/blog/the-attraction-doctor/202408/behaviors-that-signal-romantic-or-sexual-interest", "https://www.psychologytoday.com/us/blog/living-with-emotional-intensity/202408/the-rollercoaster-of-love-growing-up-with-an-unstable" ] #initialize splitter splitter = RecursiveCharacterTextSplitter(chunk_size=1000,chunk_overlap=200) file_path = 'index2' #create docs using webbase loader def links_to_docs(links: list): loader = WebBaseLoader(web_path=links1) all_docs = loader.load() split_docs = splitter.split_documents(all_docs) #need to savve split docs return split_docs def vector_store(chunks, embedding_model): if os.path.exists('./index_local'): store = FAISS.load_local("index_local",embeddings=embedding_model, allow_dangerous_deserialization=True) else: store = FAISS.from_documents(chunks, embedding_model) store.save_local('index_local') return store ##vector store not saving anything as an object if 'msg_store' not in st.session_state: st.session_state.chunks = links_to_docs(links=links1) st.session_state.embeddings = hf_embeddings st.session_state.vector_store = vector_store(chunks=st.session_state.chunks, embedding_model=st.session_state.embeddings) st.session_state.normal_ret = st.session_state.vector_store.as_retriever(search_kwargs= {"k":10}) st.session_state.keyword_ret = BM25Retriever.from_documents(st.session_state.chunks) st.session_state.ensemble_ret = EnsembleRetriever(retrievers=[st.session_state.normal_ret,st.session_state.keyword_ret],weights=[0.75, 0.25]) st.session_state.secret_id = "abc123" #manages msg history st.session_state.msg_store = {} print('--'*100) if 'msg_list' not in st.session_state: st.session_state.msg_list = [] ###creating history aware retrieval contextualize_q_system_prompt = ( "Given a chat history and the latest user question " "which might reference context in the chat history, " "sum up the chat history in 300 words" "without the chat history. Do NOT answer the question," "just reformulate it if needed and otherwise return it as is." ) contextualize_q_prompt = ChatPromptTemplate.from_messages( [ ("system", contextualize_q_system_prompt), MessagesPlaceholder("chat_history"), ("human", "{input}"), ] ) history_aware_retriever = create_history_aware_retriever( llm_groq, st.session_state.ensemble_ret, contextualize_q_prompt ) system_prompt = ( """ Answer the questions based on the provided context only. Please provide the most accurate response based on the question, avoid words "HealthyGamer Wiki","gamer", or "Wiki","psychologytoday", or any "article", or any reference to Healthy Gamer, 'also never explicitly say "HealthyGamer Wiki","gamer", or "Wiki", also keep the chat history in context "{context}" """ ) qa_prompt = ChatPromptTemplate.from_messages( [ ("system", system_prompt), MessagesPlaceholder("chat_history"), ("human", "{input}"), ] ) question_answer_chain = create_stuff_documents_chain(llm_groq, qa_prompt) rag_chain = create_retrieval_chain(history_aware_retriever, question_answer_chain) def get_session_history(session_id: str) -> BaseChatMessageHistory: if session_id not in st.session_state.msg_store : st.session_state.msg_store[session_id] = ChatMessageHistory() return st.session_state.msg_store[session_id] conversational_rag_chain = RunnableWithMessageHistory( rag_chain, get_session_history, input_messages_key="input", history_messages_key="chat_history", output_messages_key="answer", ) def get_response(userquery:str): response_generator = conversational_rag_chain.stream( {"input": userquery}, config={"configurable": {"session_id": "abc123"}} ) #len_query = len(user_query) #c = -1 # Since it's a generator, iterate over the response for rag_response in response_generator : print(rag_response) for k, v in rag_response.items(): #if k == 'context': #continue #elif k == 'chat_history': #continue if k == 'answer': yield v ##creating streamlit frontend now st.title("Calm Counsel") history = get_session_history(st.session_state.secret_id) user_query = st.chat_input("Type your message here...") sequence_number = 0 if user_query is not None and user_query != "": for msg in st.session_state.msg_list: with st.chat_message(msg['role']): st.markdown(msg['content']) user_query_backup = user_query #history.add_message(HumanMessage(content=user_query)) print('x'*100) #print(st.session_state.msg_list) with st.chat_message("human"): st.markdown(user_query) #adding user query to st history st.session_state.msg_list.append({'role':'human','content':user_query}) #display ai response with st.chat_message("ai"): with st.spinner('Thinking...'): response = st.write_stream(get_response(user_query)) print(st.session_state.msg_list) st.session_state.msg_list.append({'role':'ai','content':response}) print('Ii' * 100) print('Mm' * 100)