基于 Marvin 与 A2A 协议的联系人信息抽取代理

该示例用 Marvin 框架从文本中抽取姓名、邮箱、电话等结构化联系人信息,并通过 A2A 协议对外服务。代理维护多轮会话状态,在收集到姓名和邮箱后确认结果,响应同时包含文本摘要与结构化数据。

# 马文联系提取器代理( A2A 样本) 该样本显示,一种剂使用了 [马文](https://github.com/prefecthq/marvin) 从文本中提取结构化联系信息的框架,与Agent2Agent(A2A)协议集成. ## 概览 代理接收文本, 尝试将联系方式( 姓名、 电子邮件、 电话等) 提取为使用 Marvin 的结构化格式。 它管理跨多个回合的对话状态, 在确认所提取的数据之前收集所需的信息( 姓名、 电子邮件) 。 代理的答复既包括文本摘要/ 问题, 也包括通过 A2A 收集的结构化数据 。 ## 关键部件 - **马文 `ExtractorAgent` (`agent.py`)**: 使用核心逻辑 `marvin` 用于通过词典提取和管理多回合状态。 - **A2A `AgentTaskManager` (`task_manager.py`)**:将代理机与A2A协议整合,管理任务状态(包括通过SSE进行流化)和响应格式化. - **A2A 服务器 (`__main__.py`)**: 托管代理和任务管理器。 ## 先决条件 - Python 3.12 + (英语). - [数字 :](https://docs.astral.sh/uv/getting-started/installation/) - `OPENAI_API_KEY` (或其他LLM提供商的证明,由pydantic-ai支持). ## 设置运行( R) 1. 导航到 Python 样本目录 : ```bash cd samples/python/agents/marvin ``` 2. 设置一个 LLM 提供者 API 密钥 : ```bash export OPENAI_API_KEY=your_api_key_here ``` 3. 设置 Python 环境 : ```bash uv venv source .venv/bin/activate uv sync ``` 4. 运行 Marvin 代理服务器 : ```bash # Default host/port (localhost:10030) MARVIN_DATABASE_URL=sqlite+aiosqlite:///test.db MARVIN_LOG_LEVEL=DEBUG uv run . # Custom host/port # uv run . --host 0.0.0.0 --port 8080 ``` 无 `MARVIN_DATABASE_URL` 设置, 对话历史不会以会话 ID 持续 。 5. 在单独的终端中,运行一个A2A客户端(例如样本CLI): ```bash # Ensure the environment is active (source .venv/bin/activate) cd samples/python/hosts/cli uv run . --agent http://localhost:10030 # Use the correct agent URL/port ``` ## 提取的数据结构 结构化数据返回于 `DataPart` 定义为: ```python class ContactInfo(BaseModel): name: str = Field(description="Person's first and last name") email: EmailStr phone: str = Field(description="standardized phone number") organization: str | None = Field(None, description="org if mentioned") role: str | None = Field(None, description="title or role if mentioned") ``` 和验证器 使事情的好,如果你想要的 和也许序列化怪异的东西。 ## 学习更多 - [马文文档](https://www.askmarvin.ai/) - [马文·GitHub仓库](https://github.com/prefecthq/marvin)

查看原始来源