-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlangchain_app.py
288 lines (248 loc) · 12.5 KB
/
langchain_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import streamlit as st
from PyPDF2 import PdfReader
import langchain
import pandas as pd
import base64
from langchain.text_splitter import RecursiveCharacterTextSplitter, CharacterTextSplitter
from langchain.embeddings import OpenAIEmbeddings, HuggingFaceInstructEmbeddings
from langchain_google_genai import GoogleGenerativeAIEmbeddings
from langchain_community.vectorstores import FAISS
from langchain.chat_models import ChatOpenAI
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationalRetrievalChain
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain.chains.question_answering import load_qa_chain
from langchain.prompts import PromptTemplate
from dotenv import load_dotenv
import os
load_dotenv()
from datetime import datetime
def get_pdf_text(pdf_docs):
text = ""
for pdf in pdf_docs:
pdf_reader = PdfReader(pdf)
for page in pdf_reader.pages:
text += page.extract_text()
return text
def get_text_chunks(text, model_name):
if model_name == "OpenAI":
text_splitter = CharacterTextSplitter(
separator="\n",
chunk_size=1000,
chunk_overlap=200,
length_function=len
)
elif model_name == "Google AI":
text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000)
chunks = text_splitter.split_text(text)
return chunks
def get_vector_store(text_chunks, model_name, api_key=None):
if model_name == "OpenAI":
embeddings = OpenAIEmbeddings(api_key = api_key)
elif model_name == "Google AI":
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001", google_api_key=api_key)
vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
vector_store.save_local("faiss_index")
return vector_store
def get_conversational_chain(model_name, vectorstore=None, api_key=None):
if model_name == "OpenAI":
llm = ChatOpenAI(api_key=api_key)
memory = ConversationBufferMemory(memory_key='chat_history', return_messages=True)
conversation_chain = ConversationalRetrievalChain.from_llm(
llm=llm,
retriever=vectorstore.as_retriever(),
memory=memory
)
return conversation_chain
elif model_name == "Google AI":
prompt_template = """
Answer the question as detailed as possible from the provided context, make sure to provide all the details, if the answer is not in
provided context just say, "answer is not available in the context", don't provide the wrong answer\n\n
Context:\n {context}?\n
Question: \n{question}\n
Answer:
"""
model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.3, google_api_key=api_key)
prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
return chain
def user_input(user_question, model_name, api_key, pdf_docs, conversation_history):
if api_key is None or pdf_docs is None:
st.warning("Please upload PDF files and provide API key before processing.")
return
text_chunks = get_text_chunks(get_pdf_text(pdf_docs), model_name)
vector_store = get_vector_store(text_chunks, model_name, api_key)
user_question_output = ""
response_output = ""
if model_name == "OpenAI":
llm = ChatOpenAI(api_key=api_key)
memory = ConversationBufferMemory(memory_key='chat_history', return_messages=True)
conversation_chain = ConversationalRetrievalChain.from_llm(
llm=llm,
retriever=vector_store.as_retriever(),
memory=memory
)
response = conversation_chain({'question': user_question})
user_question_output = user_question
response_output = response['chat_history'][-1].content
pdf_names = [pdf.name for pdf in pdf_docs] if pdf_docs else []
conversation_history.append((user_question_output, response_output, model_name, datetime.now().strftime('%Y-%m-%d %H:%M:%S'), ", ".join(pdf_names)))
# conversation_history.append((user_question_output, response_output, datetime.now().strftime('%Y-%m-%d %H:%M:%S'), ", ".join(pdf_names)))
elif model_name == "Google AI":
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001", google_api_key=api_key)
new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
docs = new_db.similarity_search(user_question)
chain = get_conversational_chain("Google AI", vectorstore=new_db, api_key=api_key)
response = chain({"input_documents": docs, "question": user_question}, return_only_outputs=True)
user_question_output = user_question
response_output = response['output_text']
pdf_names = [pdf.name for pdf in pdf_docs] if pdf_docs else []
conversation_history.append((user_question_output, response_output, model_name, datetime.now().strftime('%Y-%m-%d %H:%M:%S'), ", ".join(pdf_names)))
# conversation_history.append((user_question_output, response_output, datetime.now().strftime('%Y-%m-%d %H:%M:%S'), ", ".join(pdf_names)))
# Kullanıcının sorduğu soruyu ve cevabı bir banner olarak ekleyelim
st.markdown(
f"""
<style>
.chat-message {{
padding: 1.5rem;
border-radius: 0.5rem;
margin-bottom: 1rem;
display: flex;
}}
.chat-message.user {{
background-color: #2b313e;
}}
.chat-message.bot {{
background-color: #475063;
}}
.chat-message .avatar {{
width: 20%;
}}
.chat-message .avatar img {{
max-width: 78px;
max-height: 78px;
border-radius: 50%;
object-fit: cover;
}}
.chat-message .message {{
width: 80%;
padding: 0 1.5rem;
color: #fff;
}}
.chat-message .info {{
font-size: 0.8rem;
margin-top: 0.5rem;
color: #ccc;
}}
</style>
<div class="chat-message user">
<div class="avatar">
<img src="https://i.ibb.co/CKpTnWr/user-icon-2048x2048-ihoxz4vq.png">
</div>
<div class="message">{user_question_output}</div>
</div>
<div class="chat-message bot">
<div class="avatar">
<img src="https://i.ibb.co/wNmYHsx/langchain-logo.webp" >
</div>
<div class="message">{response_output}</div>
</div>
""",
unsafe_allow_html=True
)
# <div class="info" style="margin-left: 20px;">Timestamp: {datetime.now()}</div>
# <div class="info" style="margin-left: 20px;">PDF Name: {", ".join(pdf_names)}</div>
if len(conversation_history) == 1:
conversation_history = []
elif len(conversation_history) > 1 :
last_item = conversation_history[-1] # Son öğeyi al
conversation_history.remove(last_item)
for question, answer, model_name, timestamp, pdf_name in reversed(conversation_history):
st.markdown(
f"""
<div class="chat-message user">
<div class="avatar">
<img src="https://i.ibb.co/CKpTnWr/user-icon-2048x2048-ihoxz4vq.png">
</div>
<div class="message">{question}</div>
</div>
<div class="chat-message bot">
<div class="avatar">
<img src="https://i.ibb.co/wNmYHsx/langchain-logo.webp" >
</div>
<div class="message">{answer}</div>
</div>
""",
unsafe_allow_html=True
)
# <div class="info" style="margin-left: 20px;">Timestamp: {timestamp}</div>
# <div class="info" style="margin-left: 20px;">PDF Name: {pdf_name}</div>
if len(st.session_state.conversation_history) > 0:
df = pd.DataFrame(st.session_state.conversation_history, columns=["Question", "Answer", "Model", "Timestamp", "PDF Name"])
# df = pd.DataFrame(st.session_state.conversation_history, columns=["Question", "Answer", "Timestamp", "PDF Name"])
csv = df.to_csv(index=False)
b64 = base64.b64encode(csv.encode()).decode() # Convert to base64
href = f'<a href="data:file/csv;base64,{b64}" download="conversation_history.csv"><button>Download conversation history as CSV file</button></a>'
st.sidebar.markdown(href, unsafe_allow_html=True)
st.markdown("To download the conversation, click the Download button on the left side at the bottom of the conversation.")
st.snow()
def main():
st.set_page_config(page_title="Chat with multiple PDFs", page_icon=":books:")
st.header("Chat with multiple PDFs (v1) :books:")
if 'conversation_history' not in st.session_state:
st.session_state.conversation_history = []
linkedin_profile_link = "https://www.linkedin.com/in/huseyincenik/"
kaggle_profile_link = "https://www.kaggle.com/huseyincenik/"
github_profile_link = "https://github.com/huseyincenik/"
st.sidebar.markdown(
f"[]({linkedin_profile_link}) "
f"[]({kaggle_profile_link}) "
f"[]({github_profile_link})"
)
model_name = st.sidebar.radio("Select the Model:", ("OpenAI", "Google AI"))
api_key = None
if model_name == "Google AI":
api_key = st.sidebar.text_input("Enter your Google API Key:")
st.sidebar.markdown("Click [here](https://ai.google.dev/) to get an API key.")
if not api_key:
st.sidebar.warning("Please enter your Google API Key to proceed.")
return
if model_name == "OpenAI":
api_key = st.sidebar.text_input("Enter your OpenAI API Key:")
st.sidebar.markdown("Click [here](https://openai.com/blog/openai-api) to get an API key.")
if not api_key:
st.sidebar.warning("Please enter your OpenAI API Key to proceed.")
return
with st.sidebar:
st.title("Menu:")
col1, col2 = st.columns(2)
reset_button = col2.button("Reset")
clear_button = col1.button("Rerun")
if reset_button:
st.session_state.conversation_history = [] # Clear conversation history
st.session_state.user_question = None # Clear user question input
api_key = None # Reset Google API key
pdf_docs = None # Reset PDF document
else:
if clear_button:
if 'user_question' in st.session_state:
st.warning("The previous query will be discarded.")
st.session_state.user_question = "" # Temizle
if len(st.session_state.conversation_history) > 0:
st.session_state.conversation_history.pop() # Son sorguyu kaldır
else:
st.warning("The question in the input will be queried again.")
pdf_docs = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button", accept_multiple_files=True)
if st.button("Submit & Process"):
if pdf_docs:
with st.spinner("Processing..."):
st.success("Done")
else:
st.warning("Please upload PDF files before processing.")
user_question = st.text_input("Ask a Question from the PDF Files")
if user_question:
user_input(user_question, model_name, api_key, pdf_docs, st.session_state.conversation_history)
st.session_state.user_question = "" # Clear user question input
if __name__ == "__main__":
main()