Skip to content

29.6 整合與部署

python
## 29.6.1 系统集成

### 整体集成架构

class IntegratedCodingAgent: """集成的编程 Agent"""

def **init**(self, config: AgentConfig): self.config = config

# 初始化核心组件

self.llm_client = LLMClient(config.llm_config) self.tool_manager = ToolManager() self.memory_system = MemorySystem(config.memory_config)

# 初始化能力模块

self.code_generator = CodeGenerator(self.llm_client) self.code_understander = CodeUnderstander(self.llm_client) self.debugger = DebuggingWorkflow(self.llm_client, self.tool_manager)

# 初始化交互管理

self.session_manager = SessionManager() self.context_manager = ContextManager()

# 初始化工具

self._initialize_tools()

# 初始化插件系统

self.plugin_manager = PluginManager()

# 初始化监控

self.monitor = PerformanceMonitor()

def _initialize_tools(self): """初始化工具"""

# 文件操作工具

self.tool_manager.register_tool(FileReadTool()) self.tool_manager.register_tool(FileWriteTool()) self.tool_manager.register_tool(FileSearchTool())

# 代码执行工具

self.tool_manager.register_tool(CodeExecuteTool()) self.tool_manager.register_tool(TestRunnerTool())

# 版本控制工具

self.tool_manager.register_tool(GitTool())

# 搜索工具

self.tool_manager.register_tool(SearchTool())

# 文档工具

self.tool_manager.register_tool(DocumentationTool())

async def process_request(self, request: UserRequest) -> AgentResponse: """處理使用者請求"""

yaml
# 开始监控

self.monitor.start_timer('request_processing')

try:

# 1\. 获取或创建会话

session = self.session_manager.get_or_create_session( request.session_id )

# 2\. 构建上下文

context = await self.context_manager.build_context( request, session )

# 3\. 理解意图

intent = await self._understand_intent(request, context)

# 4\. 根据意图选择处理流程

if intent.type == 'code_generation': response = await self._handle_code_generation( request, context, intent ) elif intent.type == 'code_understanding': response = await self._handle_code_understanding( request, context, intent ) elif intent.type == 'debugging': response = await self._handle_debugging( request, context, intent ) else: response = await self._handle_general_request( request, context, intent )

# 5\. 更新会话

session.add_interaction(request, response)

# 6\. 存储到记忆系统

await self.memory_system.store_interaction(request, response)

return response

python
except Exception as e: logger.error(f"Error processing request: {e}") return AgentResponse( text=f"An error occurred: {str(e)}", success=False, error=str(e) ) finally:

# 停止监控

duration = self.monitor.stop_timer('request_processing') self.monitor.record_metric('request_duration', duration)

async def _understand_intent(self, request: UserRequest, context: Context) -> Intent: """理解用户意图""" prompt = f""" 分析用户请求的意图:

用户请求:{request.text} 上下文:{context}

請識別:

  1. 意圖型別(程式碼生成、程式碼理解、除錯、最佳化等)
  2. 具體任務
  3. 相關的程式語言
  4. 需要的工具

以 JSON 格式返回。 """

response = await self.llm_client.complete(prompt) return self._parse_intent(response)

async def _handle_code_generation(self, request: UserRequest, context: Context, intent: Intent) -> AgentResponse: """處理程式碼生成請求"""

# 提取需求

requirement = await self.code_generator.extract_requirement( request.text )

# 设计架构

architecture = await self.code_generator.design_architecture( requirement )

# 生成代码

generated_code = await self.code_generator.generate_code( architecture, requirement )

# 验证代码

validation_result = await self.code_generator.validate_code( generated_code, requirement )

# 生成响应

response_text = await self._generate_code_generation_response( requirement, architecture, generated_code, validation_result )

return AgentResponse( text=response_text, success=validation_result.passed, data={ 'requirement': requirement, 'architecture': architecture, 'code': generated_code, 'validation': validation_result } )

async def _handle_code_understanding(self, request: UserRequest, context: Context, intent: Intent) -> AgentResponse: """處理程式碼理解請求"""

# 解析代码

parsed_code = await self.code_understander.parse_code( request.code )

# 分析结构

structure = await self.code_understander.analyze_structure( parsed_code )

# 分析语义

semantic = await self.code_understander.analyze_semantic( parsed_code, structure )

# 生成解释

