代码仓库: https://github.com/HKUDS/DeepCode
PyPI 安装:pip install deepcode-hku
学术论文到工程代码的转化一直是研究落地的"最后一公里"难题。研究人员花费大量时间实现算法而非专注科研,产品团队在概念验证阶段陷入漫长等待。港大数据智能实验室开源的DeepCode试图用多智能体架构彻底解决这个问题——它不仅在 OpenAI 的 PaperBench 基准上超越人类专家(75.9% vs 72.4%),更重要的是,它提供了一套工程化的、可复现的解决方案。
DeepCode 构建了7 个专业智能体,通过中央编排引擎实现复杂任务的分解与协作:
系统的核心亮点是ParallelLLM机制,让 ConceptAnalysisAgent 和 AlgorithmAnalysisAgent 并行分析同一篇论文,再由 CodePlannerAgent 整合输出:
# workflows/agent_orchestration_engine.py
concept_analysis_agent = Agent(
name="ConceptAnalysisAgent",
instruction=prompts["concept_analysis"],
server_names=agent_config["concept_analysis"],
)
algorithm_analysis_agent = Agent(
name="AlgorithmAnalysisAgent",
instruction=prompts["algorithm_analysis"],
server_names=agent_config["algorithm_analysis"],
)
code_planner_agent = Agent(
name="CodePlannerAgent",
instruction=prompts["code_planning"],
server_names=agent_config["code_planner"],
)
# 扇出/扇入架构:并行分析 → 统一整合
code_aggregator_agent = ParallelLLM(
fan_in_agent=code_planner_agent,
fan_out_agents=[concept_analysis_agent, algorithm_analysis_agent],
llm_factory=get_preferred_llm_class(),
)
这种设计兼顾了效率(并行处理)和质量(多视角分析),每个 Agent 专注于自己的领域,最终由 Planner 综合决策。
编排引擎管理8 个处理阶段:研究分析 → 资源处理 → 工作空间构建 → 代码规划 → 参考智能分析 → 仓库获取 → 代码库索引 → 代码实现。
关键设计是自适应配置:根据文档大小和复杂度动态调整 Agent 配置。例如,大型论文触发文档分段机制,小文档则直接处理;用户可通过enable_indexing参数在"全功能模式"和"快速模式"间切换。
DeepCode 的内存管理采用了一种激进且巧妙的策略——基于write_file触发的内存优化:核心逻辑体现在
ConciseMemoryAgent中:
# workflows/agents/memory_agent_concise.py
defcreate_concise_messages(self, system_prompt, messages, files_implemented):
"""每次 write_file 后,清空历史,只保留关键信息"""
ifnotself.last_write_file_detected:
returnmessages # 首次写文件前,保持完整历史
concise_messages = []
# 1. 保留初始计划消息
initial_plan_message = {
"role":"user",
"content":f"""**Code Reproduction Plan:**
{self.initial_plan}
**Previously Implemented Files:**
{implemented_files_list}
**Current Status:**{files_implemented}files implemented"""
}
concise_messages.append(initial_plan_message)
# 2. 添加知识库(最新实现文件的摘要)
knowledge_base_message = {
"role":"user",
"content":f"""**Knowledge Base:**
{self._read_code_knowledge_base()}
**Development Cycle:**
1. Call read_code_mem() to understand dependencies
2. Use write_file() to implement new component
3. Execute tests if needed"""
}
concise_messages.append(knowledge_base_message)
# 3. 添加当前轮次的工具结果(仅关键工具)
ifself.current_round_tool_results:
concise_messages.append({
"role":"user",
"content":f"**Current Tool Results:**\n{tool_results}"
})
returnconcise_messages
这种设计实现了70-90% 的上下文压缩率,每个新文件的实现都有一个干净的起点,避免了上下文膨胀导致的 token 溢出。
CodeRAG 通过code-reference-indexerMCP 服务器实现代码的语义索引和智能检索:
# workflows/agents/code_implementation_agent.py
asyncdef_handle_read_file_with_memory_optimization(self, tool_call):
"""拦截 read_file 调用,优先使用摘要缓存"""
file_path = tool_call["input"].get("file_path")
# 1. 先尝试从内存读取摘要
mem_result =awaitself.mcp_client.call_tool(
"read_code_mem",
{"file_path": file_path}
)
if"successfully"inmem_result.lower():
# 摘要存在,直接返回(节省 token)
return{"tool_id": tool_call["id"],"result": mem_result}
# 2. 摘要不存在,执行原始文件读取
original_result =awaitself.mcp_client.call_tool(
"read_file",
tool_call["input"]
)
return{"tool_id": tool_call["id"],"result": original_result}
CodeRAG 的价值在于按需加载:只在需要时读取完整文件,大部分情况下通过摘要即可理解代码结构和依赖关系。
DeepCode 系统性地实现了业界提出的四种上下文工程方法:
| Offload | read_code_mem | |
| Retrieve | search_reference_code | |
| Reduce | ||
| Isolate |
这四种方法的协同效果:
在 OpenAI 的 PaperBench 基准测试上,DeepCode 创造了多项新纪录:
这些数据背后的关键不是模型能力(大家都用 Claude 3.5 Sonnet 或 GPT-4),而是架构设计:
DeepCode 的设计体现了明确的工程权衡:
优势:
劣势:
系统通过两种运行模式平衡速度和质量:
DeepCode 的价值不仅在于"能把论文变成代码",更在于它提供了一套可复现的多智能体架构范式:
| 欢迎光临 链载Ai (https://www.lianzai.com/) | Powered by Discuz! X3.5 |