Spaces:
Sleeping
Sleeping
update application file to include reranker and change embedding model
Browse files
app.py
CHANGED
|
@@ -7,6 +7,7 @@ from pypdf import PdfReader
|
|
| 7 |
from pathlib import Path
|
| 8 |
from weaviate.auth import AuthApiKey
|
| 9 |
from dotenv import load_dotenv
|
|
|
|
| 10 |
|
| 11 |
#Setup
|
| 12 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
|
@@ -67,7 +68,7 @@ def setup_schema():
|
|
| 67 |
#Create embeddings and Store in Vector DB
|
| 68 |
def embed(text):
|
| 69 |
return openai_client.embeddings.create(
|
| 70 |
-
model = "text-embedding-3-
|
| 71 |
input=text
|
| 72 |
).data[0].embedding
|
| 73 |
|
|
@@ -81,7 +82,7 @@ def insert_chunks(chunks):
|
|
| 81 |
)
|
| 82 |
|
| 83 |
# Querying
|
| 84 |
-
def search_weaviate(query, k=
|
| 85 |
pdf_chunks = client.collections.get("PDFChunk")
|
| 86 |
query_vec = embed(query)
|
| 87 |
|
|
@@ -90,22 +91,83 @@ def search_weaviate(query, k=3):
|
|
| 90 |
limit=k,
|
| 91 |
return_properties=["text", "page"]
|
| 92 |
)
|
| 93 |
-
return [o.properties["text"]for o in result.objects]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
def ask_question(query):
|
| 96 |
-
chunks = search_weaviate(query)
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
prompt = f"""
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
"""
|
| 103 |
|
| 104 |
response = openai_client.chat.completions.create(
|
| 105 |
model="gpt-4.1-mini",
|
| 106 |
-
messages=[
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
)
|
| 108 |
-
|
|
|
|
| 109 |
|
| 110 |
#Gradio App
|
| 111 |
def process_pdf(pdf_file):
|
|
|
|
| 7 |
from pathlib import Path
|
| 8 |
from weaviate.auth import AuthApiKey
|
| 9 |
from dotenv import load_dotenv
|
| 10 |
+
import re
|
| 11 |
|
| 12 |
#Setup
|
| 13 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
|
|
|
| 68 |
#Create embeddings and Store in Vector DB
|
| 69 |
def embed(text):
|
| 70 |
return openai_client.embeddings.create(
|
| 71 |
+
model = "text-embedding-3-large",
|
| 72 |
input=text
|
| 73 |
).data[0].embedding
|
| 74 |
|
|
|
|
| 82 |
)
|
| 83 |
|
| 84 |
# Querying
|
| 85 |
+
def search_weaviate(query, k=5):
|
| 86 |
pdf_chunks = client.collections.get("PDFChunk")
|
| 87 |
query_vec = embed(query)
|
| 88 |
|
|
|
|
| 91 |
limit=k,
|
| 92 |
return_properties=["text", "page"]
|
| 93 |
)
|
| 94 |
+
return [(o.properties["text"], o.metadata.distance)for o in result.objects]
|
| 95 |
+
|
| 96 |
+
def rerank_chunks_with_llm(query, chunks):
|
| 97 |
+
"""
|
| 98 |
+
Rerank retrieved chunks using GPT reasoning.
|
| 99 |
+
Returns a list of chunks ordered in descending order
|
| 100 |
+
"""
|
| 101 |
+
|
| 102 |
+
#Build a short reranking prompt
|
| 103 |
+
chunk_list = "\n\n".join([f"[{i+1}] {text[:400].strip().replace('\n', ' ')}..."
|
| 104 |
+
for i, (text, _) in enumerate(chunks)
|
| 105 |
+
])
|
| 106 |
+
|
| 107 |
+
rerank_prompt = f"""
|
| 108 |
+
You are a precise HR assistant that ranks excerpts
|
| 109 |
+
from a staff handbook by how relevant they are to the user's question
|
| 110 |
+
|
| 111 |
+
Question: {query}
|
| 112 |
+
|
| 113 |
+
Excerpts:
|
| 114 |
+
{chunk_list}
|
| 115 |
+
|
| 116 |
+
Return only the list of excerpt numbers, separated by commas, in descending order of relevance.
|
| 117 |
+
Example: 3, 1, 2
|
| 118 |
+
"""
|
| 119 |
+
|
| 120 |
+
#Run LLM model
|
| 121 |
+
response = openai_client.chat.completions.create(
|
| 122 |
+
model="gpt-4.1-mini",
|
| 123 |
+
messages=[
|
| 124 |
+
{"role": "system", "content": "You are a factual and consistent reranker."},
|
| 125 |
+
{"role": "user", "content": rerank_prompt}
|
| 126 |
+
],
|
| 127 |
+
temperature = 0
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
text_output = response.choices[0].message.content.strip()
|
| 131 |
+
print(f"🔎 Reranker raw output: {text_output}") # optional
|
| 132 |
+
|
| 133 |
+
# extract numbers safely
|
| 134 |
+
order = [int(x) for x in re.findall(r'\d+', text_output )]
|
| 135 |
+
order = [i for i in order if 1 <= i <= len(chunks)] #ensure valid range
|
| 136 |
+
|
| 137 |
+
# fallback: if model fails to output indices, return original order
|
| 138 |
+
if not order:
|
| 139 |
+
order = list(range(1, len(chunks) + 1))
|
| 140 |
+
|
| 141 |
+
# Return reordered text chunks
|
| 142 |
+
ordered_chunks = [chunks[i-1][0] for i in order]
|
| 143 |
+
return ordered_chunks
|
| 144 |
|
| 145 |
def ask_question(query):
|
| 146 |
+
chunks = search_weaviate(query, k=5)
|
| 147 |
+
reranked_chunks = rerank_chunks_with_llm(query, chunks)
|
| 148 |
+
|
| 149 |
+
# Use top three after reranking
|
| 150 |
+
context = "\n\n---\n\n".join(reranked_chunks[:3])
|
| 151 |
|
| 152 |
prompt = f"""
|
| 153 |
+
You are an HR assitant answering questions from the staff handbook.
|
| 154 |
+
Use only the following content to answer accurately and concisely:
|
| 155 |
+
{context}
|
| 156 |
+
|
| 157 |
+
Question: {query}
|
| 158 |
+
Answer:
|
| 159 |
"""
|
| 160 |
|
| 161 |
response = openai_client.chat.completions.create(
|
| 162 |
model="gpt-4.1-mini",
|
| 163 |
+
messages=[
|
| 164 |
+
{"role": "system", "content": "You are a helpful HR assistant."},
|
| 165 |
+
{"role": "user", "content": prompt}
|
| 166 |
+
],
|
| 167 |
+
temperature=0
|
| 168 |
)
|
| 169 |
+
|
| 170 |
+
return response.choices[0].message.content.strip()
|
| 171 |
|
| 172 |
#Gradio App
|
| 173 |
def process_pdf(pdf_file):
|