explanation = await self.code_understander.generate_explanation( parsed_code, structure, semantic )

# 生成响应

response_text = await self._generate_code_understanding_response( parsed_code, structure, semantic, explanation )

return AgentResponse( text=response_text, success=True, data={ 'parsed_code': parsed_code, 'structure': structure, 'semantic': semantic, 'explanation': explanation } )

async def _handle_debugging(self, request: UserRequest, context: Context, intent: Intent) -> AgentResponse: """處理除錯請求"""

执行调试工作流

debugging_result = await self.debugger.debug( request.code, request.execution_result )

生成响应

response_text = await self._generate_debugging_response( debugging_result )

return AgentResponse( text=response_text, success=True, data={ 'debugging_result': debugging_result } )

async def _handle_general_request(self, request: UserRequest, context: Context, intent: Intent) -> AgentResponse: """處理一般請求"""

使用 LLM 直接处理

prompt = f""" 处理用户请求:

用户请求:{request.text} 上下文:

請提供有幫助的響應。 """

python
response_text = await self.llm_client.complete(prompt)

return AgentResponse( text=response_text, success=True )

## 29.6.2 API 服务

### REST API 实现

    bash


    python

    from fastapi import FastAPI, HTTPException
    from pydantic import BaseModel

    app = FastAPI(title="Coding Agent API")

    # 初始化 Agent
    agent = IntegratedCodingAgent(config=load_config())

    class CodeGenerationRequest(BaseModel):
        prompt: str
        language: str = "python"
        session_id: str = None

    class CodeUnderstandingRequest(BaseModel):
        code: str
        language: str = "python"
        session_id: str = None

    class DebuggingRequest(BaseModel):
        code: str
        error_message: str = None
        session_id: str = None

    @app.post("/api/v1/generate")
    async def generate_code(request: CodeGenerationRequest):
        """生成代码"""
        try:
            user_request = UserRequest(
                text=request.prompt,
                session_id=request.session_id or str(uuid.uuid4()),
                type='code_generation'
            )

            response = await agent.process_request(user_request)

            return {
                'success': response.success,
                'response': response.text,
                'data': response.data
            }
        except Exception as e:
            raise HTTPException(status_code=500, detail=str(e))

    @app.post("/api/v1/understand")
    async def understand_code(request: CodeUnderstandingRequest):
        """理解代码"""
        try:
            user_request = UserRequest(
                text="Explain this code",
                code=request.code,
                session_id=request.session_id or str(uuid.uuid4()),
                type='code_understanding'
            )

            response = await agent.process_request(user_request)

            return {
                'success': response.success,
                'response': response.text,
                'data': response.data
            }
        except Exception as e:
            raise HTTPException(status_code=500, detail=str(e))

    @app.post("/api/v1/debug")
    async def debug_code(request: DebuggingRequest):
        """调试代码"""
        try:
            execution_result = ExecutionResult(
                error=request.error_message
            ) if request.error_message else None

            user_request = UserRequest(
                text="Debug this code",
                code=request.code,
                execution_result=execution_result,
                session_id=request.session_id or str(uuid.uuid4()),
                type='debugging'
            )

            response = await agent.process_request(user_request)

            return {
                'success': response.success,
                'response': response.text,
                'data': response.data
            }
        except Exception as e:
            raise HTTPException(status_code=500, detail=str(e))

    @app.get("/api/v1/health")
    async def health_check():
        """健康检查"""
        return {
            'status': 'healthy',
            'timestamp': datetime.utcnow().isoformat()
        }

    @app.get("/api/v1/stats")
    async def get_stats():
        """获取统计信息"""
        return {
            'performance': agent.monitor.get_stats(),
            'sessions': agent.session_manager.get_stats(),
            'memory': agent.memory_system.get_stats()
        }

    ## 29.6.3 CLI 工具

    ### 命令行接口

    import click
    @click.group()
    def cli():
    """Coding Agent CLI"""
    pass
    @cli.command()
    @click.argument('prompt')
    @click.option('--language', default='python', help='Programming language')
    @click.option('--output', '-o', help='Output file')
    def generate(prompt, language, output):
    """Generate code from prompt"""
    async def generate_code():
    agent = IntegratedCodingAgent(config=load_config())
    request = UserRequest(
    text=prompt,
    type='code_generation'
    )
    response = await agent.process_request(request)
    if response.success and response.data:
    code = response.data.get('code', {}).get('full_code', '')
    if output:
    with open(output, 'w') as f:
    f.write(code)
    click.echo(f"Code generated and saved to {output}")
    else:
    click.echo(code)
    else:
    click.echo(f"Error: {response.text}")
    asyncio.run(generate_code())
    @cli.command()
    @click.argument('file', type=click.Path(exists=True))
    def understand(file):
    """Understand code"""
    async def understand_code():
    agent = IntegratedCodingAgent(config=load_config())
    with open(file, 'r') as f:
    code = f.read()
    request = UserRequest(
    text="Explain this code",
    code=code,
    type='code_understanding'
    )
    response = await agent.process_request(request)
    if response.success:
    click.echo(response.text)
    else:
    click.echo(f"Error: {response.text}")
    asyncio.run(understand_code())
    @cli.command()
    @click.argument('file', type=click.Path(exists=True))
    @click.option('--error', help='Error message')
    def debug(file, error):
    """Debug code"""
    async def debug_code():
    agent = IntegratedCodingAgent(config=load_config())
    with open(file, 'r') as f:
    code = f.read()
    execution_result = ExecutionResult(error=error) if error else None
    request = UserRequest(
    text="Debug this code",
    code=code,
    execution_result=execution_result,
    type='debugging'
    )
    response = await agent.process_request(request)
    if response.success:
    click.echo(response.text)
    else:
    click.echo(f"Error: {response.text}")
    asyncio.run(debug_code())
    @cli.command()
    def interactive():
    """Interactive mode"""
    async def interactive_mode():
    agent = IntegratedCodingAgent(config=load_config())
    session_id = str(uuid.uuid4())
    click.echo("Coding Agent Interactive Mode")
    click.echo("Type 'exit' to quit")
    click.echo()
    while True:
    user_input = click.prompt("You")
    if user_input.lower() == 'exit':
    break
    request = UserRequest(
    text=user_input,
    session_id=session_id
    )
    response = await agent.process_request(request)
    click.echo(f"Agent: {response.text}")
    click.echo()
    asyncio.run(interactive_mode())
    if __name__ == '__main__':
    cli()

