DrishtiSharma commited on
Commit
caff312
Β·
verified Β·
1 Parent(s): a485500

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +121 -86
app.py CHANGED
@@ -9,7 +9,7 @@ from langchain.document_loaders import PDFPlumberLoader
9
  from langchain_experimental.text_splitter import SemanticChunker
10
  from langchain_huggingface import HuggingFaceEmbeddings
11
  from langchain_chroma import Chroma
12
- from prompts import rag_prompt, relevancy_prompt, relevant_context_picker_prompt, response_synth
13
 
14
  # Set API Keys
15
  os.environ["GROQ_API_KEY"] = st.secrets.get("GROQ_API_KEY", "")
@@ -41,7 +41,7 @@ if "vector_created" not in st.session_state:
41
  st.title("Blah-2")
42
 
43
  # Step 1: Choose PDF Source
44
- pdf_source = st.radio("Upload or provide a link to a PDF:", ["Upload a PDF file", "Enter a PDF URL"], index=0, horizontal=True)
45
 
46
  if pdf_source == "Upload a PDF file":
47
  uploaded_file = st.file_uploader("Upload your PDF file", type="pdf")
@@ -54,7 +54,7 @@ if pdf_source == "Upload a PDF file":
54
  st.session_state.vector_created = False
55
 
56
  elif pdf_source == "Enter a PDF URL":
57
- pdf_url = st.text_input("Enter PDF URL:")
58
  if pdf_url and not st.session_state.pdf_path:
59
  with st.spinner("Downloading PDF..."):
60
  try:
@@ -157,101 +157,136 @@ if query:
157
  # Debugging: Check extracted context
158
  st.write("Extracted Context (page_content):", context)
159
  st.write("Number of Extracted Contexts:", len(context))
160
- # ----------------- Run Individual Chains Explicitly -----------------
161
-
162
- context_relevancy_checker_prompt = PromptTemplate(input_variables=["retriever_query","context"],template=relevancy_prompt)
163
-
164
- if "context_relevancy_evaluation_chain" in st.session_state:
165
- del st.session_state["context_relevancy_evaluation_chain"]
166
-
167
-
168
-
169
- context_relevancy_evaluation_chain = LLMChain( llm=llm_judge, prompt=PromptTemplate(input_variables=["retriever_query","context"],template=relevancy_prompt), output_key="relevancy_response" )
170
-
171
- st.write("πŸš€ Debugging Expected Keys for `context_relevancy_evaluation_chain`:")
172
- st.write(context_relevancy_evaluation_chain.input_keys)
173
-
174
- response_crisis = context_relevancy_evaluation_chain.invoke({"context":context,"retriever_query":query})
175
-
176
- relevant_prompt = relevant_prompt = PromptTemplate(input_variables=["relevancy_response"], template= relevant_context_picker_prompt)
177
-
178
- pick_relevant_context_chain = LLMChain(llm=llm_judge, prompt=relevant_prompt, output_key="context_number")
179
-
180
- relevant_response = pick_relevant_context_chain.invoke({"relevancy_response":response_crisis['relevancy_response']})
181
-
182
- context_prompt = PromptTemplate(input_variables=["context_number"],template=response_synth)
183
-
184
- relevant_contexts_chain = LLMChain(llm=llm_judge, prompt=context_prompt, output_key="relevant_contexts")
185
-
186
- contexts = relevant_contexts_chain.invoke({"context_number":relevant_response['context_number'],"context":context})
187
-
188
- #temp
189
- st.subheader("Relevant Contexts")
190
- st.json(contexts['relevant_contexts'])
191
-
192
- response_chain = LLMChain(llm=rag_llm,prompt=final_prompt,output_key="final_response")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
193
 
 
194
 
195
- #temp
196
- st.subheader("Response Chain")
197
- st.json(response_chain)
198
 
199
- #response = chain.invoke({"query":query,"context":contexts['relevant_contexts']})
200
 
201
- #temp
202
- #st.subheader("blah response")
203
- #st.json(response.content)
204
 
205
- # Orchestrate using SequentialChain
206
- context_management_chain = SequentialChain(
207
- chains=[context_relevancy_evaluation_chain ,pick_relevant_context_chain, relevant_contexts_chain,response_chain],
208
- input_variables=["context","retriever_query","query"],
209
- output_variables=["relevancy_response", "context_number","relevant_contexts","final_response"]
210
- )
211
- final_output = context_management_chain({"context":context,"retriever_query":query,"query":query})
212
- st.subheader("Final Output from Context Management chain")
213
- st.json(final_output)
214
-
215
- st.subheader("Context of Final Output from Context Management chain")
216
- st.json(final_output['context'])
217
-
218
- st.header("Relevancy Response")
219
- st.json(final_output['relevancy_response'])
220
-
221
- st.subheader("Relevant Context")
222
- st.json(final_output['relevant_contexts'])
223
-
224
- response = chain.invoke({"query":query,"context":final_output['relevant_contexts']})
225
-
226
- st.subheader("Final Response")
227
- st.json(response.content)
228
 
