Spaces:
Sleeping
Sleeping
Delete rag_pipeline.py
Browse files- rag_pipeline.py +0 -65
rag_pipeline.py
DELETED
|
@@ -1,65 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import re # Added for stripping <think> tags
|
| 3 |
-
from langchain_groq import ChatGroq
|
| 4 |
-
from langchain_core.messages import HumanMessage
|
| 5 |
-
from embedding_functions import embed_text
|
| 6 |
-
|
| 7 |
-
# 1. FIX: Update to the working Qwen 3.6 model string
|
| 8 |
-
llm = ChatGroq(model="qwen/qwen3.6-27b")
|
| 9 |
-
|
| 10 |
-
def retrieve_multimodal(query, vector_store, k=8):
|
| 11 |
-
# FIX: Bypass raw CLIP vector comparison for the query string retrieval
|
| 12 |
-
# and search against your document's text/metadata segments directly!
|
| 13 |
-
results = vector_store.similarity_search(
|
| 14 |
-
query = query,
|
| 15 |
-
k = k
|
| 16 |
-
)
|
| 17 |
-
return results
|
| 18 |
-
|
| 19 |
-
def create_multimodal_message(query, retrieved_docs, image_data_store):
|
| 20 |
-
content = []
|
| 21 |
-
|
| 22 |
-
# Keep a running string for all text blocks to pass a clean structure
|
| 23 |
-
text_context = f"Question: {query}\n\nContext:\n"
|
| 24 |
-
|
| 25 |
-
text_docs = [doc for doc in retrieved_docs if doc.metadata.get("type") == "text"]
|
| 26 |
-
image_docs = [doc for doc in retrieved_docs if doc.metadata.get("type") == "image"]
|
| 27 |
-
|
| 28 |
-
if text_docs:
|
| 29 |
-
text_context += "Text excerpts:\n" + "\n\n".join([
|
| 30 |
-
f"[Page {doc.metadata['page']}]: {doc.page_content}"
|
| 31 |
-
for doc in text_docs
|
| 32 |
-
]) + "\n"
|
| 33 |
-
|
| 34 |
-
# Append aggregated text first
|
| 35 |
-
content.append({
|
| 36 |
-
"type": "text",
|
| 37 |
-
"text": text_context
|
| 38 |
-
})
|
| 39 |
-
|
| 40 |
-
# 2. FIX: Properly append images sequentially
|
| 41 |
-
for doc in image_docs:
|
| 42 |
-
image_id = doc.metadata.get("image_id")
|
| 43 |
-
if image_id and image_id in image_data_store:
|
| 44 |
-
content.append({
|
| 45 |
-
"type": "image_url",
|
| 46 |
-
"image_url": {
|
| 47 |
-
"url": f"data:image/png;base64,{image_data_store[image_id]}"
|
| 48 |
-
}
|
| 49 |
-
})
|
| 50 |
-
|
| 51 |
-
content.append({
|
| 52 |
-
"type": "text",
|
| 53 |
-
"text": "\n\nPlease answer the question based strictly on the provided text and images above."
|
| 54 |
-
})
|
| 55 |
-
|
| 56 |
-
return HumanMessage(content = content)
|
| 57 |
-
|
| 58 |
-
def answer_query(query, vector_store, image_data_store):
|
| 59 |
-
context_docs = retrieve_multimodal(query, vector_store, k=5)
|
| 60 |
-
message = create_multimodal_message(query, context_docs, image_data_store)
|
| 61 |
-
response = llm.invoke([message])
|
| 62 |
-
|
| 63 |
-
# 3. FIX: Clean out the <think> blocks so it displays beautifully in Streamlit
|
| 64 |
-
clean_content = re.sub(r"<think>.*?</think>", "", response.content, flags=re.DOTALL).strip()
|
| 65 |
-
return clean_content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|