## 29.6.4 部署方案

### Docker 部署

    bash


    dockerfile

    # Dockerfile
    FROM python:3.11-slim

    WORKDIR /app

    # 安装依赖
    COPY requirements.txt .
    RUN pip install --no-cache-dir -r requirements.txt

    # 复制代码
    COPY . .

    # 暴露端口
    EXPOSE 8000

    # 启动服务
    CMD ["uvicorn", "api:app", "--host", "0.0.0.0", "--port", "8000"]

    # docker-compose.yml
    version: '3.8'
    services:
    agent:
    build: .
    ports:
    - "8000:8000"
    environment:
    - LLM_API_KEY=${LLM_API_KEY}
    - LLM_BASE_URL=${LLM_BASE_URL}
    volumes:
    - ./data:/app/data
    restart: unless-stopped
    redis:
    image: redis:alpine
    ports:
    - "6379:6379"
    restart: unless-stopped
    postgres:
    image: postgres:15-alpine
    environment:
    - POSTGRES_DB=coding_agent
    - POSTGRES_USER=agent
    - POSTGRES_PASSWORD=password
    ports:
    - "5432:5432"
    volumes:
    - postgres_data:/var/lib/postgresql/data
    restart: unless-stopped
    volumes:
    postgres_data:

### Kubernetes 部署

    bash


    yaml

    # deployment.yaml
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: coding-agent
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: coding-agent
      template:
        metadata:
          labels:
            app: coding-agent
        spec:
          containers:
          - name: agent
            image: coding-agent:latest
            ports:
            - containerPort: 8000
            env:
            - name: LLM_API_KEY
              valueFrom:
                secretKeyRef:
                  name: agent-secrets
                  key: llm-api-key
            - name: LLM_BASE_URL
              valueFrom:
                configMapKeyRef:
                  name: agent-config
                  key: llm-base-url
            resources:
              requests:
                memory: "512Mi"
                cpu: "500m"
              limits:
                memory: "2Gi"
                cpu: "2000m"
            livenessProbe:
              httpGet:
                path: /api/v1/health
                port: 8000
              initialDelaySeconds: 30
              periodSeconds: 10
            readinessProbe:
              httpGet:
                path: /api/v1/health
                port: 8000
              initialDelaySeconds: 5
              periodSeconds: 5

    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: coding-agent-service
    spec:
      selector:
        app: coding-agent
      ports:
      - protocol: TCP
        port: 80
        targetPort: 8000
      type: LoadBalancer

    ---
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: agent-config
    data:
      llm-base-url: "https://api.anthropic.com"

    ---
    apiVersion: v1
    kind: Secret
    metadata:
      name: agent-secrets
    type: Opaque
    data:
      llm-api-key: <base64-encoded-key>

    ## 29.6.5 监控与日志

    ### 监控配置

    from prometheus_client import Counter, Histogram, Gauge
    # 定义指标
    request_counter = Counter(
    'agent_requests_total',
    'Total number of requests',
    ['type', 'status']
    )
    request_duration = Histogram(
    'agent_request_duration_seconds',
    'Request duration',
    ['type']
    )
    active_sessions = Gauge(
    'agent_active_sessions',
    'Number of active sessions'
    )
    cache_hits = Counter(
    'agent_cache_hits_total',
    'Total cache hits'
    )
    cache_misses = Counter(
    'agent_cache_misses_total',
    'Total cache misses'
    )
    # 在 Agent 中集成监控
    class MonitoredAgent(IntegratedCodingAgent):
    """带监控的 Agent"""
    async def process_request(self, request: UserRequest) -> AgentResponse:
    # 记录请求开始
    request_counter.labels(
    type=request.type,
    status='processing'
    ).inc()
    with request_duration.labels(type=request.type).time():
    try:
    response = await super().process_request(request)
    # 记录成功
    request_counter.labels(
    type=request.type,
    status='success' if response.success else 'error'
    ).inc()
    return response
    except Exception as e:
    # 记录错误
    request_counter.labels(
    type=request.type,
    status='error'
    ).inc()
    raise

