-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathretrieve.py
More file actions
241 lines (202 loc) · 7.67 KB
/
retrieve.py
File metadata and controls
241 lines (202 loc) · 7.67 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import argparse
import json
import logging
import os
import sys
import time
from typing import Any, Dict, List, Optional
from imports import (
HuggingFaceEmbeddings,
LangchainEmbedding,
Settings,
StorageContext,
load_index_from_storage,
FaissVectorStore,
)
def configure_logging(level: str) -> None:
logging.basicConfig(
stream=sys.stdout,
level=getattr(logging, level.upper(), logging.INFO),
format="%(asctime)s %(levelname)s %(name)s - %(message)s",
)
def load_questions(path: str, question_key: str, id_key: Optional[str]) -> List[Dict[str, Any]]:
with open(path, "r", encoding="utf-8") as f:
payload = json.load(f)
if isinstance(payload, dict) and "questions" in payload:
payload = payload["questions"]
questions: List[Dict[str, Any]] = []
for idx, entry in enumerate(payload):
if isinstance(entry, str):
question_text = entry
question_id = idx
elif isinstance(entry, dict):
if question_key not in entry:
# fall back to common alternatives
for fallback in ("question", "query", "text"):
if fallback in entry:
question_text = entry[fallback]
break
else:
raise KeyError(f"Question key '{question_key}' not found in entry {idx}")
else:
question_text = entry[question_key]
if id_key and id_key in entry:
question_id = entry[id_key]
else:
question_id = entry.get("question_id") or entry.get("id") or idx
else:
raise TypeError(f"Unsupported question entry type: {type(entry)} at index {idx}")
questions.append(
{
"id": question_id,
"question": question_text,
"raw": entry,
}
)
return questions
def to_serializable(value: Any) -> Any:
if value is None or isinstance(value, (str, int, float, bool)):
return value
if isinstance(value, list):
return [to_serializable(item) for item in value]
if isinstance(value, dict):
return {str(key): to_serializable(val) for key, val in value.items()}
try:
import numpy as np
if isinstance(value, np.generic):
return value.item()
if isinstance(value, np.ndarray):
return value.tolist()
except ImportError:
pass
return str(value)
def extract_document_text(document: Any) -> str:
for attr in ("text", "get_content"):
if hasattr(document, attr):
candidate = getattr(document, attr)
if callable(candidate):
try:
return candidate()
except TypeError:
continue
return candidate
if hasattr(document, "text_resource"):
text_res = getattr(document, "text_resource")
if hasattr(text_res, "text"):
return text_res.text
return ""
def load_index(vector_store_path: str, embedding_model_name: str, chunk_size: int) -> Any:
logging.info("Loading embedding model %s", embedding_model_name)
lc_embed_model = HuggingFaceEmbeddings(model_name=embedding_model_name)
Settings.embed_model = LangchainEmbedding(lc_embed_model)
Settings.chunk_size = chunk_size
logging.info("Loading persisted vector store from %s", vector_store_path)
vector_store = FaissVectorStore.from_persist_dir(vector_store_path)
storage_context = StorageContext.from_defaults(
vector_store=vector_store, persist_dir=vector_store_path
)
index = load_index_from_storage(storage_context=storage_context)
return index
def run_retrieval(index: Any, questions: List[Dict[str, Any]], top_k: int) -> List[Dict[str, Any]]:
retriever = index.as_retriever(similarity_top_k=top_k)
results: List[Dict[str, Any]] = []
for position, item in enumerate(questions):
question_text = item["question"]
question_id = item["id"]
logging.info("Retrieving tables for question %s (%s)", question_id, question_text[:80])
start = time.time()
retrieved_nodes = retriever.retrieve(question_text)
elapsed = time.time() - start
serialized_nodes = []
for node in retrieved_nodes:
doc = getattr(node, "node", node)
metadata = getattr(doc, "metadata", {}) or {}
serialized_nodes.append(
{
"table_id": metadata.get("table_id"),
"table_name": metadata.get("table_name"),
"score": float(getattr(node, "score", 0.0) or 0.0),
"metadata": to_serializable(metadata),
"text": extract_document_text(doc),
}
)
results.append(
{
"question_id": question_id,
"question": question_text,
"position": position,
"retrieval_time_sec": elapsed,
"results": serialized_nodes,
}
)
return results
def save_results(output_path: str, payload: Dict[str, Any]) -> None:
os.makedirs(os.path.dirname(output_path) or ".", exist_ok=True)
with open(output_path, "w", encoding="utf-8") as f:
json.dump(payload, f, indent=2, ensure_ascii=False)
logging.info("Saved retrieval results to %s", output_path)
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Run base table retrieval and export candidate tables per question."
)
parser.add_argument("--questions-file", required=True, help="Path to questions JSON file.")
parser.add_argument(
"--vector-store-path",
required=True,
help="Directory containing the persisted LlamaIndex/FAISS vector store.",
)
parser.add_argument(
"--embedding-model",
default="BAAI/bge-base-en-v1.5",
help="HuggingFace embedding model used when the vector store was created.",
)
parser.add_argument(
"--chunk-size",
type=int,
default=7500,
help="Chunk size to register with the LlamaIndex Settings. Should match persistence.",
)
parser.add_argument(
"--top-k",
type=int,
default=5,
help="Number of tables to retrieve per question.",
)
parser.add_argument(
"--output-file",
required=True,
help="Destination JSON file for retrieval results.",
)
parser.add_argument(
"--question-key",
default="question",
help="Key inside each question object that holds the natural language question.",
)
parser.add_argument(
"--id-key",
default=None,
help="Optional key holding the question identifier. Falls back to 'question_id', then index.",
)
parser.add_argument(
"--log-level",
default="INFO",
help="Logging level (DEBUG, INFO, WARNING, ERROR).",
)
return parser.parse_args()
def main() -> None:
args = parse_args()
configure_logging(args.log_level)
questions = load_questions(args.questions_file, args.question_key, args.id_key)
index = load_index(args.vector_store_path, args.embedding_model, args.chunk_size)
retrievals = run_retrieval(index, questions, args.top_k)
summary = {
"questions_file": args.questions_file,
"vector_store_path": args.vector_store_path,
"embedding_model": args.embedding_model,
"top_k": args.top_k,
"retrieved_at": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
"questions": retrievals,
}
save_results(args.output_file, summary)
if __name__ == "__main__":
main()