-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinference.py
More file actions
384 lines (327 loc) · 13.2 KB
/
inference.py
File metadata and controls
384 lines (327 loc) · 13.2 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
from __future__ import annotations
import argparse, json, orjson, os, pickle, sys, re, time, random
from pathlib import Path
from typing import List, Dict, Tuple, Any
import openai
import numpy as np
import torch
from tqdm.auto import tqdm
from transformers import AutoModelForCausalLM, AutoTokenizer
import gc
import traceback
def load_docs(path: str) -> Tuple[List[int], List[str], Dict[int, List[int]]]:
doc_ids, texts, link_dict = [], [], {}
with open(path, "r", encoding="utf-8") as f:
for line in f:
j = orjson.loads(line)
doc_id = j["doc_id"]
semantic_id = j.get("semantic_id", "").strip()
body = j.get("chapter_body") or j.get("text") or ""
if semantic_id:
text = f"법령: {semantic_id}\n{body}"
else:
text = body
doc_ids.append(doc_id)
texts.append(text)
link_dict[doc_id] = j.get("matched_doc_id_merged") or []
return doc_ids, texts, link_dict
def load_queries(path: str) -> List[Dict]:
lst = []
with open(path, "r", encoding="utf-8") as f:
for line in f:
j = orjson.loads(line)
if j.get("has_matched_docs"):
lst.append(j)
return lst
def build_retriever(kind: str, docs: List[str], cache_dir: Path):
if kind == "tfidf":
from sklearn.feature_extraction.text import TfidfVectorizer
vect = TfidfVectorizer(
ngram_range=(1, 2),
max_df=0.95,
min_df=2,
max_features=120_000,
strip_accents="unicode",
sublinear_tf=True,
)
tfidf = vect.fit_transform(docs)
return {"vect": vect, "tfidf": tfidf}
elif kind == "bm25":
from rank_bm25 import BM25Okapi
tokenized = [d.replace("\n", " ").split() for d in docs]
bm25 = BM25Okapi(tokenized)
return {"bm25": bm25, "tokenized": tokenized}
elif kind == "bge":
pkl = cache_dir / "bge_index.pkl"
if pkl.exists():
print("· BGE 인덱스 로드:", pkl)
with open(pkl, "rb") as f:
return pickle.load(f)
from sentence_transformers import SentenceTransformer
import faiss
model = SentenceTransformer("BAAI/bge-m3", device="cuda" if torch.cuda.is_available() else "cpu")
model.max_seq_length = 512
all_embs = model.encode(
docs,
batch_size=256,
show_progress_bar=True,
convert_to_numpy=True,
normalize_embeddings=True,
).astype("float32")
index = faiss.IndexFlatIP(all_embs.shape[1])
index.add(all_embs)
obj = {"index": index, "model": model}
with open(pkl, "wb") as f:
pickle.dump(obj, f)
return obj
else:
raise ValueError(f"Unknown retriever: {kind}")
def search(retriever: Dict, kind: str, query: str, topk: int) -> Tuple[np.ndarray, np.ndarray]:
if kind == "tfidf":
q_vec = retriever["vect"].transform([query])
row = (q_vec @ retriever["tfidf"].T).toarray().ravel()
idx = np.argpartition(-row, range(topk))[:topk]
idx = idx[np.argsort(-row[idx])]
return idx, row[idx]
elif kind == "bm25":
q_tok = query.split()
scores = retriever["bm25"].get_scores(q_tok)
idx = np.argpartition(-scores, range(topk))[:topk]
idx = idx[np.argsort(-scores[idx])]
return idx, scores[idx]
elif kind == "bge":
import faiss
q_emb = retriever["model"].encode([query], normalize_embeddings=True, convert_to_numpy=True)
scores, idx = retriever["index"].search(q_emb, topk)
return idx.flatten(), scores.flatten()
else:
raise ValueError
def expand_with_links(
retrieved_ids: List[int],
link_dict: Dict[int, List[int]],
) -> List[int]:
seen = set(retrieved_ids)
expanded = list(retrieved_ids)
for did in retrieved_ids:
for linked in link_dict.get(did, []):
if linked not in seen:
expanded.append(linked)
seen.add(linked)
return expanded
def load_llm(model_name: str, bf16: bool = True):
if is_openai_model(model_name):
return None, None
print(f"· Loading model: {model_name}")
dtype = torch.bfloat16 if bf16 else torch.float16
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype=dtype,
device_map="auto",
trust_remote_code=True,
)
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
return model, tokenizer
def safe_run_generation(model, tokenizer, messages,
max_new_tokens=32768,
placeholder="[OOM‑SKIPPED]") -> str:
try:
return run_generation(model, tokenizer, messages, max_new_tokens)
except (torch.cuda.OutOfMemoryError, RuntimeError) as e:
if isinstance(e, torch.cuda.OutOfMemoryError) or "out of memory" in str(e).lower():
print("⚠️ OOM detected ‑ skipping this query", file=sys.stderr)
torch.cuda.empty_cache()
torch.cuda.ipc_collect()
gc.collect()
return placeholder
raise
def build_prompt(
model_name: str,
user_q: str,
context: str,
include_rag_instr: bool = True, # NEW
) -> List[Dict]:
if "hyperclovax" in model_name.lower():
sys_msg = "- 당신은 네이버가 만든 \"CLOVA X\" 라는 AI 언어모델입니다.\n-"
elif "qwen" in model_name.lower():
sys_msg = "You are Qwen3, a helpful Korean legal assistant."
elif "gpt" in model_name.lower():
sys_msg = "You are Chat-GPT from OpenAI, a helpful Korean legal assistant."
else:
sys_msg = "You are EXAONE model from LG AI Research, a helpful Korean legal assistant."
rag_instr = (
"다음은 사용자의 질문과 관련 문서입니다. "
"문서를 충분히 활용해 정확하고 간결하게 한국어로 답변하십시오."
)
messages = [{"role": "system", "content": sys_msg}]
if include_rag_instr and context:
ctx_block = "-----\n" + context.strip() + "\n-----"
messages.append({"role": "system", "content": rag_instr})
messages.append({"role": "user", "content": f"{user_q}\n\n{ctx_block}"})
else:
messages.append({"role": "user", "content": user_q})
return messages
def run_generation(model, tokenizer, messages, max_new_tokens=32768) -> str:
input_ids = tokenizer.apply_chat_template(
messages,
add_generation_prompt=True,
tokenize=True,
return_tensors="pt"
).to(model.device)
with torch.no_grad():
output = model.generate(
input_ids,
max_new_tokens=max_new_tokens,
eos_token_id=tokenizer.eos_token_id,
do_sample=False,
)
out_text = tokenizer.decode(output[0], skip_special_tokens=True)
m = re.search(r"<\|assistant\|>(.*)", out_text, re.S)
return m.group(1).strip() if m else out_text.strip()
def is_openai_model(name: str) -> bool:
return name.lower().startswith("openai:")
def ensure_openai_key(key_file: str):
key = None
if key_file and Path(key_file).exists():
key = Path(key_file).read_text().strip()
if not key:
key = os.getenv("OPENAI_API_KEY")
if not key:
raise RuntimeError("❌ Cannot find OpenAI API. Please check ‑‑openai_key_file")
openai.api_key = key
def safe_openai_generation(
model_name: str,
messages: List[Dict],
max_new_tokens: int = 2048,
min_backoff: float = 1.0,
max_backoff: float = 60.0,
) -> str:
_retryable_names = [
"RateLimitError",
"APIConnectionError",
"InternalServerError",
"APITimeoutError",
"Timeout",
"APIError",
]
RETRYABLE_EXC: tuple[type[BaseException], ...] = tuple(
exc
for name in _retryable_names
if (exc := getattr(openai, name, None))
if isinstance(exc, type) and issubclass(exc, BaseException)
)
NON_RETRYABLE = (
getattr(openai, "AuthenticationError", BaseException),
getattr(openai, "PermissionDeniedError", BaseException),
getattr(openai, "BadRequestError", BaseException),
getattr(openai, "InvalidRequestError", BaseException),
getattr(openai, "NotFoundError", BaseException),
getattr(openai, "UnprocessableEntityError", BaseException),
)
backoff = min_backoff
attempt = 1
while True:
try:
resp = openai.chat.completions.create(
model=model_name,
messages=messages,
temperature=0.0,
max_tokens=max_new_tokens,
)
return resp.choices[0].message.content.strip()
except RETRYABLE_EXC as e:
wait = random.uniform(backoff * 0.8, backoff * 1.2) # jitter
print(
f"⚠️ {type(e).__name__} ‑ retry #{attempt} after {wait:.1f}s",
file=sys.stderr,
)
time.sleep(wait)
backoff = min(backoff * 2, max_backoff)
attempt += 1
except NON_RETRYABLE as e:
raise RuntimeError(f"❌ OpenAI call failed: {e}") from e
except Exception:
raise
def main(args):
out_dir = Path(args.out_dir)
out_dir.mkdir(exist_ok=True, parents=True)
print("1) Loading Documents...")
doc_ids, docs, link_dict = load_docs(args.docs)
print(f" · {len(docs):,} docs")
print("2) Loading Query...")
queries = load_queries(args.queries)
print(f" · {len(queries):,} queries (has_matched_docs==True)")
print("3) Building Index...")
retriever = build_retriever(args.retriever, docs, out_dir)
id2text = {d: t for d, t in zip(doc_ids, docs)}
if any(is_openai_model(m) for m in args.models):
if openai is None:
raise RuntimeError("openai package not installed. `pip install openai>=1.0`")
ensure_openai_key(args.openai_key_file)
for model_name in args.models:
use_openai = is_openai_model(model_name)
if use_openai:
base = model_name.split(":", 1)[1]
model, tokenizer = None, None
else:
model, tokenizer = load_llm(model_name)
base = model_name.split("/")[-1]
filename = f"{base}_{args.retriever}_k{args.topk}"
if args.oracle:
filename += "_oracle"
filename += ".jsonl"
outfile = out_dir / filename
with open(outfile, "w", encoding="utf-8") as fw, \
tqdm(queries, desc=f"Inference [{model_name}]") as pbar:
for j in pbar:
qtext = j["question"]
if args.oracle:
init_ids = j.get("matched_doc_id", [])
elif args.topk == 0:
init_ids = []
else:
idx, _ = search(retriever, args.retriever, qtext, args.topk)
init_ids = [doc_ids[i] for i in idx]
if args.expand_with_links and init_ids:
final_ids = expand_with_links(init_ids, link_dict)
else:
final_ids = init_ids
ctx_docs = "\n\n".join(
id2text[did] for did in final_ids if did in id2text
)
include_rag = args.topk != 0
prompt_msgs = build_prompt(
model_name, qtext, ctx_docs, include_rag_instr=include_rag
)
if use_openai:
answer = safe_openai_generation(
base, prompt_msgs,
max_new_tokens=args.max_new_tokens
)
else:
answer = safe_run_generation(
model, tokenizer, prompt_msgs,
max_new_tokens=args.max_new_tokens
)
new_obj = dict(j)
new_obj["model_answer"] = answer
fw.write(orjson.dumps(
new_obj, option=orjson.OPT_APPEND_NEWLINE
).decode("utf-8"))
print("· Saved:", outfile)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--docs", required=True)
parser.add_argument("--queries", required=True)
parser.add_argument("--out_dir", default="rag_outputs")
parser.add_argument("--retriever", choices=["tfidf", "bm25", "bge"],
default="bge")
parser.add_argument("--topk", type=int, default=5)
parser.add_argument("--models", nargs="+", required=True)
parser.add_argument("--max_new_tokens", type=int, default=16384)
parser.add_argument("--expand_with_links", action="store_true")
parser.add_argument(
"--oracle", action="store_true")
parser.add_argument(
"--openai_key_file", default="openai_api_key.txt")
args = parser.parse_args()
main(args)