| import sys |
| import os |
| import json |
| import argparse |
| from datasets import load_dataset |
| from prompt_pool import DATASET_PROMPTS, SEQUENCE_PROMPT |
|
|
|
|
|
|
| def self_critique_prompt(question:str, response:str): |
| prompt = SEQUENCE_PROMPT["self_critique_new"] |
| prompt = prompt.format(input_data=question, previous_solution=response) |
| return prompt |
|
|
| def concate_prompt(question:str, response:list[str]): |
| for i, r in enumerate(response): |
| response[i] = f"Solution {i+1}: {r}" |
| response = "\n".join(response) |
| prompt = SEQUENCE_PROMPT["concate"] |
| prompt = prompt.format(input_data=question, previous_solution=response) |
| return prompt |
|
|
|
|
| def prepare_sequence_jsonl(input_path, output_dir, budget=8, model_name="Qwen/Qwen2.5-7B-Instruct", max_completion_tokens=2048): |
| |
| |
| |
| |
| |
| |
| |
| if not os.path.exists(output_dir): |
| os.makedirs(output_dir) |
| |
| batchs= [[] for _ in range(budget)] |
| with open(input_path, "r", encoding="utf-8") as f: |
| for idx, line in enumerate(f): |
| line = json.loads(line) |
| if len(line["outputs"]) < budget: |
| raise ValueError(f"Warning: {line['id']} has less than {budget} outputs") |
| for i in range(budget): |
| item = { |
| "custom_id": f"request-{idx+1}", |
| "method": "POST", |
| "url": "/v1/chat/completions", |
| "body": { |
| "model": model_name, |
| "messages": [ |
| {"role": "system", "content": "You are a helpful assistant."}, |
| {"role": "user", "content": self_critique_prompt(line["problem"], line["outputs"][i])} |
| ], |
| "max_completion_tokens": max_completion_tokens, |
| "temperature": 0.7, |
| } |
| } |
| batchs[i].append(item) |
|
|
| for i in range(budget): |
| with open(os.path.join(output_dir, f"batch_{i}.jsonl"), "w", encoding="utf-8") as f: |
| for line in batchs[i]: |
| f.write(json.dumps(line, ensure_ascii=False) + '\n') |
|
|
| def main(): |
| parser = argparse.ArgumentParser() |
| parser.add_argument("--input_path", type=str, required=True) |
| parser.add_argument("--output_dir", type=str, required=True) |
| parser.add_argument("--budget", type=int, default=8) |
| parser.add_argument("--model_name", type=str, default="Qwen/Qwen2.5-7B-Instruct") |
| parser.add_argument("--max_completion_tokens", type=int, default=2048) |
| args = parser.parse_args() |
| prepare_sequence_jsonl(args.input_path, args.output_dir, args.budget, args.model_name, args.max_completion_tokens) |
|
|
| if __name__ == "__main__": |
| main() |