### 日志配置

    bash


    python

    import logging
    from logging.handlers import RotatingFileHandler

    def setup_logging():
        """配置日志"""
        logger = logging.getLogger('coding_agent')
        logger.setLevel(logging.INFO)

        # 控制台处理器
        console_handler = logging.StreamHandler()
        console_handler.setLevel(logging.INFO)
        console_format = logging.Formatter(
            '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
        )
        console_handler.setFormatter(console_format)
        logger.addHandler(console_handler)

        # 文件处理器
        file_handler = RotatingFileHandler(
            'agent.log',
            maxBytes=10*1024*1024,  # 10MB
            backupCount=5
        )
        file_handler.setLevel(logging.DEBUG)
        file_format = logging.Formatter(
            '%(asctime)s - %(name)s - %(levelname)s - %(funcName)s:%(lineno)d - %(message)s'
        )
        file_handler.setFormatter(file_format)
        logger.addHandler(file_handler)

        return logger

    # 在 Agent 中使用
    logger = setup_logging()

    class LoggingAgent(IntegratedCodingAgent):
        """带日志的 Agent"""

        async def process_request(self, request: UserRequest) -> AgentResponse:
            logger.info(f"Processing request: {request.type}")

            try:
                response = await super().process_request(request)

                if response.success:
                    logger.info(f"Request completed successfully")
                else:
                    logger.warning(f"Request completed with errors: {response.error}")

                return response
            except Exception as e:
                logger.error(f"Request failed: {e}", exc_info=True)
                raise

## 29.6.6 最佳实践

### 1\. 安全性
  • 使用環境變數儲存敏感資訊
  • 實現請求認證和授權
  • 限制程式碼執行許可權
  • 輸入驗證和清理

2. 可擴充套件性

  • 使用非同步處理提高併發
  • 實現快取機制減少重複計算
  • 支援水平擴充套件
  • 使用訊息佇列處理非同步任務

3. 可靠性

  • 實現重試機制
  • 使用斷路器模式
  • 健康檢查和自動恢復
  • 資料持久化和備份

4. 可觀測性

  • 完善的日誌記錄
  • 效能監控和告警
  • 分散式追蹤
  • 指標收集和分析

透過合理的整合和部署,我們可以將程式設計 Agent 部署到生產環境,為使用者提供穩定、高效的程式設計輔助服務。

基于 MIT 许可发布 | 永久导航