Skip to content

RD-Agent Tutorial - Chapter 6: Extension Development and Community Contribution

📋 Chapter Overview

Chapter 6 covers how to extend RD-Agent's functionality and how to contribute to the open-source community. This chapter is aimed at developers who want to deeply customize RD-Agent or contribute to the project.

🎯 Chapter Goals

  • ✅ Master plugin development: create custom plugins and extension modules
  • ✅ Understand the architecture design: gain deep insight into RD-Agent's internal architecture
  • ✅ Learn to contribute code: participate in developing and maintaining the open-source project
  • ✅ Set up a development environment: build a local environment for development
  • ✅ Follow development conventions: learn coding standards and best practices

📁 Chapter Structure

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                     # 插件模板

🔧 Part 1: Architecture Understanding and Development Environment

1.1 In-Depth Analysis of the RD-Agent Architecture

RD-Agent adopts a modular multi-agent architecture consisting of the following core components:

Core Architecture Diagram

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

Key Extension Points

  1. Agent level: inherit from base agent classes to create domain-specific agents
  2. Tool level: develop new tools to enhance agent capabilities
  3. Scenario level: define new application scenarios and workflows
  4. Plugin level: create plugins to extend core functionality

1.2 Development Environment Setup Guide

System Requirements

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

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

Quick Environment Setup

Use the automation script we provide:

# 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安装成功!')"

Development Tool Configuration

VS Code Configuration

{
  "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 Workflow Configuration

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

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

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


🤖 Part 2: Custom Agent Development

2.1 Base Agent Class Explained

All agents in RD-Agent inherit from the base agent class. Understanding this base class design is the key to developing custom agents.

Base Agent Interface

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 Research Agent Development Example

Using a biomedical research agent as an example, here is the complete development process:

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 Development Agent Development Example

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"]
        }

🔌 Part 3: Plugin System Development

3.1 Plugin Architecture Design

RD-Agent's plugin system is based on an event-driven architecture that allows custom logic to be injected at key nodes.

Plugin Lifecycle

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

3.2 Data Connector Plugin Example

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 Model Optimization Plugin Example

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 Notification Plugin Example

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)

🎨 Part 4: Custom Scenario Development

4.1 Scenario Architecture Design

A Scenario defines the complete workflow for a specific domain, including agent collaboration patterns, data flows, and evaluation criteria.

Scenario Base Class

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 Biotech Research Scenario

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 Quantitative Finance Scenario

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 Research Scenario

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
        }

🧪 Part 5: Test Framework Development

5.1 Agent Unit Testing

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 Scenario Integration Testing

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 Performance Testing

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)")

📝 Part 6: Documentation and Community Contribution

6.1 API Documentation Generation

Use Sphinx to automatically generate API documentation:

# 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 Contribution Guide

Code Contribution Workflow

# 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 "..."

Commit Message Conventions

type(scope): description

[optional body]

[optional footer]

Type descriptions: - feat: new feature - fix: bug fix - docs: documentation update - style: code formatting - refactor: code refactoring - test: test-related - chore: build/tooling-related

6.3 Code Review Checklist

Pull Request Checklist

  • [ ] Functional completeness
  • [ ] Feature works as expected
  • [ ] Edge cases handled
  • [ ] Error handling is thorough

  • [ ] Code quality

  • [ ] Code is clear and readable
  • [ ] Follows project conventions
  • [ ] No duplicated code

  • [ ] Test coverage

  • [ ] Unit tests added
  • [ ] Integration tests added
  • [ ] Test coverage > 80%

  • [ ] Documentation completeness

  • [ ] API documentation is complete
  • [ ] Usage examples are clear
  • [ ] CHANGELOG updated

  • [ ] Performance considerations

  • [ ] No performance regressions
  • [ ] Reasonable memory usage
  • [ ] Asynchronous operations are correct

6.4 Release Process

Version Release Checklist

# 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

🎯 Part 7: Best Practices and Summary

7.1 Development Best Practices

Agent Development Principles

  1. Single responsibility: each agent focuses on one domain
  2. Loose coupling: agents communicate through standard interfaces
  3. Testability: every feature has corresponding tests
  4. Extensibility: support plugin and hook mechanisms
  5. Error recovery: handle exceptions gracefully

Code Organization Structure

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 Performance Optimization Tips

  1. Asynchronous programming: use async/await for I/O-intensive operations
  2. Connection pooling: reuse database and API connections
  3. Caching strategy: cache frequently accessed data
  4. Batching: merge similar API calls
  5. Resource limits: set reasonable concurrency limits

7.3 Security Considerations

  1. API key management: use environment variables or a key management service
  2. Input validation: validate all external input
  3. Access control: apply the principle of least privilege
  4. Audit logging: record sensitive operations
  5. Dependency security: update dependency packages regularly

7.4 Community Participation

Ways to Contribute

  1. Report issues: submit bug reports and feature requests
  2. Code contributions: fix bugs and add new features
  3. Documentation improvements: improve documentation and examples
  4. Community support: help other users solve problems
  5. Promotion: share usage experiences and best practices

Getting Help

  • 📖 Official documentation: consult the latest API docs and tutorials
  • 💬 GitHub Discussions: join community discussions
  • 🐛 Issue Tracker: report issues and check known problems
  • 📧 Mailing list: subscribe to the developer mailing list
  • 🎥 Video tutorials: watch tutorials produced officially and by the community

🎉 Conclusion

Congratulations on completing the RD-Agent extension development tutorial! Through this chapter, you should now have mastered:

  • RD-Agent's internal architecture and extension points
  • How to develop custom agents and scenarios
  • Using and developing the plugin system
  • Test frameworks and best practices
  • Community contribution workflows and conventions

Now you can:

  1. 🔨 Start developing: use the provided templates to create your own extensions
  2. 🧪 Run tests: ensure the quality and stability of your code
  3. 📚 Improve documentation: write clear documentation for your extensions
  4. 🤝 Join the community: share experiences and collaborate with other developers

RD-Agent is a fast-growing open-source project, and we look forward to your participation and contributions!


📞 Contact Us

  • Project homepage: https://github.com/microsoft/RD-Agent
  • Documentation site: https://rd-agent.readthedocs.io
  • Community forum: https://github.com/microsoft/RD-Agent/discussions
  • Issue reporting: https://github.com/microsoft/RD-Agent/issues

Thank you for choosing RD-Agent, and happy developing! 🚀