跳转至

RD-Agent 教程 - 第六章:扩展开发与社区贡献

📋 本章概览

第六章将介绍如何扩展 RD-Agent 的功能,以及如何参与开源社区的贡献。本章适合希望深度定制RD-Agent或为项目做贡献的开发者。

🎯 章节目标

  • ✅ 掌握插件开发:创建自定义插件和扩展模块
  • ✅ 理解架构设计:深入了解RD-Agent的内部架构
  • ✅ 学会贡献代码:参与开源项目的开发和维护
  • ✅ 建立开发环境:搭建用于开发的本地环境
  • ✅ 遵循开发规范:学习代码规范和最佳实践

📁 本章结构

tutorial_chapter6/
├── tutorial_chapter6_extensions.md             # 本文件 - 扩展开发指南
├── examples/                                    # 代码示例目录
│   ├── custom_agent/                           # 自定义智能体示例
│   │   ├── my_research_agent.py                # 研究智能体实现
│   │   ├── my_development_agent.py             # 开发智能体实现
│   │   └── agent_registry.py                  # 智能体注册器
│   ├── custom_scenarios/                       # 自定义场景示例
│   │   ├── biotech_scenario.py                # 生物技术场景
│   │   ├── finance_scenario.py                # 金融场景
│   │   └── nlp_scenario.py                    # NLP场景
│   ├── plugins/                                # 插件示例
│   │   ├── data_connector.py                  # 数据连接器插件
│   │   ├── model_optimizer.py                 # 模型优化插件
│   │   └── notification_plugin.py             # 通知插件
│   └── tools/                                  # 开发工具
│       ├── development_setup.py               # 开发环境配置
│       ├── testing_framework.py               # 测试框架
│       └── contribution_checker.py            # 贡献检查工具
└── templates/                                  # 模板文件
    ├── agent_template.py                      # 智能体模板
    ├── scenario_template.py                   # 场景模板
    └── plugin_template.py                     # 插件模板

🔧 第一部分:架构理解与开发环境

1.1 RD-Agent 架构深入分析

RD-Agent采用模块化的多智能体架构,主要包含以下核心组件:

核心架构图

┌─────────────────────────────────────────────────────────────┐
│                    RD-Agent 核心架构                        │
├─────────────────────────────────────────────────────────────┤
│  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐        │
│  │ 研究智能体   │  │ 开发智能体   │  │ 评估智能体   │        │
│  │ Research    │  │ Development │  │ Evaluation  │        │
│  │ Agent       │  │ Agent       │  │ Agent       │        │
│  └─────────────┘  └─────────────┘  └─────────────┘        │
│         │                 │                 │             │
│  ┌──────────────────────────────────────────────────────┐  │
│  │              协调层 (Coordination Layer)            │  │
│  └──────────────────────────────────────────────────────┘  │
│         │                 │                 │             │
│  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐        │
│  │   工具层     │  │   模型层     │  │   数据层     │        │
│  │ Tools Layer │  │ Model Layer │  │ Data Layer  │        │
│  └─────────────┘  └─────────────┘  └─────────────┘        │
│         │                 │                 │             │
│  ┌──────────────────────────────────────────────────────┐  │
│  │              基础设施层 (Infrastructure)             │  │
│  │  ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐    │  │
│  │  │ LLM API │ │ Docker  │ │ Git     │ │ Monitor │    │  │
│  │  └─────────┘ └─────────┘ └─────────┘ └─────────┘    │  │
│  └──────────────────────────────────────────────────────┘  │
└─────────────────────────────────────────────────────────────┘

关键扩展点

  1. 智能体层面:可以继承基础智能体类创建专门领域的智能体
  2. 工具层面:可以开发新的工具来增强智能体能力
  3. 场景层面:可以定义新的应用场景和工作流
  4. 插件层面:可以创建插件来扩展核心功能

1.2 开发环境搭建指南

系统要求

# 基础要求
Python >= 3.10
Git >= 2.20
Docker >= 20.10 (可选)

# 推荐配置
CPU: 4核心以上
内存: 8GB以上
存储: 20GB可用空间

快速环境搭建

使用我们提供的自动化脚本:

# 1. 克隆仓库
git clone https://github.com/microsoft/RD-Agent.git
cd RD-Agent

# 2. 运行开发环境配置脚本
python tutorial_examples/chapter6/development_setup.py

# 3. 激活虚拟环境
source venv/bin/activate  # Linux/macOS
# venv\Scripts\activate   # Windows

# 4. 验证安装
python -c "import rdagent; print('RD-Agent安装成功!')"

开发工具配置

VS Code 配置

{
  "python.defaultInterpreterPath": "./venv/bin/python",
  "python.formatting.provider": "black",
  "python.linting.enabled": true,
  "python.linting.flake8Enabled": true,
  "editor.formatOnSave": true,
  "files.exclude": {
    "**/__pycache__": true,
    "**/venv": true
  }
}

Git 工作流配置

# 设置提交模板
git config commit.template .gitmessage

# 配置预提交钩子
pre-commit install

# 创建开发分支
git checkout -b feature/your-feature-name


