AI-Manith commited on
Commit
63453da
·
verified ·
1 Parent(s): 50c21be

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -0
app.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Import necessary libraries
2
+ import streamlit as st
3
+ import os
4
+ from dotenv import load_dotenv
5
+ from langchain_groq import ChatGroq
6
+ from langchain_chroma import Chroma
7
+ from langchain_community.document_loaders import WebBaseLoader, MongodbLoader
8
+ from langchain_core.prompts import ChatPromptTemplate
9
+ from langchain_text_splitters import RecursiveCharacterTextSplitter
10
+ from langchain.chains import create_retrieval_chain, create_history_aware_retriever
11
+ from langchain.chains.combine_documents import create_stuff_documents_chain
12
+ from langchain_core.messages import AIMessage, HumanMessage
13
+ from langchain_core.prompts import MessagesPlaceholder
14
+
15
+ # Load environment variables
16
+ load_dotenv()
17
+ groq_api_key = os.getenv('GROQ_API_KEY')
18
+ hf_token = os.getenv('HF_TOKEN')
19
+
20
+ # Initialize the ChatGroq model
21
+ llm = ChatGroq(groq_api_key=groq_api_key, model_name="llama3-8b-8192")
22
+
23
+ # Initialize embeddings
24
+ from langchain_huggingface.embeddings import HuggingFaceEmbeddings
25
+ embeddings = HuggingFaceEmbeddings(model_name='all-MiniLM-L6-v2')
26
+
27
+ # MongoDB data loading setup
28
+ loader = MongodbLoader(
29
+ connection_string="mongodb+srv://deshcode0:[email protected]/?retryWrites=true&w=majority&appName=deshcode0",
30
+ db_name="sample_mflix",
31
+ collection_name="movies",
32
+ field_names = ["_id", "plot", "genres", "runtime", "cast", "poster", "title", "fullplot", "languages", "released", "directors", "rated", "awards", "lastupdated", "year", "imdb", "countries", "type", "tomatoes", "num_mflix_comments"],
33
+ )
34
+ docs = loader.load()
35
+
36
+ # Split documents and initialize Chroma vector store
37
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
38
+ splits = text_splitter.split_documents(docs)
39
+ vectorstore = Chroma.from_documents(documents=splits, embedding=embeddings)
40
+ retriever = vectorstore.as_retriever()
41
+
42
+ # Define prompt templates
43
+ system_prompt = (
44
+ "You are an assistant for question-answering tasks. "
45
+ "Use the following pieces of retrieved context to answer "
46
+ "the question. If you don't know the answer, say that you "
47
+ "don't know. Use three sentences maximum and keep the "
48
+ "answer concise.\n\n{context}"
49
+ )
50
+
51
+ qa_prompt = ChatPromptTemplate.from_messages(
52
+ [
53
+ ("system", system_prompt),
54
+ MessagesPlaceholder("chat_history"),
55
+ ("human", "{input}"),
56
+ ]
57
+ )
58
+
59
+ # Initialize the retrieval chain
60
+ question_answer_chain = create_stuff_documents_chain(llm, qa_prompt)
61
+ rag_chain = create_retrieval_chain(retriever, question_answer_chain)
62
+
63
+ # Streamlit App
64
+ st.title("LLM-Powered Question Answering with Memory")
65
+
66
+ # Initialize session state for chat history
67
+ if "chat_history" not in st.session_state:
68
+ st.session_state.chat_history = []
69
+
70
+ # Sidebar for question input
71
+ st.sidebar.title("Ask a Question")
72
+ question = st.sidebar.text_input("Enter your question:")
73
+
74
+ # Retrieve and display the answer
75
+ if question:
76
+ # Add question to chat history
77
+ st.session_state.chat_history.append(HumanMessage(content=question))
78
+
79
+ # Retrieve answer with context from chat history
80
+ response = rag_chain.invoke({"input": question, "chat_history": st.session_state.chat_history})
81
+
82
+ # Display the answer
83
+ st.write("**Answer:**")
84
+ st.write(response['answer'])
85
+
86
+ # Add answer to chat history
87
+ st.session_state.chat_history.append(AIMessage(content=response['answer']))
88
+
89
+ # Display chat history in the main app
90
+ st.write("## Chat History")
91
+ for message in st.session_state.chat_history:
92
+ if isinstance(message, HumanMessage):
93
+ st.write(f"**You:** {message.content}")
94
+ elif isinstance(message, AIMessage):
95
+ st.write(f"**Bot:** {message.content}")