File size: 3,373 Bytes
3c35d3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import re
import json
import os
from collections import defaultdict

def extract_data_from_log(log_content, extract_id=None):
    # 提取查询ID
    query_id_match = re.search(r'找到查询ID: (case_\w+_\d+)', log_content)
    query_id = query_id_match.group(1) if query_id_match else "unknown_id"
    
    # 尝试提取epoch信息 (如果存在)
    epoch_match = re.search(r'epoch[_-](\d+)', log_content, re.IGNORECASE)
    epoch = epoch_match.group(1) if epoch_match else None
    
    # 提取所有回复
    responses = []
    
    # 用正则表达式匹配每个模型回复块
    response_blocks = re.findall(
        r'模型回复 #(\d+) \(评分: ([\d.]+)\):\s*-+\s*<reasoning>(.*?)</reasoning>\s*<answer>(.*?)</answer>\s*-+\s*参考答案:\s*-+\s*(.*?)\s*=+',
        log_content, re.DOTALL
    )
    
    for idx, score, reasoning, answer, reference in response_blocks:
        # 将reasoning和answer合并为completion
        completion = f"<reasoning>{reasoning.strip()}</reasoning>\n<answer>{answer.strip()}</answer>"
        
        responses.append({
            "response_id": int(idx),
            "score": float(score),
            "completion": completion,
            "reference": reference.strip()
        })
    
    result = {
        "query_id": query_id,
        "responses": responses
    }
    
    # 添加提取ID和epoch信息(如果有)
    if extract_id:
        result["extract_id"] = extract_id
    if epoch:
        result["epoch"] = int(epoch)
        
    return result

def process_log_files(log_dir, output_file):
    all_data = []
    extract_counter = 1  # 用于生成唯一的extract_id
    
    # 如果输入是单个文件而不是目录
    if os.path.isfile(log_dir):
        with open(log_dir, 'r', encoding='utf-8') as f:
            log_content = f.read()
        
        # 生成唯一ID: 文件名_1
        base_filename = os.path.basename(log_dir).split('.')[0]
        extract_id = f"{base_filename}_{extract_counter}"
        
        data = extract_data_from_log(log_content, extract_id)
        all_data.append(data)
    else:
        # 处理目录中的所有文本文件
        for filename in sorted(os.listdir(log_dir)):
            if filename.endswith('.txt') or filename.endswith('.log'):
                file_path = os.path.join(log_dir, filename)
                
                with open(file_path, 'r', encoding='utf-8') as f:
                    log_content = f.read()
                
                # 生成唯一ID: 文件名_计数器
                base_filename = filename.split('.')[0]
                extract_id = f"{base_filename}_{extract_counter}"
                extract_counter += 1
                
                data = extract_data_from_log(log_content, extract_id)
                all_data.append(data)
    
    # 写入JSON文件
    with open(output_file, 'w', encoding='utf-8') as f:
        json.dump(all_data, f, ensure_ascii=False, indent=2)
    
    print(f"已成功提取 {len(all_data)} 个问题的回复,并保存到 {output_file}")

# 使用示例
if __name__ == "__main__":
    import sys
    
    if len(sys.argv) < 3:
        print("用法: python extract_responses.py <日志文件或目录> <输出的JSON文件>")
        sys.exit(1)
    
    log_path = sys.argv[1]
    output_file = sys.argv[2]
    
    process_log_files(log_path, output_file)