🤖 第二部分:自定义智能体开发

2.1 智能体基类详解

RD-Agent中的所有智能体都继承自基础智能体类。理解这个基类的设计是开发自定义智能体的关键。

基础智能体接口

from abc import ABC, abstractmethod
from typing import Dict, List, Optional, Any
from dataclasses import dataclass
from enum import Enum

class AgentRole(Enum):
    RESEARCHER = "researcher"
    DEVELOPER = "developer"
    EVALUATOR = "evaluator"
    COORDINATOR = "coordinator"
    CUSTOM = "custom"

@dataclass
class AgentConfig:
    name: str
    role: AgentRole
    description: str
    max_iterations: int = 10
    timeout_seconds: int = 300
    llm_config: Dict[str, Any] = None
    tools: List[str] = None

class BaseAgent(ABC):
    def __init__(self, config: AgentConfig):
        self.config = config
        self.state = {}
        self.tools = []
        self.history = []

    @abstractmethod
    async def process_task(self, task: Dict[str, Any]) -> Dict[str, Any]:
        """处理任务的核心方法"""
        pass

    @abstractmethod
    def _initialize_tools(self):
        """初始化智能体专用工具"""
        pass

2.2 研究智能体开发示例

以生物医学研究智能体为例,展示完整的开发过程:

class BioMedResearchAgent(BaseAgent):
    """生物医学研究专用智能体"""

    def __init__(self, config: AgentConfig):
        super().__init__(config)
        self.knowledge_base = BiologyKnowledgeBase()
        self.experiment_tracker = ExperimentTracker()

    def _initialize_tools(self):
        """初始化生物医学专用工具"""
        self.add_tool(PubMedSearchTool())
        self.add_tool(ProteinAnalysisTool())
        self.add_tool(GenomicsDataTool())
        self.add_tool(ClinicalTrialTool())

    async def process_task(self, task: Dict[str, Any]) -> Dict[str, Any]:
        """处理生物医学研究任务"""
        research_type = task.get("type", "general")

        if research_type == "drug_discovery":
            return await self._drug_discovery_pipeline(task)
        elif research_type == "genomics":
            return await self._genomics_analysis_pipeline(task)
        elif research_type == "clinical_trial":
            return await self._clinical_trial_design(task)
        else:
            return await self._general_research_pipeline(task)

    async def _drug_discovery_pipeline(self, task: Dict) -> Dict:
        """药物发现流程"""
        # 1. 目标蛋白质分析
        target_analysis = await self._analyze_target_protein(task["target"])

        # 2. 化合物筛选
        compounds = await self._screen_compounds(target_analysis)

        # 3. 分子对接模拟
        docking_results = await self._molecular_docking(compounds)

        # 4. ADMET预测
        admet_predictions = await self._predict_admet(compounds)

        # 5. 候选药物排序
        ranked_candidates = await self._rank_drug_candidates(
            docking_results, admet_predictions
        )

        return {
            "status": "success",
            "pipeline": "drug_discovery",
            "target_analysis": target_analysis,
            "top_candidates": ranked_candidates[:10],
            "next_steps": self._recommend_next_steps(ranked_candidates)
        }

2.3 开发智能体开发示例

class MLDevAgent(BaseAgent):
    """机器学习开发专用智能体"""

    def __init__(self, config: AgentConfig):
        super().__init__(config)
        self.code_repository = CodeRepository()
        self.model_registry = ModelRegistry()

    def _initialize_tools(self):
        """初始化ML开发工具"""
        self.add_tool(AutoMLTool())
        self.add_tool(CodeGeneratorTool())
        self.add_tool(ModelOptimizationTool())
        self.add_tool(TestGeneratorTool())

    async def process_task(self, task: Dict[str, Any]) -> Dict[str, Any]:
        """处理ML开发任务"""
        task_type = task.get("type", "end_to_end")

        if task_type == "model_development":
            return await self._model_development_pipeline(task)
        elif task_type == "code_optimization":
            return await self._code_optimization_pipeline(task)
        elif task_type == "deployment":
            return await self._deployment_pipeline(task)
        else:
            return await self._end_to_end_pipeline(task)

    async def _end_to_end_pipeline(self, task: Dict) -> Dict:
        """端到端ML开发流程"""
        # 1. 数据分析和预处理
        data_analysis = await self._analyze_data(task["data"])
        preprocessing_code = await self._generate_preprocessing_code(data_analysis)

        # 2. 模型选择和训练
        model_candidates = await self._select_model_architectures(task)
        training_results = await self._train_models(model_candidates, task["data"])

        # 3. 模型评估和优化
        evaluation_results = await self._evaluate_models(training_results)
        optimized_model = await self._optimize_best_model(evaluation_results)

        # 4. 代码生成
        production_code = await self._generate_production_code(optimized_model)
        test_code = await self._generate_test_code(production_code)

        # 5. 部署配置
        deployment_config = await self._generate_deployment_config(optimized_model)

        return {
            "status": "success",
            "data_analysis": data_analysis,
            "best_model": optimized_model,
            "production_code": production_code,
            "test_code": test_code,
            "deployment_config": deployment_config,
            "performance_metrics": evaluation_results["best_metrics"]
        }

