-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoutput_prediction.py
More file actions
1074 lines (897 loc) · 51.3 KB
/
output_prediction.py
File metadata and controls
1074 lines (897 loc) · 51.3 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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# import json
# import os
# from pathlib import Path
# import time
# import datetime
# import tiktoken
# from openai import OpenAI, APIError, APITimeoutError, APIConnectionError, RateLimitError
# import random
# # ==============================================================================
# # 实验配置
# # ==============================================================================
# # --- API 和模型配置 ---
# CUSTOM_API_KEY = "sk-Eyhl41vT8nBB8xLY77D7C9Bf0198453dA2797221Aa75E486"
# CUSTOM_API_BASE_URL = "https://api.vveai.com/v1/"
# OPENAI_COMPATIBLE_MODEL_NAME = "gpt-4.1-mini"
# TOKENIZER_NAME = "o200k_base"
# # --- 核心目录配置 ---
# BASE_DATA_DIR = Path("data_collection_align")
# BM25_RANK_DIR = Path("output_with_bm25_rank")
# REPO_BASE_DIR = Path("python_repos")
# EXPERIMENTS_OUTPUT_DIR = Path("experiments_output_gpt-4.1-mini_30k")
# # --- 实验参数 ---
# MODEL_CONTEXT_WINDOW = 30720
# MAX_ITEMS_PER_REPO = 200
# NUM_SAMPLES_PER_TASK = 5
# MAX_RELATED_FILES_TO_CONSIDER_RETRIEVAL = 20
# NUM_CONFUSION_FILES_TO_SAMPLE = 20
# TOP_N_TO_EXCLUDE_FOR_CONFUSION = 20
# TEMPERATURE = 0.7
# TOP_P = 0.9
# RETRY_DELAY_SECONDS = 5
# # --- 派生常量 ---
# MAX_TOKENS_FOR_PROMPT_CONSTRUCTION = MODEL_CONTEXT_WINDOW - 100
# SAFETY_MARGIN_TOKENS_PROMPT_BUILDING = 50
# # ==============================================================================
# # Prompt模板
# # ==============================================================================
# PROMPT_TEMPLATE_PARTS = {
# "system_message": """Your task is to infer the correct values for the '???' placeholder based on provided Python code.
# # Rules
# 1. Analyze the provided code and related files to determine the value.
# 2. The value MUST be a determined value or a valid Python expression.
# 3. **Do not** output any explanations, reasoning, introductions, or any other text outside of the specified output format. Your output must be clean.
# # Output format
# ```output
# answer_of_placeholder
# ```
# # Example
# Here is an example of a perfect interaction.
# ## Related files are as follow:
# ### file: config.py
# ```python
# DEFAULT_TIMEOUT = 50
# ```
# ### file: get_timeout.py
# ```python
# from config import DEFAULT_TIMEOUT
# def get_timeout():
# return DEFAULT_TIMEOUT
# ```
# ## Test function is as follows:
# ### test
# ```python
# def test_timeout():
# from get_timeout import get_timeout
# assert get_timeout() == '???'
# ```
# ### Your Correct Output:
# ```output
# 50
# ```
# """,
# "user_prompt_template": """## Related files are as follows:
# {related_files_content}
# ## Test function is as follows:
# ```python
# {masked_code_content}
# ```
# Based on all the provided context and the test function, infer the correct value or expression for the "???" placeholder. Output it directly in the required format. Do not output extra information.
# """
# }
# # ==============================================================================
# # 初始化和辅助函数
# # ==============================================================================
# random.seed(42)
# try:
# tokenizer_encoding = tiktoken.get_encoding(TOKENIZER_NAME)
# except KeyError:
# print(f"错误: Tokenizer编码 '{TOKENIZER_NAME}' 未找到。"); exit(1)
# try:
# client = OpenAI(api_key=CUSTOM_API_KEY, base_url=CUSTOM_API_BASE_URL, timeout=60.0, max_retries=2)
# except Exception as e:
# print(f"错误: 初始化OpenAI客户端失败: {e}"); exit(1)
# def count_tokens_custom(text, encoding):
# return len(encoding.encode(text))
# def load_jsonl(file_path):
# data = []
# if not Path(file_path).exists():
# return data
# with open(file_path, 'r', encoding='utf-8') as f:
# for ln, line in enumerate(f):
# try: data.append(json.loads(line))
# except json.JSONDecodeError as e: print(f"警告: 在 {file_path} 第{ln+1}行JSON解码错误: {e}。行内容: '{line.strip()}'")
# return data
# def get_related_files_content_budgeted_custom(reponame_arg, related_files_rank_list, current_encoding, token_budget_for_related_files):
# content_parts, accumulated_tokens = [], 0
# if token_budget_for_related_files <= 0: return "# 没有为相关文件分配token预算。\n", 0
# for rel_path_str in related_files_rank_list:
# if accumulated_tokens >= token_budget_for_related_files: break
# file_path = REPO_BASE_DIR / reponame_arg / rel_path_str
# file_header_str = f"### file: {rel_path_str}\n```python\n"
# file_footer_str = "\n```\n"
# file_header_tokens = count_tokens_custom(file_header_str, current_encoding)
# file_footer_tokens = count_tokens_custom(file_footer_str, current_encoding)
# file_content_str, error_reading = "", False
# if file_path.exists() and file_path.is_file():
# try:
# with open(file_path, 'r', encoding='utf-8', errors='ignore') as f: file_content_str = f.read()
# except Exception as e: file_content_str, error_reading = f"# 读取文件错误 {file_path}: {e}", True
# else: file_content_str, error_reading = f"# 文件 {file_path} 未找到", True
# available_tokens_for_this_file = token_budget_for_related_files - accumulated_tokens
# if file_header_tokens + file_footer_tokens >= available_tokens_for_this_file: break
# current_file_str_parts = [file_header_str]
# current_file_tokens = file_header_tokens
# content_tokens_budget = available_tokens_for_this_file - file_header_tokens - file_footer_tokens
# if content_tokens_budget > 0:
# content_token_ids = current_encoding.encode(file_content_str)
# if len(content_token_ids) > content_tokens_budget:
# truncated_content_token_ids = content_token_ids[:content_tokens_budget]
# try: truncated_content_str = current_encoding.decode(truncated_content_token_ids)
# except: truncated_content_str = "[内容截断 - 解码错误]"
# current_file_str_parts.append(truncated_content_str)
# current_file_tokens += count_tokens_custom(truncated_content_str, current_encoding)
# else:
# current_file_str_parts.append(file_content_str)
# current_file_tokens += len(content_token_ids)
# current_file_str_parts.append(file_footer_str)
# current_file_tokens += file_footer_tokens
# if accumulated_tokens + current_file_tokens <= token_budget_for_related_files:
# content_parts.append("".join(current_file_str_parts))
# accumulated_tokens += current_file_tokens
# else:
# break
# if not content_parts:
# final_str = "# 没有相关文件内容可以在token限制内被包含。\n"
# return final_str, count_tokens_custom(final_str, current_encoding)
# return "".join(content_parts), accumulated_tokens
# # [新增] 新的辅助函数,用于清理和去重JSONL文件
# def deduplicate_and_rewrite_jsonl(file_path: Path):
# """
# 读取一个JSONL文件,根据 (task_id, sample_id) 去重,只保留最新的条目,
# 然后用去重后的内容重写该文件。
# """
# if not file_path.exists():
# return
# # 使用字典来存储每个任务样本的最新记录
# # key: (task_id, sample_id), value: 完整的JSON对象
# latest_records = {}
# with open(file_path, 'r', encoding='utf-8') as f:
# for line in f:
# try:
# data = json.loads(line)
# # 必须有 task_id 和 sample_id 才能进行去重
# if 'task_id' in data and 'sample_id' in data:
# key = (data['task_id'], data['sample_id'])
# latest_records[key] = data
# except json.JSONDecodeError:
# # 忽略格式不正确的行
# continue
# # 用去重后并保留最新的记录重写文件
# with open(file_path, 'w', encoding='utf-8') as f:
# for record in latest_records.values():
# f.write(json.dumps(record, ensure_ascii=False) + '\n')
# print(f" 已清理并去重文件: {file_path},保留了 {len(latest_records)} 条最新记录。")
# # ==============================================================================
# # 核心实验逻辑
# # ==============================================================================
# def process_repository(reponame, dataset_dir, base_predictions_dir, base_interactions_dir):
# print(f"\n--- 正在处理仓库: {reponame} ---")
# dataset_file = dataset_dir / f"{reponame}.jsonl"
# bm25_file = BM25_RANK_DIR / f"{reponame}.jsonl"
# if not dataset_file.exists(): print(f"数据集文件未找到: {dataset_file}。"); return
# if not bm25_file.exists(): print(f"BM25排名文件未找到: {bm25_file}。"); return
# test_data_list = load_jsonl(dataset_file)[:MAX_ITEMS_PER_REPO]
# print(f" 已加载 {len(test_data_list)} 个条目 (最大 {MAX_ITEMS_PER_REPO})。")
# bm25_lookup = {item['task_id']: item for item in load_jsonl(bm25_file)}
# tokens_system_message = count_tokens_custom(PROMPT_TEMPLATE_PARTS["system_message"], tokenizer_encoding)
# static_user_prompt_template = PROMPT_TEMPLATE_PARTS["user_prompt_template"].format(related_files_content="", masked_code_content="")
# tokens_static_user_prompt = count_tokens_custom(static_user_prompt_template, tokenizer_encoding)
# # for experiment_type in ["retrieval", "confusion"]:
# for experiment_type in ["retrieval"]:
# print(f" 实验模式: {experiment_type}")
# current_preds_dir = base_predictions_dir / experiment_type
# current_inters_dir = base_interactions_dir / experiment_type
# current_preds_dir.mkdir(parents=True, exist_ok=True)
# current_inters_dir.mkdir(parents=True, exist_ok=True)
# predictions_output_file = current_preds_dir / f"{reponame}.jsonl"
# interactions_output_file = current_inters_dir / f"{reponame}.jsonl"
# # [修改] 在开始处理前,先清理输出文件,移除旧的/失败的尝试
# deduplicate_and_rewrite_jsonl(predictions_output_file)
# deduplicate_and_rewrite_jsonl(interactions_output_file)
# # --- 断点续传逻辑 ---
# completed_tasks = set()
# # 现在的加载逻辑是基于清理过的文件,所以更准确
# existing_preds = load_jsonl(predictions_output_file)
# for pred in existing_preds:
# # 这里的逻辑保持不变,因为文件已经是干净的了
# if 'task_id' in pred and 'sample_id' in pred:
# if pred.get('finish_reason') not in ['api_error', 'sdk_error', 'init_fail']:
# completed_tasks.add((pred['task_id'], pred['sample_id']))
# print(f" 已加载 {len(completed_tasks)} 个已成功完成的样本。将跳过这些任务。")
# # 以追加模式打开文件,因为它们已经被清理过了
# with open(predictions_output_file, 'a', encoding='utf-8') as pred_f, \
# open(interactions_output_file, 'a', encoding='utf-8') as intr_f:
# for item_idx, test_case in enumerate(test_data_list):
# task_id = test_case['task_id']
# if task_id not in bm25_lookup:
# print(f" 警告: 任务ID {task_id} 没有BM25排名。跳过。")
# continue
# all_ranked_files = bm25_lookup[task_id].get('related_files_rank', [])
# related_files_for_experiment = []
# if experiment_type == "retrieval": related_files_for_experiment = all_ranked_files[:MAX_RELATED_FILES_TO_CONSIDER_RETRIEVAL]
# elif experiment_type == "confusion":
# candidates = all_ranked_files[TOP_N_TO_EXCLUDE_FOR_CONFUSION:]
# if len(candidates) >= NUM_CONFUSION_FILES_TO_SAMPLE: related_files_for_experiment = random.sample(candidates, NUM_CONFUSION_FILES_TO_SAMPLE)
# else: related_files_for_experiment = candidates
# random.shuffle(related_files_for_experiment)
# for sample_idx in range(NUM_SAMPLES_PER_TASK):
# task_key = (task_id, sample_idx)
# if task_key in completed_tasks:
# continue
# print(f" 处理任务ID: {task_id} ({item_idx+1}/{len(test_data_list)}), 样本 {sample_idx + 1}/{NUM_SAMPLES_PER_TASK}")
# masked_code = test_case['masked_code']
# tokens_test_code = count_tokens_custom(masked_code, tokenizer_encoding)
# token_budget_for_dynamic_content = (
# MAX_TOKENS_FOR_PROMPT_CONSTRUCTION - tokens_system_message -
# tokens_static_user_prompt - SAFETY_MARGIN_TOKENS_PROMPT_BUILDING
# )
# token_budget_for_related_files = token_budget_for_dynamic_content - tokens_test_code
# related_files_str, _ = get_related_files_content_budgeted_custom(
# reponame, related_files_for_experiment,
# tokenizer_encoding, token_budget_for_related_files
# ) if token_budget_for_related_files > 0 and related_files_for_experiment else ("# 无相关文件上下文。\n", 0)
# user_prompt = PROMPT_TEMPLATE_PARTS["user_prompt_template"].format(
# related_files_content=related_files_str,
# masked_code_content=masked_code
# )
# messages = [{"role": "system", "content": PROMPT_TEMPLATE_PARTS["system_message"]}, {"role": "user", "content": user_prompt}]
# response_text, finish_reason, usage, response_obj, error_details = \
# "ERROR: INIT", "init_fail", None, None, None
# while True:
# try:
# response = client.chat.completions.create(
# model=OPENAI_COMPATIBLE_MODEL_NAME,
# messages=messages,
# temperature=TEMPERATURE,
# top_p=TOP_P,
# )
# response_obj = response
# response_text = response.choices[0].message.content
# finish_reason = response.choices[0].finish_reason
# usage = response.usage.model_dump() if response.usage else None
# print(f" API调用成功。Finish reason: {finish_reason}")
# break
# except (APITimeoutError, APIConnectionError, RateLimitError) as e:
# print(f" API可重试错误 ({e.__class__.__name__})。将在 {RETRY_DELAY_SECONDS} 秒后重试...")
# time.sleep(RETRY_DELAY_SECONDS)
# except APIError as e:
# if hasattr(e, 'status_code') and e.status_code in [500, 502, 503, 504]:
# print(f" API服务器端错误 (可重试, status={e.status_code})。将在 {RETRY_DELAY_SECONDS} 秒后重试...")
# time.sleep(RETRY_DELAY_SECONDS)
# else:
# print(f" API错误 (不可重试): {e}")
# response_text = f"ERROR: APIError - {e.__class__.__name__}"
# finish_reason = "api_error"
# error_details = {"type": e.__class__.__name__, "message": str(e)}
# if hasattr(e, 'status_code'):
# error_details['status_code'] = e.status_code
# if hasattr(e, 'body'):
# error_details['raw_body'] = str(e.body) if e.body else None
# break
# except Exception as e:
# print(f" 意外错误 (不可重试): {e}")
# response_text = f"ERROR: SDK/Network Error - {e.__class__.__name__}"
# finish_reason = "sdk_error"
# error_details = {"type": e.__class__.__name__, "message": str(e)}
# break
# intr_f.write(json.dumps({
# "task_id": task_id, "experiment_type": experiment_type, "sample_id": sample_idx,
# "timestamp_utc": datetime.datetime.now(datetime.timezone.utc).isoformat(),
# "messages_sent": messages, "model_used": OPENAI_COMPATIBLE_MODEL_NAME,
# "related_files_provided_to_prompt": related_files_for_experiment,
# "api_response_object_str": str(response_obj), "api_error_details": error_details
# }, ensure_ascii=False) + "\n")
# pred_f.write(json.dumps({
# "task_id": task_id, "experiment_type": experiment_type, "sample_id": sample_idx,
# "response": response_text.strip() if isinstance(response_text, str) else str(response_text),
# "finish_reason": finish_reason, "usage": usage
# }, ensure_ascii=False) + "\n")
# pred_f.flush()
# intr_f.flush()
# time.sleep(0.3)
# print(f"--- 完成仓库: {reponame} ---")
# # ==============================================================================
# # 主函数
# # ==============================================================================
# def main():
# data_conditions = {"original": BASE_DATA_DIR / "original", "rewrite": BASE_DATA_DIR / "rewrite"}
# for condition_name, dataset_dir in data_conditions.items():
# print("\n" + "="*80)
# print(f"开始实验条件: '{condition_name.upper()}'")
# print(f"数据源: {dataset_dir}")
# print("="*80)
# if not dataset_dir.exists():
# print(f"错误: 数据目录 '{dataset_dir}' 不存在。跳过此实验条件。")
# continue
# base_predictions_dir = EXPERIMENTS_OUTPUT_DIR / condition_name / "predictions"
# base_interactions_dir = EXPERIMENTS_OUTPUT_DIR / condition_name / "interactions"
# base_predictions_dir.mkdir(parents=True, exist_ok=True)
# base_interactions_dir.mkdir(parents=True, exist_ok=True)
# repo_names = sorted([p.stem for p in dataset_dir.glob("*.jsonl")])
# if not repo_names:
# print(f"在 {dataset_dir} 中未找到.jsonl文件。")
# continue
# print(f"找到的仓库: {repo_names}")
# for reponame in repo_names:
# process_repository(reponame, dataset_dir, base_predictions_dir, base_interactions_dir)
# print("\n--- 所有实验条件均已完成 ---")
# if __name__ == "__main__":
# main()
# import json
# import os
# from pathlib import Path
# import time
# import datetime
# import random
# import re
# import argparse
# from tqdm import tqdm
# from openai import OpenAI, APIError
# import tiktoken
# # ==============================================================================
# # 实验配置
# # ==============================================================================
# class Config:
# # --- 动态API配置 (由命令行参数设置) ---
# API_BASE_URL = None
# API_KEY = None
# MODEL_NAME = None
# MODEL_CONTEXT_WINDOW = None
# # --- 静态目录配置 ---
# BASE_DATA_DIR = Path("data_collection_align")
# BM25_RANK_DIR = Path("output_with_bm25_rank")
# CALL_CHAIN_DATA_DIR = Path("output_results")
# REPO_BASE_DIR = Path("python_repos")
# EXPERIMENTS_OUTPUT_DIR = None
# # --- 实验参数 ---
# MAX_COMPLETION_TOKENS = 512
# MAX_ITEMS_PER_REPO = 200
# NUM_SAMPLES_PER_TASK = 5
# TEMPERATURE = 0.7
# TOP_P = 0.9
# REQUEST_DELAY_SECONDS = 1 # 避免API限速
# # --- Tokenizer ---
# TOKENIZER_NAME = "o200k_base"
# # --- 不同实验类型的文件选择参数 ---
# MAX_RELATED_FILES_TO_CONSIDER_RETRIEVAL = 20
# NUM_CONFUSION_FILES_TO_SAMPLE = 20
# TOP_N_TO_EXCLUDE_FOR_CONFUSION = 20
# MAX_ORACLE_FILES_TO_CONSIDER = 20
# # --- 派生常量 ---
# MAX_TOKENS_FOR_PROMPT_CONSTRUCTION = None
# SAFETY_MARGIN_TOKENS_PROMPT_BUILDING = 50
# PROMPT_TEMPLATE_PARTS = {
# "system_message": """Your task is to infer the correct values for the '???' placeholder based on provided Python code.
# # Rules
# 1. Analyze the provided code and related files to determine the value.
# 2. The value MUST be a determined value or a valid Python expression.
# 3. **Do not** output any explanations, reasoning, introductions, or any other text outside of the specified output format. Your output must be clean.
# # Output format
# ```output
# answer_of_placeholder
# ```
# # Example
# Here is an example of a perfect interaction.
# ## Related files are as follow:
# ### file: config.py
# ```python
# DEFAULT_TIMEOUT = 50
# ```
# ### file: get_timeout.py
# ```python
# from config import DEFAULT_TIMEOUT
# def get_timeout():
# return DEFAULT_TIMEOUT
# ```
# ## Test function is as follows:
# ### test
# ```python
# def test_timeout():
# from get_timeout import get_timeout
# assert get_timeout() == '???'
# ```
# ### Your Correct Output:
# ```output
# 50
# ```
# """,
# "user_prompt_template": """## Related files are as follows:
# {related_files_content}
# ## Test function is as follows:
# ```python
# {masked_code_content}
# ```
# Based on all the provided context and the test function, infer the correct value or expression for the "???" placeholder. Output it directly in the required format. Do not output extra information.
# """
# }
# # ==============================================================================
# # 辅助函数
# # ==============================================================================
# def count_tokens_custom(text, tok):
# return len(tok.encode(text))
# def load_jsonl(file_path):
# data = []
# if not file_path.exists(): return data
# with open(file_path, 'r', encoding='utf-8') as f:
# for ln, line in enumerate(f):
# try:
# data.append(json.loads(line))
# except json.JSONDecodeError:
# pass # Silently skip malformed lines
# return data
# def get_related_files_content_budgeted_custom(reponame_arg, related_files_rank_list, current_tokenizer, token_budget_for_related_files):
# content_parts, accumulated_tokens = [], 0
# if token_budget_for_related_files <= 0: return "# 没有为相关文件分配token预算。\n", 0
# for rel_path_str in related_files_rank_list:
# if accumulated_tokens >= token_budget_for_related_files: break
# file_path = Config.REPO_BASE_DIR / reponame_arg / rel_path_str
# file_header_str = f"### file: {rel_path_str}\n```python\n"
# file_footer_str = "\n```\n"
# file_header_tokens = count_tokens_custom(file_header_str, current_tokenizer)
# file_footer_tokens = count_tokens_custom(file_footer_str, current_tokenizer)
# file_content_str = ""
# try:
# if file_path.exists() and file_path.is_file():
# with open(file_path, 'r', encoding='utf-8', errors='ignore') as f: file_content_str = f.read()
# else: file_content_str = f"# 文件 {file_path} 未找到"
# except Exception as e: file_content_str = f"# 读取文件错误 {file_path}: {e}"
# available_tokens_for_this_file = token_budget_for_related_files - accumulated_tokens
# if file_header_tokens + file_footer_tokens >= available_tokens_for_this_file: break
# current_file_str_parts = [file_header_str]
# content_tokens_budget = available_tokens_for_this_file - file_header_tokens - file_footer_tokens
# if content_tokens_budget > 0:
# content_token_ids = current_tokenizer.encode(file_content_str)
# if len(content_token_ids) > content_tokens_budget:
# truncated_content_token_ids = content_token_ids[:content_tokens_budget]
# truncated_content_str = current_tokenizer.decode(truncated_content_token_ids)
# current_file_str_parts.append(truncated_content_str)
# else:
# current_file_str_parts.append(file_content_str)
# current_file_str_parts.append(file_footer_str)
# full_part = "".join(current_file_str_parts)
# part_tokens = count_tokens_custom(full_part, current_tokenizer)
# if accumulated_tokens + part_tokens <= token_budget_for_related_files:
# content_parts.append(full_part)
# accumulated_tokens += part_tokens
# else: break
# if not content_parts:
# return "# 没有相关文件内容可以在token限制内被包含。\n", 0
# return "".join(content_parts), accumulated_tokens
# def normalize_funcname(name: str) -> str:
# name = re.sub(r'\[.*\]$', '', name)
# if '::' in name:
# name = name.split('::')[-1]
# return name
# # ==============================================================================
# # 核心实验逻辑
# # ==============================================================================
# def process_repository(reponame, dataset_dir, condition_name, client, tokenizer):
# dataset_file = dataset_dir / f"{reponame}.jsonl"
# bm25_file = Config.BM25_RANK_DIR / f"{reponame}.jsonl"
# call_chain_file = Config.CALL_CHAIN_DATA_DIR / reponame / "report_functions.jsonl"
# if not dataset_file.exists(): return
# test_data_list = load_jsonl(dataset_file)[:Config.MAX_ITEMS_PER_REPO]
# bm25_lookup = {item['task_id']: item for item in load_jsonl(bm25_file)}
# call_chain_data = load_jsonl(call_chain_file)
# call_chain_lookup = {}
# for item in call_chain_data:
# dirty_func_name = item.get('test_function')
# if not dirty_func_name: continue
# normalized_name = normalize_funcname(dirty_func_name)
# sorted_deps = sorted(item.get('dependencies', []), key=lambda x: x.get('hops', float('inf')))
# call_chain_lookup[normalized_name] = [dep['file'] for dep in sorted_deps]
# tokens_system_message = count_tokens_custom(Config.PROMPT_TEMPLATE_PARTS["system_message"], tokenizer)
# static_user_prompt_template = Config.PROMPT_TEMPLATE_PARTS["user_prompt_template"].format(related_files_content="", masked_code_content="")
# tokens_static_user_prompt = count_tokens_custom(static_user_prompt_template, tokenizer)
# # for experiment_type in ["retrieval", "confusion", "oracle"]:
# for experiment_type in ["oracle"]:
# print(f" -> 开始实验: {experiment_type}")
# current_preds_dir = Config.EXPERIMENTS_OUTPUT_DIR / condition_name / "predictions" / experiment_type
# current_inters_dir = Config.EXPERIMENTS_OUTPUT_DIR / condition_name / "interactions" / experiment_type
# current_preds_dir.mkdir(parents=True, exist_ok=True)
# current_inters_dir.mkdir(parents=True, exist_ok=True)
# predictions_output_file = current_preds_dir / f"{reponame}.jsonl"
# interactions_output_file = current_inters_dir / f"{reponame}.jsonl"
# with open(predictions_output_file, 'w', encoding='utf-8') as pred_f, \
# open(interactions_output_file, 'w', encoding='utf-8') as intr_f:
# tasks_to_process = []
# for test_case in test_data_list:
# task_id = test_case['task_id']
# related_files_for_experiment = []
# if experiment_type == "retrieval":
# if task_id in bm25_lookup:
# files = bm25_lookup[task_id].get('related_files_rank', [])
# related_files_for_experiment = files[:Config.MAX_RELATED_FILES_TO_CONSIDER_RETRIEVAL]
# random.shuffle(related_files_for_experiment)
# elif experiment_type == "confusion":
# if task_id in bm25_lookup:
# files = bm25_lookup[task_id].get('related_files_rank', [])
# pool = files[Config.TOP_N_TO_EXCLUDE_FOR_CONFUSION:]
# related_files_for_experiment = random.sample(pool, min(len(pool), Config.NUM_CONFUSION_FILES_TO_SAMPLE))
# elif experiment_type == "oracle":
# func_name = test_case.get('funcname')
# if func_name and normalize_funcname(func_name) in call_chain_lookup:
# files = call_chain_lookup[normalize_funcname(func_name)]
# related_files_for_experiment = files[:Config.MAX_ORACLE_FILES_TO_CONSIDER]
# for sample_idx in range(Config.NUM_SAMPLES_PER_TASK):
# tasks_to_process.append({"test_case": test_case, "related_files": related_files_for_experiment, "sample_idx": sample_idx})
# task_iterator = tqdm(tasks_to_process, desc=f" Tasks", leave=False, position=1)
# for task_item in task_iterator:
# test_case, related_files, sample_idx = task_item["test_case"], task_item["related_files"], task_item["sample_idx"]
# task_id = test_case["task_id"]
# masked_code = test_case['masked_code']
# tokens_test_code = count_tokens_custom(masked_code, tokenizer)
# token_budget_for_dynamic_content = (Config.MAX_TOKENS_FOR_PROMPT_CONSTRUCTION - tokens_system_message - tokens_static_user_prompt - Config.SAFETY_MARGIN_TOKENS_PROMPT_BUILDING)
# token_budget_for_related_files = token_budget_for_dynamic_content - tokens_test_code
# related_files_str, _ = get_related_files_content_budgeted_custom(reponame, related_files, tokenizer, token_budget_for_related_files) if token_budget_for_related_files > 0 else ("# 无相关文件上下文。\n", 0)
# user_prompt = Config.PROMPT_TEMPLATE_PARTS["user_prompt_template"].format(related_files_content=related_files_str, masked_code_content=masked_code)
# messages = [{"role": "system", "content": Config.PROMPT_TEMPLATE_PARTS["system_message"]}, {"role": "user", "content": user_prompt}]
# response_text, finish_reason, usage, error_details = "ERROR: INIT", "error", None, None
# try:
# response = client.chat.completions.create(
# model=Config.MODEL_NAME, messages=messages, temperature=Config.TEMPERATURE,
# top_p=Config.TOP_P, max_tokens=Config.MAX_COMPLETION_TOKENS
# )
# response_text = response.choices[0].message.content
# finish_reason = response.choices[0].finish_reason
# usage = response.usage.model_dump() if response.usage else None
# except APIError as e:
# response_text, error_details = f"ERROR: APIError - {e.message}", {"type": type(e).__name__, "message": str(e), "status_code": e.status_code}
# except Exception as e:
# response_text, error_details = f"ERROR: SDK/Network Error - {type(e).__name__}", {"type": type(e).__name__, "message": str(e)}
# intr_f.write(json.dumps({"task_id": task_id, "experiment_type": experiment_type, "sample_id": sample_idx, "timestamp_utc": datetime.datetime.now(datetime.timezone.utc).isoformat(), "messages_sent_to_model": messages, "model_used": Config.MODEL_NAME, "related_files_provided_to_prompt": related_files, "api_error_details": error_details}, ensure_ascii=False) + "\n")
# pred_f.write(json.dumps({"task_id": task_id, "experiment_type": experiment_type, "sample_id": sample_idx, "response": response_text.strip(), "finish_reason": finish_reason, "usage": usage}, ensure_ascii=False) + "\n")
# time.sleep(Config.REQUEST_DELAY_SECONDS)
# # ==============================================================================
# # 主函数 (协调器)
# # ==============================================================================
# def main():
# CUSTOM_API_KEY = "sk-Eyhl41vT8nBB8xLY77D7C9Bf0198453dA2797221Aa75E486"
# CUSTOM_API_BASE_URL = "https://api.vveai.com/v1/"
# OPENAI_COMPATIBLE_MODEL_NAME = "gpt-4.1-mini"
# Config.API_BASE_URL = CUSTOM_API_BASE_URL
# Config.API_KEY = CUSTOM_API_KEY
# Config.MODEL_NAME = OPENAI_COMPATIBLE_MODEL_NAME
# Config.MODEL_CONTEXT_WINDOW = 10240
# model_name_slug = re.sub(r'[^a-zA-Z0-9_-]', '_', Config.MODEL_NAME)
# Config.EXPERIMENTS_OUTPUT_DIR = Path(f"experiments_output_oracle_{model_name_slug}_{Config.MODEL_CONTEXT_WINDOW}")
# Config.MAX_TOKENS_FOR_PROMPT_CONSTRUCTION = Config.MODEL_CONTEXT_WINDOW - Config.MAX_COMPLETION_TOKENS - 100
# # 当前实验为gpt-4.1min-10k的oracle实验
# print("="*80)
# print("实验配置初始化完成")
# print(f" - API Base URL: {Config.API_BASE_URL}")
# print(f" - Model Name: {Config.MODEL_NAME}")
# print(f" - 上下文窗口: {Config.MODEL_CONTEXT_WINDOW}")
# print(f" - 输出目录: {Config.EXPERIMENTS_OUTPUT_DIR}")
# print("="*80)
# random.seed(42)
# print("\n--- 初始化API客户端和Tokenizer ---")
# try:
# client = OpenAI(api_key=Config.API_KEY, base_url=Config.API_BASE_URL)
# tokenizer = tiktoken.get_encoding(Config.TOKENIZER_NAME)
# print("--- 初始化成功 ---\n")
# except Exception as e:
# print(f"错误: 初始化失败: {e}"); exit(1)
# data_conditions = {"original": Config.BASE_DATA_DIR / "original", "rewrite": Config.BASE_DATA_DIR / "rewrite"}
# for condition_name, dataset_dir in data_conditions.items():
# print(f"\n{'='*80}\n开始数据条件: '{condition_name.upper()}'\n{'='*80}")
# if not dataset_dir.exists():
# print(f"错误: 数据目录 '{dataset_dir}' 不存在。跳过。")
# continue
# repo_names = sorted([p.stem for p in dataset_dir.glob("*.jsonl")])
# repo_iterator = tqdm(repo_names, desc="Repos", position=0)
# for reponame in repo_iterator:
# repo_iterator.set_postfix_str(f"Repo: {reponame}")
# process_repository(reponame, dataset_dir, condition_name, client, tokenizer)
# print("\n--- 所有实验条件均已完成 ---")
# if __name__ == "__main__":
# main()
import json
import os
from pathlib import Path
import time
import datetime
import random
import re
import argparse
from tqdm import tqdm
from openai import OpenAI, APIError
import tiktoken
import httpx # 导入httpx以支持代理
# ==============================================================================
# 实验配置 (未更改您原有的配置)
# ==============================================================================
class Config:
# --- 动态API配置 (由命令行参数设置) ---
API_BASE_URL = None
API_KEY = None
MODEL_NAME = None
MODEL_CONTEXT_WINDOW = None
# --- 静态目录配置 ---
BASE_DATA_DIR = Path("data_collection_align")
BM25_RANK_DIR = Path("output_with_bm25_rank")
CALL_CHAIN_DATA_DIR = Path("output_results")
REPO_BASE_DIR = Path("python_repos")
EXPERIMENTS_OUTPUT_DIR = None
# --- 实验参数 (完全保留您原有的设置) ---
MAX_COMPLETION_TOKENS = 512
MAX_ITEMS_PER_REPO = 200
NUM_SAMPLES_PER_TASK = 5
TEMPERATURE = 0.7
TOP_P = 0.9
REQUEST_DELAY_SECONDS = 1
# --- Tokenizer (完全保留您原有的设置) ---
TOKENIZER_NAME = "o200k_base"
# --- 文件选择参数 (完全保留您原有的设置) ---
MAX_RELATED_FILES_TO_CONSIDER_RETRIEVAL = 20
NUM_CONFUSION_FILES_TO_SAMPLE = 20
TOP_N_TO_EXCLUDE_FOR_CONFUSION = 20
MAX_ORACLE_FILES_TO_CONSIDER = 20
# --- 派生常量 ---
MAX_TOKENS_FOR_PROMPT_CONSTRUCTION = None
SAFETY_MARGIN_TOKENS_PROMPT_BUILDING = 50
# [NEW] 增加网络健壮性参数,不影响实验逻辑
API_TIMEOUT_SECONDS = 60.0
PROMPT_TEMPLATE_PARTS = {
"system_message": """Your task is to infer the correct values for the '???' placeholder based on provided Python code.
# Rules
1. Analyze the provided code and related files to determine the value.
2. The value MUST be a determined value or a valid Python expression.
3. **Do not** output any explanations, reasoning, introductions, or any other text outside of the specified output format. Your output must be clean.
# Output format
```output
answer_of_placeholder
```
# Example
Here is an example of a perfect interaction.
## Related files are as follow:
### file: config.py
```python
DEFAULT_TIMEOUT = 50
```
### file: get_timeout.py```python
from config import DEFAULT_TIMEOUT
def get_timeout():
return DEFAULT_TIMEOUT
```
## Test function is as follows:
### test
```python
def test_timeout():
from get_timeout import get_timeout
assert get_timeout() == '???'
```
### Your Correct Output:
```output
50
```
""",
"user_prompt_template": """## Related files are as follows:
{related_files_content}
## Test function is as follows:
```python
{masked_code_content}
```
Based on all the provided context and the test function, infer the correct value or expression for the "???" placeholder. Output it directly in the required format. Do not output extra information.
"""
}
# ==============================================================================
# 辅助函数 (无变化)
# ==============================================================================
def count_tokens_custom(text, tok):
return len(tok.encode(text))
def load_jsonl(file_path):
data = []
if not file_path.exists(): return data
with open(file_path, 'r', encoding='utf-8') as f:
for ln, line in enumerate(f):
try:
data.append(json.loads(line))
except json.JSONDecodeError:
pass
return data
def get_related_files_content_budgeted_custom(reponame_arg, related_files_rank_list, current_tokenizer, token_budget_for_related_files):
content_parts, accumulated_tokens = [], 0
if token_budget_for_related_files <= 0: return "# 没有为相关文件分配token预算。\n", 0
for rel_path_str in related_files_rank_list:
if accumulated_tokens >= token_budget_for_related_files: break
file_path = Config.REPO_BASE_DIR / reponame_arg / rel_path_str
file_header_str = f"### file: {rel_path_str}\n```python\n"
file_footer_str = "\n```\n"
file_header_tokens = count_tokens_custom(file_header_str, current_tokenizer)
file_footer_tokens = count_tokens_custom(file_footer_str, current_tokenizer)
file_content_str = ""
try:
if file_path.exists() and file_path.is_file():
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f: file_content_str = f.read()
else: file_content_str = f"# 文件 {file_path} 未找到"
except Exception as e: file_content_str = f"# 读取文件错误 {file_path}: {e}"
available_tokens_for_this_file = token_budget_for_related_files - accumulated_tokens
if file_header_tokens + file_footer_tokens >= available_tokens_for_this_file: break
current_file_str_parts = [file_header_str]
content_tokens_budget = available_tokens_for_this_file - file_header_tokens - file_footer_tokens
if content_tokens_budget > 0:
content_token_ids = current_tokenizer.encode(file_content_str)
if len(content_token_ids) > content_tokens_budget:
truncated_content_token_ids = content_token_ids[:content_tokens_budget]
truncated_content_str = current_tokenizer.decode(truncated_content_token_ids)
current_file_str_parts.append(truncated_content_str)
else:
current_file_str_parts.append(file_content_str)
current_file_str_parts.append(file_footer_str)
full_part = "".join(current_file_str_parts)
part_tokens = count_tokens_custom(full_part, current_tokenizer)
if accumulated_tokens + part_tokens <= token_budget_for_related_files:
content_parts.append(full_part)
accumulated_tokens += part_tokens
else: break
if not content_parts:
return "# 没有相关文件内容可以在token限制内被包含。\n", 0
return "".join(content_parts), accumulated_tokens
def normalize_funcname(name: str) -> str:
name = re.sub(r'\[.*\]$', '', name)
if '::' in name:
name = name.split('::')[-1]
return name
# ==============================================================================
# 核心实验逻辑
# ==============================================================================
def process_repository(reponame, dataset_dir, condition_name, client, tokenizer):
dataset_file = dataset_dir / f"{reponame}.jsonl"
bm25_file = Config.BM25_RANK_DIR / f"{reponame}.jsonl"
call_chain_file = Config.CALL_CHAIN_DATA_DIR / reponame / "report_functions.jsonl"
if not dataset_file.exists(): return
test_data_list = load_jsonl(dataset_file)[:Config.MAX_ITEMS_PER_REPO]
bm25_lookup = {item['task_id']: item for item in load_jsonl(bm25_file)}
call_chain_data = load_jsonl(call_chain_file)
call_chain_lookup = {}
for item in call_chain_data:
dirty_func_name = item.get('test_function')
if not dirty_func_name: continue
normalized_name = normalize_funcname(dirty_func_name)
sorted_deps = sorted(item.get('dependencies', []), key=lambda x: x.get('hops', float('inf')))
call_chain_lookup[normalized_name] = [dep['file'] for dep in sorted_deps]
tokens_system_message = count_tokens_custom(Config.PROMPT_TEMPLATE_PARTS["system_message"], tokenizer)
static_user_prompt_template = Config.PROMPT_TEMPLATE_PARTS["user_prompt_template"].format(related_files_content="", masked_code_content="")
tokens_static_user_prompt = count_tokens_custom(static_user_prompt_template, tokenizer)
# for experiment_type in ["retrieval", "confusion", "oracle"]:
for experiment_type in ["oracle"]:
print(f" -> 开始实验: {experiment_type}")
current_preds_dir = Config.EXPERIMENTS_OUTPUT_DIR / condition_name / "predictions" / experiment_type
current_inters_dir = Config.EXPERIMENTS_OUTPUT_DIR / condition_name / "interactions" / experiment_type
current_preds_dir.mkdir(parents=True, exist_ok=True)
current_inters_dir.mkdir(parents=True, exist_ok=True)
predictions_output_file = current_preds_dir / f"{reponame}.jsonl"
interactions_output_file = current_inters_dir / f"{reponame}.jsonl"
# [NEW] 断点续存: 加载已完成的任务
completed_tasks = set()
if predictions_output_file.exists():
for item in load_jsonl(predictions_output_file):
task_key = (item.get('task_id'), item.get('sample_id'))
completed_tasks.add(task_key)
# 仅在需要处理任务时打印消息
if completed_tasks:
print(f" 发现 {len(completed_tasks)} 个已完成的任务,将跳过它们。")
# [MODIFIED] 以追加模式 ('a') 打开文件
with open(predictions_output_file, 'a', encoding='utf-8') as pred_f, \
open(interactions_output_file, 'a', encoding='utf-8') as intr_f:
tasks_to_process = []
for test_case in test_data_list:
task_id = test_case['task_id']
related_files_for_experiment = []
if experiment_type == "retrieval":
if task_id in bm25_lookup:
files = bm25_lookup[task_id].get('related_files_rank', [])
related_files_for_experiment = files[:Config.MAX_RELATED_FILES_TO_CONSIDER_RETRIEVAL]
random.shuffle(related_files_for_experiment)
elif experiment_type == "confusion":
if task_id in bm25_lookup:
files = bm25_lookup[task_id].get('related_files_rank', [])
pool = files[Config.TOP_N_TO_EXCLUDE_FOR_CONFUSION:]
related_files_for_experiment = random.sample(pool, min(len(pool), Config.NUM_CONFUSION_FILES_TO_SAMPLE))
elif experiment_type == "oracle":
func_name = test_case.get('funcname')
if func_name and normalize_funcname(func_name) in call_chain_lookup:
files = call_chain_lookup[normalize_funcname(func_name)]
related_files_for_experiment = files[:Config.MAX_ORACLE_FILES_TO_CONSIDER]
for sample_idx in range(Config.NUM_SAMPLES_PER_TASK):
tasks_to_process.append({"test_case": test_case, "related_files": related_files_for_experiment, "sample_idx": sample_idx})
task_iterator = tqdm(tasks_to_process, desc=f" Tasks", leave=False, position=1)
for task_item in task_iterator:
test_case, related_files, sample_idx = task_item["test_case"], task_item["related_files"], task_item["sample_idx"]
task_id = test_case["task_id"]
# [NEW] 断点续存: 检查并跳过
task_key = (task_id, sample_idx)
if task_key in completed_tasks:
continue
masked_code = test_case['masked_code']
tokens_test_code = count_tokens_custom(masked_code, tokenizer)
token_budget_for_dynamic_content = (Config.MAX_TOKENS_FOR_PROMPT_CONSTRUCTION - tokens_system_message - tokens_static_user_prompt - Config.SAFETY_MARGIN_TOKENS_PROMPT_BUILDING)
token_budget_for_related_files = token_budget_for_dynamic_content - tokens_test_code
related_files_str, _ = get_related_files_content_budgeted_custom(reponame, related_files, tokenizer, token_budget_for_related_files) if token_budget_for_related_files > 0 else ("# 无相关文件上下文。\n", 0)
user_prompt = Config.PROMPT_TEMPLATE_PARTS["user_prompt_template"].format(related_files_content=related_files_str, masked_code_content=masked_code)
messages = [{"role": "system", "content": Config.PROMPT_TEMPLATE_PARTS["system_message"]}, {"role": "user", "content": user_prompt}]
response_text, finish_reason, usage, error_details = "ERROR: INIT", "error", None, None
# [FIXED] 更全面的错误处理
try:
response = client.chat.completions.create(
model=Config.MODEL_NAME, messages=messages, temperature=Config.TEMPERATURE,
top_p=Config.TOP_P, max_tokens=Config.MAX_COMPLETION_TOKENS
)
response_text = response.choices[0].message.content
finish_reason = response.choices[0].finish_reason
usage = response.usage.model_dump() if response.usage else None
except APIError as e:
print(f"\n 捕获到 API 错误 (Task: {task_id}): {type(e).__name__} - {e}")
response_text = f"ERROR: {type(e).__name__} - {str(e)}"