Spaces:
Build error
Build error
Update app.py
Browse files
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
|
| 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:", ["
|
| 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 |
-
|
| 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 |
-
st.subheader("Response Chain")
|
| 197 |
-
st.json(response_chain)
|
| 198 |
|
| 199 |
-
|
| 200 |
|
| 201 |
-
|
| 202 |
-
#st.subheader("blah response")
|
| 203 |
-
#st.json(response.content)
|
| 204 |
|
| 205 |
-
|
| 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 |
-
|
| 230 |
-
#st.subheader("response_crisis")
|
| 231 |
-
#st.json((response_crisis))
|
| 232 |
|
| 233 |
-
|
| 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 |
-
|
| 242 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
|
| 244 |
-
|
| 245 |
-
#st.json(contexts["relevant_contexts"])
|
| 246 |
|
| 247 |
-
|
| 248 |
-
|
| 249 |
|
| 250 |
-
|
| 251 |
-
|
| 252 |
|
| 253 |
-
|
| 254 |
-
|
| 255 |
|
| 256 |
-
|
| 257 |
-
|
|
|
|
| 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"])
|