🔌 第三部分:插件系统开发

3.1 插件架构设计

RD-Agent的插件系统基于事件驱动架构,允许在关键节点注入自定义逻辑。

插件生命周期

┌─────────────┐    ┌─────────────┐    ┌─────────────┐
│   加载插件   │───▶│   初始化     │───▶│   注册钩子   │
│  Load Plugin│    │  Initialize │    │Register Hook│
└─────────────┘    └─────────────┘    └─────────────┘
       │                   │                   │
       ▼                   ▼                   ▼
┌─────────────┐    ┌─────────────┐    ┌─────────────┐
│   执行任务   │───▶│   处理事件   │───▶│   清理资源   │
│Execute Task │    │Handle Event │    │  Cleanup    │
└─────────────┘    └─────────────┘    └─────────────┘

3.2 数据连接器插件示例

from rdagent.core.plugin import BasePlugin
from rdagent.core.events import EventType
from typing import Dict, Any, List

class DataConnectorPlugin(BasePlugin):
    """数据连接器插件"""

    def __init__(self, config: Dict[str, Any]):
        super().__init__("data_connector", "1.0.0")
        self.config = config
        self.connections = {}

    async def initialize(self):
        """初始化插件"""
        await self._setup_connections()
        self.register_hook(EventType.DATA_REQUEST, self._handle_data_request)
        self.register_hook(EventType.DATA_UPLOAD, self._handle_data_upload)

    async def _setup_connections(self):
        """设置数据连接"""
        for conn_config in self.config.get("connections", []):
            conn_type = conn_config["type"]

            if conn_type == "postgresql":
                self.connections[conn_config["name"]] = await self._setup_postgresql(conn_config)
            elif conn_type == "mongodb":
                self.connections[conn_config["name"]] = await self._setup_mongodb(conn_config)
            elif conn_type == "s3":
                self.connections[conn_config["name"]] = await self._setup_s3(conn_config)

    async def _handle_data_request(self, event):
        """处理数据请求事件"""
        request = event.data
        source = request.get("source")
        query = request.get("query")

        if source in self.connections:
            connection = self.connections[source]
            data = await self._execute_query(connection, query)

            event.result = {
                "status": "success",
                "data": data,
                "metadata": {
                    "source": source,
                    "rows": len(data) if isinstance(data, list) else None
                }
            }
        else:
            event.result = {
                "status": "error",
                "error": f"Unknown data source: {source}"
            }

    async def _execute_query(self, connection, query):
        """执行数据查询"""
        conn_type = connection["type"]

        if conn_type == "postgresql":
            return await self._execute_sql_query(connection, query)
        elif conn_type == "mongodb":
            return await self._execute_mongo_query(connection, query)
        elif conn_type == "s3":
            return await self._fetch_s3_data(connection, query)

3.3 模型优化插件示例

class ModelOptimizationPlugin(BasePlugin):
    """模型优化插件"""

    def __init__(self, config: Dict[str, Any]):
        super().__init__("model_optimizer", "1.0.0")
        self.optimization_strategies = config.get("strategies", ["quantization", "pruning"])

    async def initialize(self):
        """初始化插件"""
        self.register_hook(EventType.MODEL_TRAINED, self._optimize_model)
        self.register_hook(EventType.MODEL_DEPLOY, self._prepare_optimized_model)

    async def _optimize_model(self, event):
        """优化训练好的模型"""
        model = event.data["model"]
        optimization_results = {}

        for strategy in self.optimization_strategies:
            if strategy == "quantization":
                optimized_model = await self._quantize_model(model)
                optimization_results["quantization"] = {
                    "model": optimized_model,
                    "compression_ratio": self._calculate_compression_ratio(model, optimized_model),
                    "accuracy_loss": await self._evaluate_accuracy_loss(model, optimized_model)
                }

            elif strategy == "pruning":
                optimized_model = await self._prune_model(model)
                optimization_results["pruning"] = {
                    "model": optimized_model,
                    "sparsity": self._calculate_sparsity(optimized_model),
                    "speed_improvement": await self._measure_speed_improvement(model, optimized_model)
                }

        event.result = optimization_results

    async def _quantize_model(self, model):
        """模型量化"""
        # 实现量化逻辑
        pass

    async def _prune_model(self, model):
        """模型剪枝"""
        # 实现剪枝逻辑
        pass

3.4 通知插件示例

