-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathrun_flash_searcher.py
More file actions
137 lines (112 loc) · 4.47 KB
/
run_flash_searcher.py
File metadata and controls
137 lines (112 loc) · 4.47 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
#!/usr/bin/env python
# coding=utf-8
# Copyright 2025 The OPPO Inc. PersonalAI team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import random
import argparse
import json
import logging
from tqdm import tqdm
import threading
from dotenv import load_dotenv
from concurrent.futures import ThreadPoolExecutor, as_completed
from FlashOAgents import OpenAIServerModel
from base_agent import SearchAgent
from utils import read_jsonl, write_jsonl
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.StreamHandler()
]
)
logger = logging.getLogger(__name__)
load_dotenv(override=True)
def process_item(item, model, summary_interval, prompts_type, max_steps):
search_agent = SearchAgent(
model,
summary_interval=summary_interval,
prompts_type=prompts_type,
max_steps=max_steps
)
question = item["question"]
golden_answer = item["answer"]
try:
result = search_agent(question)
except Exception as e:
logger.error(f"Exception occurred while calling multi_agent: {str(e)}")
return None
return {
"question": question,
"golden_answer": golden_answer,
**result,
}
def main(args):
custom_role_conversions = {"tool-call": "assistant", "tool-response": "user"}
model = OpenAIServerModel(
os.environ.get("DEFAULT_MODEL"),
custom_role_conversions=custom_role_conversions,
max_completion_tokens=32768,
api_key=os.environ.get("OPENAI_API_KEY"),
api_base=os.environ.get("OPENAI_API_BASE"),
)
if args.infile.lower().endswith('.json'):
with open(args.infile, 'r') as f:
data = json.load(f)
else:
data = read_jsonl(args.infile)
if args.sample_num is not None:
data = data[:args.sample_num]
try:
out_data = read_jsonl(args.outfile)
except Exception:
out_data = []
done_questions = set([item.get("question") for item in out_data])
data_to_run = [item for item in data if item.get("question") not in done_questions]
logger.info(f"Total data: {len(data)}, Completed: {len(done_questions)}, Remaining: {len(data_to_run)}")
results = []
file_lock = threading.Lock()
def safe_write(result):
with file_lock:
write_jsonl(args.outfile, [result], "a")
with ThreadPoolExecutor(max_workers=args.concurrency) as executor:
summary_interval = random.randint(args.summary_interval - 1, args.summary_interval + 1)
futures = [
executor.submit(
process_item,
item,
model,
summary_interval,
args.prompts_type,
args.max_steps
) for item in data_to_run
]
for future in tqdm(as_completed(futures), total=len(futures), desc="Processing"):
result = future.result()
if result:
results.append(result)
safe_write(result)
logger.info(f"Processing completed. Newly added: {len(results)}, Total completed: {len(done_questions) + len(results)}")
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Data generation script')
parser.add_argument('--infile', type=str, default="./data/<example.json>", help='input path')
parser.add_argument('--outfile', type=str, default="./output/<example.jsonl>", help='output path')
parser.add_argument('--sample_num', type=int, default=None, help='Number of samples to process')
parser.add_argument('--summary_interval', type=int, default=8, help='Summary interval')
parser.add_argument('--prompts_type', type=str, default="default", help='Type of prompts to use')
parser.add_argument('--concurrency', type=int, default=15, help='Number of concurrency')
parser.add_argument('--max_steps', type=int, default=40, help='Maximum number of steps')
args = parser.parse_args()
main(args)