-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript_query.py
More file actions
158 lines (115 loc) · 4.22 KB
/
script_query.py
File metadata and controls
158 lines (115 loc) · 4.22 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
import json
import argparse
import os
import asyncio
# from dataclasses import is_dataclass, asdict
from datetime import datetime
import gc
import pathlib
from PRoH import PRoH, QueryParam
os.environ["OPENAI_API_KEY"] = open("openai_api_key.txt").read().strip()
# CLI
parser = argparse.ArgumentParser()
parser.add_argument('--data_source', default='example')
parser.add_argument('--part', default='')
# parser.add_argument('--qp', default='', help='Path to JSON with QueryParam overrides')
parser.add_argument('--ts', default='', help='Run timestamp decided by input')
args = parser.parse_args()
data_source = args.data_source
part = args.part.strip()
part_tag = part if part else "orig"
MAX_CONCURRENCY = 32
MAX_RETRYS = 3
WORKING_DIR = os.path.abspath(f"./data/work/{data_source}")
if not os.path.exists(WORKING_DIR):
exit(f"Error: working dir not exists: {WORKING_DIR}")
QUESTION_DIR = f"questions/{data_source}/questions.json"
if part:
QUESTION_DIR = f"questions/{data_source}/questions_{part}.json"
RUN_ROOT = f"results/{data_source}" if not part else f"results/{data_source}_{part}"
# RUN_TS / RUN_DIR
RUN_TS = args.ts.strip() or datetime.now().strftime("%Y%m%d-%H%M%S")
RUN_DIR = os.path.join(RUN_ROOT, RUN_TS)
os.makedirs(RUN_DIR, exist_ok=True)
# File paths inside the run dir
LOG_FILE = os.path.join(RUN_DIR, "test.log")
ANSWER_SAVE_PATH = os.path.join(RUN_DIR, "answers.jsonl")
FINAL_JSON_PATH = os.path.join(RUN_DIR, "generated_answer.json")
# # Registry / params snapshot
# REGISTRY_PATH = os.path.join(RUN_ROOT, "run_registry.jsonl")
# PARAMS_SNAPSHOT_PATH = os.path.join(RUN_DIR, "qp_used.json")
# RAG instance (single-process async like original code 1) -
rag = PRoH(
working_dir=WORKING_DIR,
log_level="INFO",
log_file=LOG_FILE,
llm_model_max_async=MAX_CONCURRENCY,
max_concurrency=MAX_CONCURRENCY
)
async def query_with_semaphore(sem: asyncio.Semaphore, q_data: dict):
async with sem:
qd = dict(q_data)
# if "answer" in qd:
# qd["golden_answers"] = [qd.pop("answer")]
q = qd["question"]
result = {
"gen_answer": "",
"generation": "",
"retrieved": [],
"dag": "",
"timer": None
}
last_err = None
for _ in range(MAX_RETRYS):
try:
response = await rag.aquery_reasoning(q)
if response:
result.update(response)
last_err = None
break
except Exception as e:
last_err = repr(e)
if last_err is not None:
result["error"] = last_err
# prune heavy context in provided reasoning paths
rp = qd.get("reasoning_paths")
if isinstance(rp, list):
for path in rp:
if isinstance(path, dict):
path.pop("context", None)
qd.update(result)
# console echo
print(f"Question: {q}")
print(f"Answer: {qd.get('gen_answer', '')}")
print(f"Timer: {qd.get('timer', None)}")
return qd
async def amain():
# Load questions
with open(QUESTION_DIR, "r", encoding="utf-8") as f:
data = json.load(f)
# fresh stream file
open(ANSWER_SAVE_PATH, "w", encoding="utf-8").close()
sem = asyncio.Semaphore(MAX_CONCURRENCY)
write_lock = asyncio.Lock()
tasks = [asyncio.create_task(query_with_semaphore(sem, d)) for d in data]
results_all = []
for fut in asyncio.as_completed(tasks):
updated = await fut
results_all.append(updated)
# incremental append
async with write_lock:
with open(ANSWER_SAVE_PATH, "a", encoding="utf-8") as f:
f.write(json.dumps(updated, ensure_ascii=False) + "\n")
# final consolidated snapshot
with open(FINAL_JSON_PATH, "w", encoding="utf-8") as f:
json.dump(results_all, f, indent=4, ensure_ascii=False)
print(f"Run dir -> {RUN_DIR}")
print(f"Log -> {LOG_FILE}")
print(f"Out -> {ANSWER_SAVE_PATH}")
print(f"Final -> {FINAL_JSON_PATH}")
# print(f"Registry-> {REGISTRY_PATH}")
def main():
asyncio.run(amain())
if __name__ == "__main__":
main()
pathlib.Path(RUN_DIR, ".READY").touch()