class NotificationPlugin(BasePlugin):
    """通知插件"""

    def __init__(self, config: Dict[str, Any]):
        super().__init__("notification", "1.0.0")
        self.notification_channels = config.get("channels", [])

    async def initialize(self):
        """初始化插件"""
        await self._setup_notification_channels()

        # 注册各种事件的通知钩子
        self.register_hook(EventType.TASK_STARTED, self._notify_task_started)
        self.register_hook(EventType.TASK_COMPLETED, self._notify_task_completed)
        self.register_hook(EventType.TASK_FAILED, self._notify_task_failed)
        self.register_hook(EventType.MODEL_TRAINED, self._notify_model_ready)

    async def _notify_task_completed(self, event):
        """任务完成通知"""
        task = event.data
        message = f"✅ 任务 '{task['name']}' 已完成\n"
        message += f"用时: {task['duration']}\n"
        message += f"状态: {task['status']}"

        await self._send_notification("任务完成", message, priority="normal")

    async def _notify_task_failed(self, event):
        """任务失败通知"""
        task = event.data
        message = f"❌ 任务 '{task['name']}' 执行失败\n"
        message += f"错误: {task['error']}\n"
        message += f"建议: {task.get('suggestion', '请检查日志')}"

        await self._send_notification("任务失败", message, priority="high")

    async def _send_notification(self, title: str, message: str, priority: str = "normal"):
        """发送通知"""
        for channel in self.notification_channels:
            if channel["type"] == "email":
                await self._send_email(channel, title, message)
            elif channel["type"] == "slack":
                await self._send_slack_message(channel, title, message)
            elif channel["type"] == "webhook":
                await self._send_webhook(channel, title, message)

🎨 第四部分:自定义场景开发

4.1 场景架构设计

场景(Scenario)定义了特定领域的完整工作流程,包括智能体协作模式、数据流和评估标准。

场景基类

from abc import ABC, abstractmethod
from typing import Dict, List, Any, Optional
from dataclasses import dataclass

@dataclass
class ScenarioConfig:
    name: str
    description: str
    agents: List[str]
    workflow_steps: List[str]
    evaluation_metrics: List[str]
    timeout_minutes: int = 60

class BaseScenario(ABC):
    def __init__(self, config: ScenarioConfig):
        self.config = config
        self.agents = {}
        self.workflow_state = {}

    @abstractmethod
    async def initialize_agents(self):
        """初始化场景所需的智能体"""
        pass

    @abstractmethod
    async def execute_workflow(self, input_data: Dict[str, Any]) -> Dict[str, Any]:
        """执行场景工作流"""
        pass

    @abstractmethod
    async def evaluate_results(self, results: Dict[str, Any]) -> Dict[str, Any]:
        """评估场景执行结果"""
        pass

4.2 生物技术研究场景

class BiotechResearchScenario(BaseScenario):
    """生物技术研究场景"""

    async def initialize_agents(self):
        """初始化生物技术研究智能体"""
        # 研究智能体 - 负责文献调研和假设生成
        research_config = AgentConfig(
            name="BiotechResearcher",
            role=AgentRole.RESEARCHER,
            description="生物技术领域研究专家"
        )
        self.agents["researcher"] = BioMedResearchAgent(research_config)

        # 实验设计智能体 - 负责实验方案设计
        experiment_config = AgentConfig(
            name="ExperimentDesigner",
            role=AgentRole.DEVELOPER,
            description="实验设计专家"
        )
        self.agents["experimenter"] = ExperimentDesignAgent(experiment_config)

        # 数据分析智能体 - 负责实验数据分析
        analysis_config = AgentConfig(
            name="DataAnalyst",
            role=AgentRole.EVALUATOR,
            description="生物信息学数据分析专家"
        )
        self.agents["analyst"] = BioinformaticsAgent(analysis_config)

    async def execute_workflow(self, input_data: Dict[str, Any]) -> Dict[str, Any]:
        """执行生物技术研究工作流"""
        research_question = input_data.get("research_question")
        available_data = input_data.get("data", {})

        workflow_results = {}

        # 步骤1: 文献调研和背景分析
        research_task = {
            "question": research_question,
            "domain": "biotechnology",
            "focus_areas": ["genomics", "proteomics", "metabolomics"]
        }

        literature_review = await self.agents["researcher"].process_task(research_task)
        workflow_results["literature_review"] = literature_review

        # 步骤2: 实验设计
        experiment_task = {
            "research_context": literature_review,
            "available_resources": input_data.get("resources", {}),
            "constraints": input_data.get("constraints", [])
        }

        experiment_design = await self.agents["experimenter"].process_task(experiment_task)
        workflow_results["experiment_design"] = experiment_design

        # 步骤3: 数据分析(如果有现有数据)
        if available_data:
            analysis_task = {
                "data": available_data,
                "analysis_type": "exploratory",
                "research_context": literature_review
            }

            data_analysis = await self.agents["analyst"].process_task(analysis_task)
            workflow_results["data_analysis"] = data_analysis

        # 步骤4: 整合结果和建议
        integrated_results = await self._integrate_workflow_results(workflow_results)

        return {
            "status": "success",
            "workflow_results": workflow_results,
            "integrated_insights": integrated_results,
            "next_steps": self._generate_next_steps(integrated_results)
        }

    async def evaluate_results(self, results: Dict[str, Any]) -> Dict[str, Any]:
        """评估生物技术研究结果"""
        evaluation_metrics = {
            "literature_coverage": self._evaluate_literature_coverage(results),
            "experiment_feasibility": self._evaluate_experiment_feasibility(results),
            "innovation_score": self._evaluate_innovation(results),
            "ethical_compliance": self._evaluate_ethics(results),
            "reproducibility": self._evaluate_reproducibility(results)
        }

        overall_score = sum(evaluation_metrics.values()) / len(evaluation_metrics)

        return {
            "evaluation_metrics": evaluation_metrics,
            "overall_score": overall_score,
            "recommendations": self._generate_improvement_recommendations(evaluation_metrics)
        }

