Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,4 @@
|
|
1 |
import os
|
2 |
-
import json
|
3 |
-
|
4 |
import streamlit as st
|
5 |
from langchain_huggingface import HuggingFaceEmbeddings
|
6 |
from langchain_chroma import Chroma
|
@@ -8,25 +6,33 @@ from langchain_groq import ChatGroq
|
|
8 |
from langchain.memory import ConversationBufferMemory
|
9 |
from langchain.chains import ConversationalRetrievalChain
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
os.environ["GROQ_API_KEY"] = GROQ_API_KEY
|
17 |
-
|
18 |
|
|
|
19 |
def setup_vectorstore():
|
|
|
20 |
persist_directory = f"{working_dir}/vector_db_dir"
|
21 |
-
embedddings = HuggingFaceEmbeddings()
|
22 |
-
vectorstore = Chroma(persist_directory=persist_directory,
|
23 |
-
embedding_function=embeddings)
|
24 |
-
return vectorstore
|
25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
|
|
27 |
def chat_chain(vectorstore):
|
28 |
-
llm = ChatGroq(model="llama-3.1-70b-versatile",
|
29 |
-
temperature=0
|
|
|
|
|
30 |
retriever = vectorstore.as_retriever()
|
31 |
memory = ConversationBufferMemory(
|
32 |
llm=llm,
|
@@ -34,6 +40,7 @@ def chat_chain(vectorstore):
|
|
34 |
memory_key="chat_history",
|
35 |
return_messages=True
|
36 |
)
|
|
|
37 |
chain = ConversationalRetrievalChain.from_llm(
|
38 |
llm=llm,
|
39 |
retriever=retriever,
|
@@ -42,32 +49,33 @@ def chat_chain(vectorstore):
|
|
42 |
verbose=True,
|
43 |
return_source_documents=True
|
44 |
)
|
45 |
-
|
46 |
return chain
|
47 |
|
48 |
-
|
49 |
st.set_page_config(
|
50 |
page_title="Multi Doc Chat",
|
51 |
-
page_icon
|
52 |
layout="centered"
|
53 |
)
|
54 |
|
55 |
st.title("π Multi Documents Chatbot")
|
56 |
|
|
|
57 |
if "chat_history" not in st.session_state:
|
58 |
st.session_state.chat_history = []
|
59 |
|
60 |
if "vectorstore" not in st.session_state:
|
61 |
st.session_state.vectorstore = setup_vectorstore()
|
62 |
|
63 |
-
if "
|
64 |
-
st.session_state.
|
65 |
-
|
66 |
|
|
|
67 |
for message in st.session_state.chat_history:
|
68 |
with st.chat_message(message["role"]):
|
69 |
st.markdown(message["content"])
|
70 |
|
|
|
71 |
user_input = st.chat_input("Ask AI...")
|
72 |
|
73 |
if user_input:
|
@@ -76,9 +84,10 @@ if user_input:
|
|
76 |
with st.chat_message("user"):
|
77 |
st.markdown(user_input)
|
78 |
|
79 |
-
|
80 |
with st.chat_message("assistant"):
|
81 |
-
|
|
|
82 |
assistant_response = response["answer"]
|
|
|
83 |
st.markdown(assistant_response)
|
84 |
st.session_state.chat_history.append({"role": "assistant", "content": assistant_response})
|
|
|
1 |
import os
|
|
|
|
|
2 |
import streamlit as st
|
3 |
from langchain_huggingface import HuggingFaceEmbeddings
|
4 |
from langchain_chroma import Chroma
|
|
|
6 |
from langchain.memory import ConversationBufferMemory
|
7 |
from langchain.chains import ConversationalRetrievalChain
|
8 |
|
9 |
+
# Ensure required environment variables are set
|
10 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
11 |
+
if not GROQ_API_KEY:
|
12 |
+
st.error("GROQ_API_KEY is not set. Please configure it in Hugging Face Spaces secrets.")
|
13 |
+
st.stop()
|
|
|
|
|
14 |
|
15 |
+
# Function to set up the vectorstore
|
16 |
def setup_vectorstore():
|
17 |
+
working_dir = os.path.dirname(os.path.abspath(__file__))
|
18 |
persist_directory = f"{working_dir}/vector_db_dir"
|
|
|
|
|
|
|
|
|
19 |
|
20 |
+
# Initialize HuggingFace Embeddings
|
21 |
+
embeddings = HuggingFaceEmbeddings()
|
22 |
+
|
23 |
+
# Initialize Chroma vectorstore
|
24 |
+
vectorstore = Chroma(
|
25 |
+
persist_directory=persist_directory,
|
26 |
+
embedding_function=embeddings
|
27 |
+
)
|
28 |
+
return vectorstore
|
29 |
|
30 |
+
# Function to set up the chat chain
|
31 |
def chat_chain(vectorstore):
|
32 |
+
llm = ChatGroq(model="llama-3.1-70b-versatile",
|
33 |
+
temperature=0,
|
34 |
+
groq_api_key=GROQ_API_KEY)
|
35 |
+
|
36 |
retriever = vectorstore.as_retriever()
|
37 |
memory = ConversationBufferMemory(
|
38 |
llm=llm,
|
|
|
40 |
memory_key="chat_history",
|
41 |
return_messages=True
|
42 |
)
|
43 |
+
|
44 |
chain = ConversationalRetrievalChain.from_llm(
|
45 |
llm=llm,
|
46 |
retriever=retriever,
|
|
|
49 |
verbose=True,
|
50 |
return_source_documents=True
|
51 |
)
|
|
|
52 |
return chain
|
53 |
|
54 |
+
# Streamlit UI configuration
|
55 |
st.set_page_config(
|
56 |
page_title="Multi Doc Chat",
|
57 |
+
page_icon="π",
|
58 |
layout="centered"
|
59 |
)
|
60 |
|
61 |
st.title("π Multi Documents Chatbot")
|
62 |
|
63 |
+
# Initialize session state variables
|
64 |
if "chat_history" not in st.session_state:
|
65 |
st.session_state.chat_history = []
|
66 |
|
67 |
if "vectorstore" not in st.session_state:
|
68 |
st.session_state.vectorstore = setup_vectorstore()
|
69 |
|
70 |
+
if "conversational_chain" not in st.session_state:
|
71 |
+
st.session_state.conversational_chain = chat_chain(st.session_state.vectorstore)
|
|
|
72 |
|
73 |
+
# Display chat history
|
74 |
for message in st.session_state.chat_history:
|
75 |
with st.chat_message(message["role"]):
|
76 |
st.markdown(message["content"])
|
77 |
|
78 |
+
# User input
|
79 |
user_input = st.chat_input("Ask AI...")
|
80 |
|
81 |
if user_input:
|
|
|
84 |
with st.chat_message("user"):
|
85 |
st.markdown(user_input)
|
86 |
|
|
|
87 |
with st.chat_message("assistant"):
|
88 |
+
# Generate response
|
89 |
+
response = st.session_state.conversational_chain({"question": user_input})
|
90 |
assistant_response = response["answer"]
|
91 |
+
|
92 |
st.markdown(assistant_response)
|
93 |
st.session_state.chat_history.append({"role": "assistant", "content": assistant_response})
|