基于 CrewAI 与 A2A 协议构建图像生成智能体
该示例用 CrewAI 和 Gemini API 构建图像生成智能体,并通过 A2A 协议对外提供标准化交互。客户端发送文本提示后,服务端转发给智能体生成图像,再以 artifact 形式返回,生成结果可缓存在内存或文件中。
# CrewAI 代理 A2A 协议 此样本显示一个简单的图像生成代理 [CrewAI](https://www.crewai.com/open-source) 并通过A2A协议曝光. ## 如何运作 这个代理机利用CrewAI和Google双子座API根据文本提示生成图像. A2A协议可以实现与代理机的标准化交互,允许客户端作为文物发送请求并接收图像. ```mermaid sequenceDiagram participant Client as A2A Client participant Server as A2A Server participant Agent as CrewAI Agent participant API as Gemini API Client->>Server: Send task with text prompt Server->>Agent: Forward prompt to image agent Note over Server,Agent: Optional: Simulated streaming updates Agent->>API: Generate image using Gemini API->>Agent: Return generated image Agent->>Server: Store image and return ID Server->>Client: Respond with image artifact ``` ## 关键部件 - **CrewAI探员**: 带有专门工具的图像生成代理 - **A2A 服务器**: 提供与代理人互动的标准化协议 - **图像生成**: 使用双子座 API 从文本描述创建图像 - **缓存系统**: 存储生成的图像以供检索( 以记忆或文件为基础) ## 先决条件 - Python 3.12 或更高音量 - [UV](https://docs.astral.sh/uv/) 软件包管理器( 建议) - Google API 密钥(用于双子座访问) ## 设置运行( R) 1. 导航到样本目录 : ```bash cd samples/python/agents/crewai ``` 2. 使用您的 API 密钥( 或 Vertex AI 证书) 创建环境文件 : ```bash echo "GOOGLE_API_KEY=your_api_key_here" > .env ``` 3. 设置 Python 环境 : ```bash uv python pin 3.13 uv venv source .venv/bin/activate ``` 4. 使用想要的选项运行代理 : ```bash # Basic run uv run . # On custom host/port uv run . --host 0.0.0.0 --port 8080 ``` 5. 运行 A2A 客户端 : 在单独的终端中 : ```bash # Connect to the agent (specify the agent URL with correct port and host) cd samples/python/hosts/cli uv run . --agent http://localhost:10001 # If you changed the port when starting the agent, use that port instead # uv run . --agent http://localhost:YOUR_PORT ``` ## 构建容器图像 代理也可以使用容器文件构建. 1. 导航到 `samples/python` 目录 : ```bash cd samples/python ``` 2. 构建容器文件 ```bash podman build -f agents/crewai/Containerfile . -t crewai-a2a-server ``` > [提普 提普 提普 提普 提普 提普] > 波德曼是替补 `docker` 也可以用于这些命令。 3. 运行您的容器 ```bash podman run -p 10001:10001 -e GOOGLE_API_KEY=your_api_key_here crewai-a2a-server ``` 4. 运行 A2A 客户端(从上面一节走步骤 5) > [重要吗?] > > - **访问 URL :** 您必须通过 A2A 客户端访问 URL `0.0.0.0:10001`。使用 `localhost` 将不工作。 > - **主机名覆盖 :** 如果部署到容器外对主机名定义不同的环境,请使用 `HOST_OVERRIDE` 在代理卡上设置预期主机名的环境变量。这确保了与客户端应用程序的适当通信。 ## 特性改进( P) **特性 :** - 使用 Google 双子座生成文本到图像 - 支持使用引用修改现有图像 - 自动重试处理的强力错误 - 基于文件的可选缓存持久性 - 改进的文物 ID 从查询中提取 **限制:** - 无真流( CrewAI 不本土支持) - 有限的代理互动(不进行多回合对话) ## 学习更多 - [A2A 协议文档](https://a2a-protocol.org) - [CrewAI 文档](https://docs.crewai.com/introduction) - [谷歌双子座 API](https://ai.google.dev/gemini-api)
查看原始来源