4.3 金融量化场景

class QuantitativeFinanceScenario(BaseScenario):
    """量化金融研究场景"""

    async def initialize_agents(self):
        """初始化量化金融智能体"""
        # 因子研究智能体
        factor_config = AgentConfig(
            name="FactorResearcher",
            role=AgentRole.RESEARCHER,
            description="金融因子挖掘专家"
        )
        self.agents["factor_researcher"] = FactorResearchAgent(factor_config)

        # 策略开发智能体
        strategy_config = AgentConfig(
            name="StrategyDeveloper",
            role=AgentRole.DEVELOPER,
            description="量化策略开发专家"
        )
        self.agents["strategy_developer"] = StrategyDevelopmentAgent(strategy_config)

        # 回测评估智能体
        backtest_config = AgentConfig(
            name="BacktestEvaluator",
            role=AgentRole.EVALUATOR,
            description="回测和风险评估专家"
        )
        self.agents["evaluator"] = BacktestEvaluationAgent(backtest_config)

    async def execute_workflow(self, input_data: Dict[str, Any]) -> Dict[str, Any]:
        """执行量化金融工作流"""
        market_data = input_data.get("market_data")
        strategy_objective = input_data.get("objective", "alpha_generation")

        workflow_results = {}

        # 步骤1: 因子研究
        factor_task = {
            "market_data": market_data,
            "factor_categories": ["technical", "fundamental", "macro"],
            "target_universe": input_data.get("universe", "all_stocks")
        }

        factor_results = await self.agents["factor_researcher"].process_task(factor_task)
        workflow_results["factor_research"] = factor_results

        # 步骤2: 策略开发
        strategy_task = {
            "factors": factor_results["significant_factors"],
            "objective": strategy_objective,
            "constraints": input_data.get("constraints", {}),
            "market_regime": factor_results.get("market_regime_analysis")
        }

        strategy_results = await self.agents["strategy_developer"].process_task(strategy_task)
        workflow_results["strategy_development"] = strategy_results

        # 步骤3: 回测和评估
        backtest_task = {
            "strategy": strategy_results["optimized_strategy"],
            "market_data": market_data,
            "benchmark": input_data.get("benchmark", "market_index"),
            "risk_constraints": input_data.get("risk_limits", {})
        }

        backtest_results = await self.agents["evaluator"].process_task(backtest_task)
        workflow_results["backtest_evaluation"] = backtest_results

        # 步骤4: 生成投资建议
        investment_recommendations = await self._generate_investment_recommendations(
            workflow_results
        )

        return {
            "status": "success",
            "workflow_results": workflow_results,
            "investment_recommendations": investment_recommendations,
            "performance_summary": self._summarize_performance(backtest_results)
        }

    async def evaluate_results(self, results: Dict[str, Any]) -> Dict[str, Any]:
        """评估量化金融结果"""
        evaluation_metrics = {
            "return_quality": self._evaluate_returns(results),
            "risk_management": self._evaluate_risk_control(results),
            "factor_significance": self._evaluate_factor_quality(results),
            "strategy_robustness": self._evaluate_robustness(results),
            "implementation_feasibility": self._evaluate_implementation(results)
        }

        overall_score = sum(evaluation_metrics.values()) / len(evaluation_metrics)

        return {
            "evaluation_metrics": evaluation_metrics,
            "overall_score": overall_score,
            "risk_warnings": self._identify_risk_warnings(results),
            "optimization_suggestions": self._suggest_optimizations(evaluation_metrics)
        }

4.4 NLP研究场景

class NLPResearchScenario(BaseScenario):
    """自然语言处理研究场景"""

    async def initialize_agents(self):
        """初始化NLP研究智能体"""
        # 数据处理智能体
        data_config = AgentConfig(
            name="NLPDataProcessor",
            role=AgentRole.DEVELOPER,
            description="NLP数据预处理专家"
        )
        self.agents["data_processor"] = NLPDataAgent(data_config)

        # 模型开发智能体
        model_config = AgentConfig(
            name="NLPModelDeveloper",
            role=AgentRole.DEVELOPER,
            description="NLP模型开发专家"
        )
        self.agents["model_developer"] = NLPModelAgent(model_config)

        # 评估智能体
        eval_config = AgentConfig(
            name="NLPEvaluator",
            role=AgentRole.EVALUATOR,
            description="NLP模型评估专家"
        )
        self.agents["evaluator"] = NLPEvaluationAgent(eval_config)

    async def execute_workflow(self, input_data: Dict[str, Any]) -> Dict[str, Any]:
        """执行NLP研究工作流"""
        task_type = input_data.get("task_type", "text_classification")
        dataset = input_data.get("dataset")

        workflow_results = {}

        # 步骤1: 数据分析和预处理
        data_task = {
            "dataset": dataset,
            "task_type": task_type,
            "preprocessing_options": input_data.get("preprocessing", {})
        }

        data_results = await self.agents["data_processor"].process_task(data_task)
        workflow_results["data_processing"] = data_results

        # 步骤2: 模型开发
        model_task = {
            "processed_data": data_results["processed_dataset"],
            "task_type": task_type,
            "model_constraints": input_data.get("model_constraints", {}),
            "performance_targets": input_data.get("targets", {})
        }

        model_results = await self.agents["model_developer"].process_task(model_task)
        workflow_results["model_development"] = model_results

        # 步骤3: 模型评估
        eval_task = {
            "models": model_results["candidate_models"],
            "test_data": data_results["test_dataset"],
            "evaluation_metrics": input_data.get("metrics", ["accuracy", "f1", "precision", "recall"])
        }

        eval_results = await self.agents["evaluator"].process_task(eval_task)
        workflow_results["model_evaluation"] = eval_results

        # 步骤4: 生成最终模型和部署建议
        final_recommendations = await self._generate_model_recommendations(
            workflow_results
        )

        return {
            "status": "success",
            "workflow_results": workflow_results,
            "best_model": eval_results["best_model"],
            "deployment_recommendations": final_recommendations
        }

