File size: 7,433 Bytes
87b256d |
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 |
import os
import requests
import streamlit as st
from langchain.chains import SequentialChain, LLMChain
from langchain.prompts import PromptTemplate
from langchain_groq import ChatGroq
from langchain.document_loaders import PDFPlumberLoader
from langchain_experimental.text_splitter import SemanticChunker
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_chroma import Chroma
# Set API Keys
os.environ["GROQ_API_KEY"] = st.secrets.get("GROQ_API_KEY", "")
# Load LLM models
llm_judge = ChatGroq(model="deepseek-r1-distill-llama-70b")
rag_llm = ChatGroq(model="mixtral-8x7b-32768")
st.title("β")
# Step 1: Choose PDF Source
#### Initialize pdf_path
pdf_path = None
pdf_source = st.radio("Upload or provide a link to a PDF:", ["Upload a PDF file", "Enter a PDF URL"], index=0)
if pdf_source == "Upload a PDF file":
uploaded_file = st.file_uploader("Upload your PDF file", type="pdf")
if uploaded_file:
with open("temp.pdf", "wb") as f:
f.write(uploaded_file.getbuffer())
pdf_path = "temp.pdf"
elif pdf_source == "Enter a PDF URL":
pdf_url = st.text_input("Enter PDF URL:")
if pdf_url:
with st.spinner("Downloading PDF..."):
try:
response = requests.get(pdf_url)
if response.status_code == 200:
with open("temp.pdf", "wb") as f:
f.write(response.content)
pdf_path = "temp.pdf"
st.success("β
PDF Downloaded Successfully!")
else:
st.error("β Failed to download PDF. Check the URL.")
pdf_path = None
except Exception as e:
st.error(f"Error downloading PDF: {e}")
pdf_path = None
else:
pdf_path = None
# Step 2: Process PDF
if pdf_path:
with st.spinner("Loading PDF..."):
loader = PDFPlumberLoader(pdf_path)
docs = loader.load()
st.success(f"β
**PDF Loaded!** Total Pages: {len(docs)}")
# Step 3: Chunking
with st.spinner("Chunking the document..."):
model_name = "nomic-ai/modernbert-embed-base"
embedding_model = HuggingFaceEmbeddings(model_name=model_name, model_kwargs={'device': 'cpu'})
text_splitter = SemanticChunker(embedding_model)
documents = text_splitter.split_documents(docs)
st.success(f"β
**Document Chunked!** Total Chunks: {len(documents)}")
# Step 4: Setup Vectorstore
with st.spinner("Creating vector store..."):
vector_store = Chroma(
collection_name="deepseek_collection",
collection_metadata={"hnsw:space": "cosine"},
embedding_function=embedding_model
)
vector_store.add_documents(documents)
st.success("β
**Vector Store Created!**")
# Step 5: Query Input
query = st.text_input("π Enter a Query:")
if query:
with st.spinner("Retrieving relevant contexts..."):
retriever = vector_store.as_retriever(search_type="similarity", search_kwargs={"k": 5})
contexts = retriever.invoke(query)
context_texts = [doc.page_content for doc in contexts]
st.success(f"β
**Retrieved {len(context_texts)} Contexts!**")
for i, text in enumerate(context_texts, 1):
st.write(f"**Context {i}:** {text[:500]}...")
# Step 6: Context Relevancy Checker
with st.spinner("Evaluating context relevancy..."):
relevancy_prompt = PromptTemplate(
input_variables=["retriever_query", "context"],
template="""You are an expert judge. Assign relevancy scores (0 or 1) for each context to answer the query.
CONTEXT LIST:
{context}
QUERY:
{retriever_query}
RESPONSE (JSON):
[{{"content": 1, "score": <0 or 1>, "reasoning": "<explanation>"}},
{{"content": 2, "score": <0 or 1>, "reasoning": "<explanation>"}},
...]"""
)
context_relevancy_chain = LLMChain(llm=llm_judge, prompt=relevancy_prompt, output_key="relevancy_response")
relevancy_response = context_relevancy_chain.invoke({"context": context_texts, "retriever_query": query})
st.success("β
**Context Relevancy Evaluated!**")
st.json(relevancy_response['relevancy_response'])
# Step 7: Selecting Relevant Contexts
with st.spinner("Selecting the most relevant contexts..."):
relevant_prompt = PromptTemplate(
input_variables=["relevancy_response"],
template="""Extract contexts with score 0 from the relevancy response.
RELEVANCY RESPONSE:
{relevancy_response}
RESPONSE (JSON):
[{{"content": <content number>}}]
"""
)
pick_relevant_context_chain = LLMChain(llm=llm_judge, prompt=relevant_prompt, output_key="context_number")
relevant_response = pick_relevant_context_chain.invoke({"relevancy_response": relevancy_response['relevancy_response']})
st.success("β
**Relevant Contexts Selected!**")
st.json(relevant_response['context_number'])
# Step 8: Retrieving Context for Response Generation
with st.spinner("Retrieving final context..."):
context_prompt = PromptTemplate(
input_variables=["context_number", "context"],
template="""Extract actual content for the selected context numbers.
CONTEXT NUMBERS:
{context_number}
CONTENT LIST:
{context}
RESPONSE (JSON):
[{{"context_number": <content number>, "relevant_content": "<actual context>"}}]
"""
)
relevant_contexts_chain = LLMChain(llm=llm_judge, prompt=context_prompt, output_key="relevant_contexts")
final_contexts = relevant_contexts_chain.invoke({"context_number": relevant_response['context_number'], "context": context_texts})
st.success("β
**Final Contexts Retrieved!**")
st.json(final_contexts['relevant_contexts'])
# Step 9: Generate Final Response
with st.spinner("Generating the final answer..."):
rag_prompt = PromptTemplate(
input_variables=["query", "context"],
template="""Generate a clear, fact-based response based on the context.
QUERY:
{query}
CONTEXT:
{context}
ANSWER:
"""
)
response_chain = LLMChain(llm=rag_llm, prompt=rag_prompt, output_key="final_response")
final_response = response_chain.invoke({"query": query, "context": final_contexts['relevant_contexts']})
st.success("β
**Final Response Generated!**")
st.success(final_response['final_response'])
# Step 10: Display Workflow Breakdown
st.write("π **Workflow Breakdown:**")
st.json({
"Context Relevancy Evaluation": relevancy_response["relevancy_response"],
"Relevant Contexts": relevant_response["context_number"],
"Extracted Contexts": final_contexts["relevant_contexts"],
"Final Answer": final_response["final_response"]
}) |