把 Agent 做成平台:可插拔工具、可配置角色与多租户隔离

张开发
2026/4/21 5:14:45 15 分钟阅读
把 Agent 做成平台:可插拔工具、可配置角色与多租户隔离
把 Agent 做成平台:可插拔工具、可配置角色与多租户隔离实战引言痛点引入如果你所在的团队最近一年一直在做AI Agent相关的项目,大概率会遇到这些糟心事:上个月刚为运营团队做了一个活动策划Agent,这个月产品团队要做一个用户反馈分析Agent,所有的工具对接、Prompt调试、权限逻辑都要重新写一遍,代码复用率不到30%;好不容易把两个Agent都上线了,又收到安全部门的警告:不同部门的Agent访问知识库的时候没有隔离,销售部的Agent居然能查到研发部的内部文档;更头疼的是,来了一个企业客户要私有化部署你的客服Agent,你不仅要改半天配置,还要担心客户自己改了工具之后会不会把整个系统搞崩。这些问题的本质,是我们还在用「单实例开发」的思路做Agent:每一个业务场景都从零搭建一个独立的Agent,所有能力都是耦合在业务代码里的,既没有复用性,也没有安全性,更谈不上规模化。解决方案概述解决这一系列问题的核心思路,就是把Agent能力平台化:打造一个具备可插拔工具体系、可配置角色引擎、企业级多租户隔离三大核心能力的Agent开发与运行平台,让业务方不需要写一行代码,只需要拖拽配置就能生成符合自己需求的Agent,同时保障不同租户、不同部门之间的数据完全隔离,资源互不影响。相比传统单实例Agent开发模式,平台化方案有以下优势:开发效率提升80%:工具、角色、记忆能力一次开发,全平台复用,不需要重复造轮子运维成本降低70%:只需要维护一套平台系统,不需要为每个业务场景单独部署安全合规性大幅提升:原生多租户隔离,工具执行沙箱防护,满足企业级数据安全要求生态扩展性强:支持第三方开发者接入工具和角色,快速形成生态最终效果展示本文最终实现的最小可行Agent平台,具备以下核心功能:工具市场:支持开发者一键注册、发布工具,用户可以一键安装到自己的Agent角色配置中心:可视化配置角色的Prompt、绑定工具、记忆规则、工作流租户管理控制台:支持创建租户、配置配额、管理租户用户、隔离租户数据用户只需要3分钟就可以配置一个专属的业务Agent,从创建租户、配置角色到上线使用全流程可视化操作。准备工作环境/工具依赖本文实现的Agent平台基于云原生技术栈,所需环境如下:依赖组件版本要求用途Python3.10+后端开发语言FastAPI0.100+后端API框架LangChain0.1.0+Agent底层框架PostgreSQL14+元数据存储Redis7.0+缓存、限流、会话存储MinIO最新版文件、向量数据存储Docker24.0+工具执行沙箱Kubernetes1.25+平台部署、资源调度Vue33.3+前端控制台开发前置知识读者需要具备以下基础知识:AI Agent的基本原理:了解Agent的核心组成(LLM、工具、记忆、规划)Python后端开发:熟悉FastAPI、SQLAlchemy等常用框架多租户架构基本概念:了解行级隔离、Schema隔离、物理隔离的差异权限系统常识:了解RBAC权限模型的基本原理相关学习资源:LangChain官方文档RBAC权限模型介绍多租户架构最佳实践平台整体架构设计边界与外延在正式开始设计之前,我们首先明确平台的边界,避免过度设计:核心能力边界(必须做)提供通用的LLM接入适配能力,支持主流公有云和开源LLM模型提供可插拔工具的注册、管理、执行、安全隔离能力提供可视化的角色配置、调试、发布、版本管理能力提供企业级的多租户隔离、权限管理、配额管控能力提供Agent的记忆管理、向量存储、工作流编排能力非核心边界(不做)不自研LLM大模型,只做LLM的适配层不开发垂直领域的业务工具,只提供工具的接入标准和执行环境不做具体的业务逻辑,只提供配置能力让业务方自己实现外延能力(可扩展)对接第三方工具市场,引入更多第三方工具对接第三方角色市场,支持开发者分享优秀角色配置支持Agent导出部署,可独立运行在边缘设备或其他平台支持多Agent协作,多个角色Agent配合完成复杂任务支持Agent监控、告警、数据分析,帮助优化Agent效果整体架构设计平台采用分层架构设计,各层之间解耦,便于扩展和维护,整体架构图如下:渲染错误:Mermaid 渲染失败: Parsing failed: Lexer error on line 2, column 18: unexpected character: -[- at offset: 35, skipped 5 characters. Lexer error on line 3, column 23: unexpected character: -[- at offset: 63, skipped 1 characters. Lexer error on line 3, column 27: unexpected character: -网- at offset: 67, skipped 3 characters. Lexer error on line 4, column 21: unexpected character: -[- at offset: 91, skipped 8 characters. Lexer error on line 5, column 15: unexpected character: -[- at offset: 114, skipped 7 characters. Lexer error on line 6, column 25: unexpected character: -[- at offset: 146, skipped 8 characters. Lexer error on line 7, column 25: unexpected character: -[- at offset: 179, skipped 8 characters. Lexer error on line 8, column 28: unexpected character: -[- at offset: 215, skipped 1 characters. Lexer error on line 8, column 34: unexpected character: -调- at offset: 221, skipped 5 characters. Lexer error on line 9, column 27: unexpected character: -[- at offset: 253, skipped 8 characters. Lexer error on line 10, column 21: unexpected character: -[- at offset: 282, skipped 6 characters. Lexer error on line 11, column 21: unexpected character: -[- at offset: 309, skipped 5 characters. Lexer error on line 12, column 28: unexpected character: -[- at offset: 342, skipped 1 characters. Lexer error on line 12, column 32: unexpected character: -接- at offset: 346, skipped 5 characters. Lexer error on line 13, column 28: unexpected character: -[- at offset: 379, skipped 8 characters. Lexer error on line 14, column 23: unexpected character: -[- at offset: 410, skipped 8 characters. Lexer error on line 15, column 23: unexpected character: -[- at offset: 441, skipped 8 characters. Lexer error on line 16, column 19: unexpected character: -[- at offset: 468, skipped 5 characters. Lexer error on line 20, column 20: unexpected character: -[- at offset: 584, skipped 1 characters. Lexer error on line 20, column 24: unexpected character: -计- at offset: 588, skipped 5 characters. Parse error on line 3, column 24: Expecting: one of these possible Token sequences: 1. [NEWLINE] 2. [EOF] but found: 'API' Parse error on line 3, column 30: Expecting token of type ':' but found ` `. Parse error on line 8, column 29: Expecting: one of these possible Token sequences: 1. [NEWLINE] 2. [EOF] but found: 'Agent' Parse error on line 8, column 39: Expecting token of type ':' but found ` `. Parse error on line 12, column 29: Expecting: one of these possible Token sequences: 1. [NEWLINE] 2. [EOF] but found: 'L' Parse error on line 12, column 37: Expecting token of type ':' but found ` `. Parse error on line 17, column 18: Expecting token of type ':' but found `pg`. Parse error on line 17, column 20: Expecting: one of these possible Token sequences: 1. [NEWLINE] 2. [EOF] but found: '[PostgreSQL]' Parse error on line 18, column 18: Expecting token of type ':' but found `redis`. Parse error on line 18, column 23: Expecting: one of these possible Token sequences: 1. [NEWLINE] 2. [EOF] but found: '[Redis]' Parse error on line 19, column 17: Expecting token of type ':' but found `minio`. Parse error on line 19, column 22: Expecting: one of these possible Token sequences: 1. [NEWLINE] 2. [EOF] but found: '[MinIO]' Parse error on line 20, column 21: Expecting: one of these possible Token sequences: 1. [NEWLINE] 2. [EOF] but found: 'K8s' Parse error on line 20, column 29: Expecting token of type ':' but found ` `. Parse error on line 22, column 12: Expecting token of type ':' but found `--`. Parse error on line 22, column 16: Expecting token of type 'ARROW_DIRECTION' but found `auth`. Parse error on line 23, column 10: Expecting token of type ':' but found `--`. Parse error on line 23, column 14: Expecting token of type 'ARROW_DIRECTION' but found `role_mgr`. Parse error on line 24, column 10: Expecting token of type ':' but found `--`. Parse error on line 24, column 14: Expecting token of type 'ARROW_DIRECTION' but found `tool_mgr`. Parse error on line 25, column 10: Expecting token of type ':' but found `--`. Parse error on line 25, column 14: Expecting token of type 'ARROW_DIRECTION' but found `agent_sched`. Parse error on line 26, column 14: Expecting token of type ':' but found `--`. Parse error on line 26, column 18: Expecting token of type 'ARROW_DIRECTION' but found `llm_adapter`. Parse error on line 27, column 14: Expecting token of type ':' but found `--`. Parse error on line 27, column 18: Expecting token of type 'ARROW_DIRECTION' but found `memory`. Parse error on line 28, column 14: Expecting token of type ':' but found `--`. Parse error on line 28, column 18: Expecting token of type 'ARROW_DIRECTION' but found `tool_engine`. Parse error on line 29, column 17: Expecting token of type ':' but found `--`. Parse error on line 29, column 21: Expecting token of type 'ARROW_DIRECTION' but found `llm_adapter`. Parse error on line 30, column 17: Expecting token of type ':' but found `--`. Parse error on line 30, column 21: Expecting token of type 'ARROW_DIRECTION' but found `tool_engine`. Parse error on line 31, column 17: Expecting token of type ':' but found `--`. Parse error on line 31, column 21: Expecting token of type 'ARROW_DIRECTION' but found `vector`. Parse error on line 32, column 17: Expecting token of type ':' but found `--`. Parse error on line 32, column 21: Expecting token of type 'ARROW_DIRECTION' but found `k8s`. Parse error on line 33, column 17: Expecting token of type ':' but found `--`. Parse error on line 33, column 21: Expecting token of type 'ARROW_DIRECTION' but found `k8s`. Parse error on line 34, column 12: Expecting token of type ':' but found `--`. Parse error on line 34, column 16: Expecting token of type 'ARROW_DIRECTION' but found `pg`. Parse error on line 35, column 12: Expecting token of type ':' but found `--`. Parse error on line 35, column 16: Expecting token of type 'ARROW_DIRECTION' but found `minio`. Parse error on line 36, column 16: Expecting token of type ':' but found `--`. Parse error on line 36, column 20: Expecting token of type 'ARROW_DIRECTION' but found `perm`. Parse error on line 37, column 10: Expecting token of type ':' but found `--`. Parse error on line 37, column 14: Expecting token of type 'ARROW_DIRECTION' but found `role_mgr`. Parse error on line 38, column 10: Expecting token of type ':' but found `--`. Parse error on line 38, column 14: Expecting token of type 'ARROW_DIRECTION' but found `tool_mgr`.各层职责说明:接入层:负责租户认证、请求路由、限流熔断,所有请求都需要经过API网关校验租户身份后才能进入核心层核心服务层:负责平台核心业务逻辑,包括角色管理、工具管理、Agent调度、租户管理、权限管控能力层:提供底层能力支撑,包括LLM接入适配、工具执行引擎、向量存储、记忆管理资源层:负责数据存储和计算资源调度,包括数据库、缓存、对象存储、K8s计算集群核心实体关系设计平台核心实体包括租户、用户、角色、工具,实体之间的ER关系如下:hasownsownsbindsusesTENANTuuididPKstringnameintquota_llmintquota_toolintstorage_limitstringisolation_modetimestampcreated_atUSERuuididPKuuid

更多文章