MCPcopy Create free account
hub / github.com/SkyworkAI/DeepResearchAgent / inference_with_reflection

Function inference_with_reflection

tests/leetcode_reflection_agent.py:853–1098  ·  view source on GitHub ↗

带反思的推理:生成代码 -> 评测 -> 反思改进 -> 再评测,最多MAX_REFLECTION_ROUNDS轮

(
    task: Task,
    save_dir: str,
    semaphore: asyncio.Semaphore,
    benchmark: Any,
)

Source from the content-addressed store, hash-verified

851
852
853async def inference_with_reflection(
854 task: Task,
855 save_dir: str,
856 semaphore: asyncio.Semaphore,
857 benchmark: Any,
858) -> Task:
859 """
860 带反思的推理:生成代码 -> 评测 -> 反思改进 -> 再评测,最多MAX_REFLECTION_ROUNDS轮
861 """
862 task_id = task.task_id
863
864 async with semaphore:
865 inference_start_time = time.time()
866 task.extra["inference_start_time"] = inference_start_time
867
868 current_code = ""
869 current_reasoning = ""
870 best_score = 0.0
871 best_code = ""
872 best_reasoning = ""
873 best_eval_result = None
874
875 try:
876 logger.info(f"| 🚀 [Task {task_id}] Starting inference with reflection...")
877
878 # ============ Round 1: 初始生成 ============
879 logger.info(f"| 🔄 [Task {task_id}] Round 1: Generating initial solution...")
880
881 try:
882 initial_response = await generate_initial_solution(task, TARGET_MODEL)
883 current_reasoning = initial_response.reasoning
884 current_code = initial_response.result
885
886 logger.info(f"| ✅ [Task {task_id}] Round 1: Initial solution generated")
887
888 except Exception as e:
889 logger.error(f"| ❌ [Task {task_id}] Round 1: Failed to generate initial solution: {e}")
890 task.reasoning = ""
891 task.result = ""
892 task.extra["inference_time"] = time.time() - inference_start_time
893 task.extra["final_round"] = 0
894 task.extra["error"] = str(e)
895 return task
896
897 # 保存初始代码
898 try:
899 file_name = f"{task.extra['file_name']}_round1.md"
900 file_path = os.path.join(save_dir, file_name)
901 with open(file_path, "w", encoding="utf-8") as f:
902 f.write(f"# Round 1 - Initial Solution\n\n")
903 f.write(f"## Reasoning\n{current_reasoning}\n\n")
904 f.write(f"## Code\n```\n{current_code}\n```\n")
905 except Exception as save_err:
906 logger.warning(f"| ⚠️ [Task {task_id}] Failed to save round 1 markdown: {save_err}")
907
908 # ============ 迭代评测和反思 ============
909 for round_num in range(1, MAX_REFLECTION_ROUNDS + 1):
910 logger.info(f"| 📊 [Task {task_id}] Round {round_num}: Evaluating solution...")

Callers 1

process_taskFunction · 0.70

Calls 14

TaskClass · 0.90
joinMethod · 0.80
warningMethod · 0.80
eval_single_fileMethod · 0.80
format_evaluation_resultFunction · 0.70
infoMethod · 0.45
errorMethod · 0.45
writeMethod · 0.45

Tested by

no test coverage detected