229
- # ----------------- Display All Outputs -----------------
230
- #st.subheader("response_crisis")
231
- #st.json((response_crisis))
232
 
233
- #st.subheader("response_crisis['relevancy_response']")
234
- #st.json((response_crisis['relevancy_response']))
235
 
 
236
 
237
-
238
- #st.markdown("### Context Relevancy Evaluation")
239
- #st.json(response_crisis["relevancy_response"])
240
 
241
- #st.markdown("### Picked Relevant Contexts")
242
- #st.json(relevant_response["context_number"])
 
 
 
 
243
 
244
- #st.markdown("### Extracted Relevant Contexts")
245
- #st.json(contexts["relevant_contexts"])
246
 
247
- #st.subheader("context_relevancy_evaluation_chain Statement")
248
- #st.json(final_response["relevancy_response"])
249
 
250
- #st.subheader("pick_relevant_context_chain Statement")
251
- #st.json(final_response["context_number"])
252
 
253
- #st.subheader("relevant_contexts_chain Statement")
254
- #st.json(final_response["relevant_contexts"])
255
 
256
- #st.subheader("RAG Response Statement")
257
- #st.json(final_response["final_response"])
 
9
  from langchain_experimental.text_splitter import SemanticChunker
10
  from langchain_huggingface import HuggingFaceEmbeddings
11
  from langchain_chroma import Chroma
12
+ from langchain.chains import SequentialChain, LLMChain
13
 
14
  # Set API Keys
15
  os.environ["GROQ_API_KEY"] = st.secrets.get("GROQ_API_KEY", "")
 
41
  st.title("Blah-2")
42
 
43
  # Step 1: Choose PDF Source
44
+ pdf_source = st.radio("Upload or provide a link to a PDF:", ["Enter a PDF URL", "Upload a PDF file"], index=0, horizontal=True)
45
 
46
  if pdf_source == "Upload a PDF file":
47
  uploaded_file = st.file_uploader("Upload your PDF file", type="pdf")
 
54
  st.session_state.vector_created = False
55
 
56
  elif pdf_source == "Enter a PDF URL":
57
+ pdf_url = st.text_input("Enter PDF URL:", value = "https://arxiv.org/pdf/2406.06998")
58
  if pdf_url and not st.session_state.pdf_path:
59
  with st.spinner("Downloading PDF..."):
60
  try:
 
157
  # Debugging: Check extracted context
158
  st.write("Extracted Context (page_content):", context)
159
  st.write("Number of Extracted Contexts:", len(context))