🧪 第五部分:测试框架开发

5.1 智能体单元测试

import pytest
import asyncio
from unittest.mock import Mock, patch, AsyncMock
from rdagent.agents.custom import CustomResearchAgent
from rdagent.core.config import AgentConfig

class TestCustomResearchAgent:
    """自定义研究智能体测试"""

    @pytest.fixture
    def agent_config(self):
        """智能体配置fixture"""
        return AgentConfig(
            name="TestResearcher",
            role=AgentRole.RESEARCHER,
            description="测试用研究智能体"
        )

    @pytest.fixture
    def research_agent(self, agent_config):
        """研究智能体fixture"""
        return CustomResearchAgent(agent_config)

    @pytest.mark.asyncio
    async def test_process_basic_research_task(self, research_agent):
        """测试基础研究任务处理"""
        task = {
            "id": "test_001",
            "question": "机器学习在医疗诊断中的应用",
            "domain": "healthcare"
        }

        with patch.object(research_agent, '_search_literature') as mock_search:
            mock_search.return_value = [
                {"title": "ML in Healthcare", "relevance": 0.9}
            ]

            result = await research_agent.process_task(task)

            assert result["status"] == "success"
            assert "research_question" in result
            assert "literature_review" in result
            mock_search.assert_called_once()

    @pytest.mark.asyncio
    async def test_literature_search_integration(self, research_agent):
        """测试文献搜索集成"""
        query = "artificial intelligence healthcare"

        # 模拟外部API调用
        with patch('aiohttp.ClientSession.get') as mock_get:
            mock_response = AsyncMock()
            mock_response.status = 200
            mock_response.json.return_value = {
                "data": [
                    {
                        "title": "AI in Healthcare Review",
                        "authors": [{"name": "Dr. Smith"}],
                        "abstract": "This paper reviews AI applications...",
                        "year": 2023
                    }
                ]
            }
            mock_get.return_value.__aenter__.return_value = mock_response

            papers = await research_agent._search_literature({"keywords": [query]})

            assert len(papers) > 0
            assert papers[0]["title"] == "AI in Healthcare Review"

    def test_tool_initialization(self, research_agent):
        """测试工具初始化"""
        assert len(research_agent.tools) > 0

        tool_names = [tool.__class__.__name__ for tool in research_agent.tools]
        assert "PaperSearchTool" in tool_names
        assert "DataAnalysisTool" in tool_names
        assert "VisualizationTool" in tool_names

    @pytest.mark.asyncio
    async def test_error_handling(self, research_agent):
        """测试错误处理"""
        # 测试无效任务数据
        invalid_task = {"invalid": "data"}

        result = await research_agent.process_task(invalid_task)

        assert result["status"] == "error"
        assert "error" in result

    @pytest.mark.parametrize("domain,expected_methodology", [
        ("healthcare", ["clinical_trials", "meta_analysis"]),
        ("finance", ["time_series", "econometric_analysis"]),
        ("general", ["literature_review", "case_study"])
    ])
    def test_domain_specific_methodology(self, research_agent, domain, expected_methodology):
        """测试领域特定方法论建议"""
        suggestions = research_agent._suggest_methodology("test question", domain)

        for method in expected_methodology:
            assert any(method in suggestion for suggestion in suggestions)

5.2 场景集成测试

class TestBiotechResearchScenario:
    """生物技术研究场景集成测试"""

    @pytest.fixture
    def scenario_config(self):
        """场景配置fixture"""
        return ScenarioConfig(
            name="TestBiotechScenario",
            description="测试用生物技术研究场景",
            agents=["researcher", "experimenter", "analyst"],
            workflow_steps=["literature_review", "experiment_design", "data_analysis"],
            evaluation_metrics=["innovation", "feasibility", "ethics"]
        )

    @pytest.fixture
    async def biotech_scenario(self, scenario_config):
        """生物技术场景fixture"""
        scenario = BiotechResearchScenario(scenario_config)
        await scenario.initialize_agents()
        return scenario

    @pytest.mark.asyncio
    @pytest.mark.integration
    async def test_full_workflow_execution(self, biotech_scenario):
        """测试完整工作流执行"""
        input_data = {
            "research_question": "CRISPR基因编辑在癌症治疗中的安全性研究",
            "resources": {
                "budget": 500000,
                "timeline_months": 12,
                "team_size": 5
            },
            "constraints": ["ethical_approval", "regulatory_compliance"]
        }

        result = await biotech_scenario.execute_workflow(input_data)

        assert result["status"] == "success"
        assert "workflow_results" in result
        assert "literature_review" in result["workflow_results"]
        assert "experiment_design" in result["workflow_results"]
        assert "integrated_insights" in result

    @pytest.mark.asyncio
    async def test_agent_coordination(self, biotech_scenario):
        """测试智能体协调"""
        # 验证智能体间的数据传递
        mock_research_result = {
            "status": "success",
            "key_findings": ["finding1", "finding2"],
            "research_gaps": ["gap1", "gap2"]
        }

        with patch.object(biotech_scenario.agents["researcher"],
                         'process_task', return_value=mock_research_result):

            input_data = {"research_question": "test question"}
            result = await biotech_scenario.execute_workflow(input_data)

            # 验证研究结果被正确传递给下游智能体
            assert result["workflow_results"]["literature_review"] == mock_research_result

    @pytest.mark.asyncio
    async def test_evaluation_metrics(self, biotech_scenario):
        """测试评估指标"""
        mock_results = {
            "workflow_results": {
                "literature_review": {"papers_found": 50, "key_insights": ["insight1"]},
                "experiment_design": {"feasibility_score": 0.8, "cost_estimate": 100000}
            }
        }

        evaluation = await biotech_scenario.evaluate_results(mock_results)

        assert "evaluation_metrics" in evaluation
        assert "overall_score" in evaluation
        assert 0 <= evaluation["overall_score"] <= 1
        assert "recommendations" in evaluation

5.3 性能测试

class TestPerformance:
    """性能测试"""

    @pytest.mark.asyncio
    @pytest.mark.slow
    async def test_concurrent_agent_execution(self):
        """测试并发智能体执行性能"""
        import time

        # 创建多个智能体实例
        agents = []
        for i in range(5):
            config = AgentConfig(
                name=f"Agent_{i}",
                role=AgentRole.RESEARCHER,
                description=f"Test agent {i}"
            )
            agents.append(CustomResearchAgent(config))

        # 准备任务
        tasks = [
            {
                "id": f"task_{i}",
                "question": f"Research question {i}",
                "domain": "test"
            }
            for i in range(5)
        ]

        # 并发执行
        start_time = time.time()
        results = await asyncio.gather(*[
            agent.process_task(task)
            for agent, task in zip(agents, tasks)
        ])
        end_time = time.time()

        execution_time = end_time - start_time

        # 验证结果
        assert len(results) == 5
        assert all(result["status"] == "success" for result in results)

        # 性能断言(根据实际情况调整)
        assert execution_time < 30  # 应在30秒内完成

        print(f"并发执行时间: {execution_time:.2f} 秒")

    @pytest.mark.asyncio
    async def test_memory_usage(self, research_agent):
        """测试内存使用"""
        import psutil
        import gc

        process = psutil.Process()
        initial_memory = process.memory_info().rss / 1024 / 1024  # MB

        # 执行多个任务
        for i in range(10):
            task = {
                "id": f"memory_test_{i}",
                "question": f"Memory test question {i}",
                "domain": "test"
            }
            await research_agent.process_task(task)

        # 强制垃圾回收
        gc.collect()

        final_memory = process.memory_info().rss / 1024 / 1024  # MB
        memory_increase = final_memory - initial_memory

        # 内存增长应该在合理范围内
        assert memory_increase < 100  # 不应超过100MB

        print(f"内存使用: {initial_memory:.2f}MB -> {final_memory:.2f}MB (+{memory_increase:.2f}MB)")

📝 第六部分:文档和社区贡献

6.1 API文档生成

使用Sphinx自动生成API文档:

# docs/conf.py
import os
import sys
sys.path.insert(0, os.path.abspath('..'))

extensions = [
    'sphinx.ext.autodoc',
    'sphinx.ext.viewcode',
    'sphinx.ext.napoleon',
    'myst_parser'
]

html_theme = 'sphinx_rtd_theme'

# API文档配置
autodoc_default_options = {
    'members': True,
    'member-order': 'bysource',
    'special-members': '__init__',
    'undoc-members': True,
    'exclude-members': '__weakref__'
}

6.2 贡献指南

代码贡献流程

# 1. Fork仓库并克隆
git clone https://github.com/your-username/RD-Agent.git
cd RD-Agent

# 2. 创建开发分支
git checkout -b feature/your-feature-name

# 3. 安装开发环境
python tutorial_examples/chapter6/development_setup.py

# 4. 进行开发
# ... 编写代码 ...

# 5. 运行测试
pytest tests/

# 6. 代码检查
black .
isort .
flake8 .
mypy .

# 7. 提交代码
git add .
git commit -m "feat(agent): add new research agent capability"