160
+
161
+ relevancy_prompt = """You are an expert judge tasked with evaluating whether the EACH OF THE CONTEXT provided in the CONTEXT LIST is self sufficient to answer the QUERY asked.
162
+ Analyze the provided QUERY AND CONTEXT to determine if each Ccontent in the CONTEXT LIST contains Relevant information to answer the QUERY.
163
+
164
+ Guidelines:
165
+ 1. The content must not introduce new information beyond what's provided in the QUERY.
166
+ 2. Pay close attention to the subject of statements. Ensure that attributes, actions, or dates are correctly associated with the right entities (e.g., a person vs. a TV show they star in).
167
+ 3. Be vigilant for subtle misattributions or conflations of information, even if the date or other details are correct.
168
+ 4. Check that the content in the CONTEXT LIST doesn't oversimplify or generalize information in a way that changes the meaning of the QUERY.
169
+
170
+ Analyze the text thoroughly and assign a relevancy score 0 or 1 where:
171
+ - 0: The content has all the necessary information to answer the QUERY
172
+ - 1: The content does not has the necessary information to answer the QUERY
173
+
174
+ ```
175
+ EXAMPLE:
176
+ INPUT (for context only, not to be used for faithfulness evaluation):
177
+ What is the capital of France?
178
+
179
+ CONTEXT:
180
+ ['France is a country in Western Europe. Its capital is Paris, which is known for landmarks like the Eiffel Tower.',
181
+ 'Mr. Naveen patnaik has been the chief minister of Odisha for consequetive 5 terms']
182
+
183
+ OUTPUT:
184
+ The Context has sufficient information to answer the query.
185
+
186
+ RESPONSE:
187
+ {{"score":0}}
188
+ ```
189
+
190
+ CONTENT LIST:
191
+ {context}
192
+
193
+ QUERY:
194
+ {retriever_query}
195
+ Provide your verdict in JSON format with a single key 'score' and no preamble or explanation:
196
+ [{{"content:1,"score": <your score either 0 or 1>,"Reasoning":<why you have chose the score as 0 or 1>}},
197
+ {{"content:2,"score": <your score either 0 or 1>,"Reasoning":<why you have chose the score as 0 or 1>}},
198
+ ...]
199
+ """
200
+
201
+ context_relevancy_checker_prompt = PromptTemplate(input_variables=["retriever_query","context"],template=relevancy_prompt)
202
+
203
+ relevant_prompt = PromptTemplate(
204
+ input_variables=["relevancy_response"],
205
+ template="""
206
+ Your main task is to analyze the json structure as a part of the Relevancy Response.
207
+ Review the Relevancy Response and do the following:-
208
+ (1) Look at the Json Structure content
209
+ (2) Analyze the 'score' key in the Json Structure content.
210
+ (3) pick the value of 'content' key against those 'score' key value which has 0.
211
+
212
+ Relevancy Response:
213
+ {relevancy_response}
214
+
215
+ Provide your verdict in JSON format with a single key 'content number' and no preamble or explanation:
216
+ [{{"content":<content number>}}]
217
+ """
218
+ )
219
+
220
+ context_prompt = PromptTemplate(
221
+ input_variables=["context_number"],
222
+ template="""
223
+ You main task is to analyze the json structure as a part of the Context Number Response and the list of Contexts provided in the 'Content List' and perform the following steps:-
224
+ (1) Look at the output from the Relevant Context Picker Agent.
225
+ (2) Analyze the 'content' key in the Json Structure format({{"content":<<content_number>>}}).
226
+ (3) Retrieve the value of 'content' key and pick up the context corresponding to that element from the Content List provided.
227
+ (4) Pass the retrieved context for each corresponing element number referred in the 'Context Number Response'
228
+
229
+ Context Number Response:
230
+ {context_number}
231
+
232
+ Content List:
233
+ {context}
234
+
235
+ Provide your verdict in JSON format with a two key 'relevant_content' and 'context_number' no preamble or explanation:
236
+ [{{"context_number":<content1>,"relevant_content":<content corresponing to that element 1 in the Content List>}},
237
+ {{"context_number":<content4>,"relevant_content":<content corresponing to that element 4 in the Content List>}},
238
+ ...
239
+ ]
240
+ """
241
+ )
242
+
243
+ rag_prompt = """ You are ahelpful assistant very profiient in formulating clear and meaningful answers from the context provided.Based on the CONTEXT Provided ,Please formulate
244
+ a clear concise and meaningful answer for the QUERY asked.Please refrain from making up your own answer in case the COTEXT provided is not sufficient to answer the QUERY.In such a situation please respond as 'I do not know'.
245
+
246
+ QUERY:
247
+ {query}
248
+
249
+ CONTEXT
250
+ {context}
251
+
252
+ ANSWER:
253
+ """
254
 
255
+ context_relevancy_evaluation_chain = LLMChain(llm=llm_judge, prompt=context_relevancy_checker_prompt, output_key="relevancy_response")
256
 
257
+ response_crisis = context_relevancy_evaluation_chain.invoke({"context":context,"retriever_query":query})
 
 
258
 
259
+ pick_relevant_context_chain = LLMChain(llm=llm_judge, prompt=relevant_prompt, output_key="context_number")
260
 
261
+ relevant_response = pick_relevant_context_chain.invoke({"relevancy_response":response_crisis['relevancy_response']})
 
 
262
 
263
+ relevant_contexts_chain = LLMChain(llm=llm_judge, prompt=context_prompt, output_key="relevant_contexts")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
264
 
265
+ contexts = relevant_contexts_chain.invoke({"context_number":relevant_response['context_number'],"context":context})
 
 
266
 
267
+ final_prompt = PromptTemplate(input_variables=["query","context"],template=rag_prompt)
 
268
 
269
+ response_chain = LLMChain(llm=rag_llm,prompt=final_prompt,output_key="final_response")
270
 
271
+ response = response_chain.invoke({"query":query,"context":contexts['relevant_contexts']})
 
 
272
 
273
+ # Orchestrate using SequentialChain
274
+ context_management_chain = SequentialChain(
275
+ chains=[context_relevancy_evaluation_chain ,pick_relevant_context_chain, relevant_contexts_chain,response_chain],
276
+ input_variables=["context","retriever_query","query"],
277
+ output_variables=["relevancy_response", "context_number","relevant_contexts","final_response"]
278
+ )
279
 
280
+ final_output = context_management_chain({"context":context,"retriever_query":query,"query":query})
 
281
 
282
+ st.subheader('final_output["relevancy_response"]')
283
+ st.json(final_output["relevancy_response"] )
284
 
285
+ st.subheader('final_output["context_number"]')
286
+ st.json(final_output["context_number"])
287
 
288
+ st.subheader('final_output["relevant_contexts"]')
289
+ st.json(final_output["relevant_contexts"])
290
 
291
+ st.subheader('final_output["final_response"]')
292
+ st.json(final_output["final_response"])