# 8. 推送分支
git push origin feature/your-feature-name

# 9. 创建Pull Request
gh pr create --title "Add new research agent capability" --body "..."

提交消息规范

type(scope): description

[optional body]

[optional footer]

类型说明: - feat: 新功能 - fix: 错误修复 - docs: 文档更新 - style: 代码格式化 - refactor: 代码重构 - test: 测试相关 - chore: 构建/工具相关

6.3 代码审查清单

Pull Request检查清单

  • [ ] 功能完整性
  • [ ] 功能按预期工作
  • [ ] 处理边界情况
  • [ ] 错误处理完善

  • [ ] 代码质量

  • [ ] 代码清晰易读
  • [ ] 遵循项目规范
  • [ ] 无重复代码

  • [ ] 测试覆盖

  • [ ] 添加单元测试
  • [ ] 添加集成测试
  • [ ] 测试覆盖率>80%

  • [ ] 文档完善

  • [ ] API文档完整
  • [ ] 使用示例清晰
  • [ ] 更新CHANGELOG

  • [ ] 性能考虑

  • [ ] 无性能回归
  • [ ] 内存使用合理
  • [ ] 异步操作正确

6.4 发布流程

版本发布检查清单

# 1. 更新版本号
echo "1.2.0" > VERSION

# 2. 更新CHANGELOG
# 编辑CHANGELOG.md,添加新版本内容

# 3. 运行完整测试套件
pytest tests/ --cov=rdagent --cov-report=html

# 4. 构建文档
cd docs/
make html

# 5. 创建发布标签
git tag -a v1.2.0 -m "Release version 1.2.0"

# 6. 推送标签
git push origin v1.2.0

# 7. 创建GitHub Release
gh release create v1.2.0 --title "RD-Agent v1.2.0" --notes-file RELEASE_NOTES.md

🎯 第七部分:最佳实践与总结

7.1 开发最佳实践

智能体开发原则

  1. 单一职责:每个智能体专注一个领域
  2. 松耦合:智能体间通过标准接口通信
  3. 可测试性:所有功能都要有对应测试
  4. 可扩展性:支持插件和钩子机制
  5. 错误恢复:优雅处理异常情况

代码组织结构

your_extension/
├── agents/                 # 自定义智能体
│   ├── __init__.py
│   ├── research_agent.py
│   └── development_agent.py
├── scenarios/              # 自定义场景
│   ├── __init__.py
│   └── custom_scenario.py
├── plugins/                # 插件
│   ├── __init__.py
│   └── custom_plugin.py
├── tools/                  # 工具
│   ├── __init__.py
│   └── custom_tools.py
├── tests/                  # 测试
│   ├── test_agents.py
│   ├── test_scenarios.py
│   └── test_plugins.py
├── docs/                   # 文档
│   └── README.md
├── setup.py               # 安装配置
└── requirements.txt       # 依赖列表

7.2 性能优化建议

  1. 异步编程:使用async/await处理I/O密集型操作
  2. 连接池:复用数据库和API连接
  3. 缓存策略:缓存频繁访问的数据
  4. 批处理:合并相似的API调用
  5. 资源限制:设置合理的并发限制

7.3 安全考虑

  1. API密钥管理:使用环境变量或密钥管理服务
  2. 输入验证:验证所有外部输入
  3. 权限控制:实施最小权限原则
  4. 审计日志:记录敏感操作
  5. 依赖安全:定期更新依赖包

7.4 社区参与

贡献方式

  1. 报告问题:提交bug报告和功能请求
  2. 代码贡献:修复bug和添加新功能
  3. 文档改进:改善文档和示例
  4. 社区支持:帮助其他用户解决问题
  5. 推广宣传:分享使用经验和最佳实践

获得帮助

  • 📖 官方文档:查阅最新的API文档和教程
  • 💬 GitHub Discussions:参与社区讨论
  • 🐛 Issue Tracker:报告问题和查看已知问题
  • 📧 邮件列表:订阅开发者邮件列表
  • 🎥 视频教程:观看官方和社区制作的教程

🎉 结语

恭喜您完成了RD-Agent扩展开发教程!通过本章的学习,您应该已经掌握了:

  • RD-Agent的内部架构和扩展点
  • 如何开发自定义智能体和场景
  • 插件系统的使用和开发
  • 测试框架和最佳实践
  • 社区贡献的流程和规范

现在您可以:

  1. 🔨 开始开发:使用提供的模板创建自己的扩展
  2. 🧪 进行测试:确保您的代码质量和稳定性
  3. 📚 完善文档:为您的扩展编写清晰的文档
  4. 🤝 参与社区:与其他开发者分享经验和协作

RD-Agent是一个快速发展的开源项目,我们期待您的参与和贡献!


📞 联系我们

  • 项目主页:https://github.com/microsoft/RD-Agent
  • 文档站点:https://rd-agent.readthedocs.io
  • 社区论坛:https://github.com/microsoft/RD-Agent/discussions
  • 问题报告:https://github.com/microsoft/RD-Agent/issues

感谢您选择RD-Agent,祝您开发愉快!🚀