食谱膳食计划
🎥 观看视频演示
使用Google ADK(Agent Development Kit)和LangGraph构建的智能膳食计划系统,可创建具有优化购物清单的预算意识膳食计划。
概述
该项目展示了一个多代理架构,在该架构中,专门的人工智能代理协作计划膳食、优化杂货清单并确保预算合规。该系统接受用户请求,如“计划5顿总价低于50美元的晚餐”,并返回一份完整的用餐计划,其中包含合并的购物清单和成本明细。
特性
- 智能膳食过滤:根据偏好(素食、纯素、无麸质、低碳水化合物)自动检测和过滤食谱
- 成分整合:识别食谱中的共享成分,以优化购物效率
- 预算优化:确保膳食计划保持在指定的预算限制范围内(默认值:50美元)
- 成本计算:生成包含数量和价格的详细购物清单
技术特性
✅1.多代理系统
三个LLM授权代理
- 编排器代理 (
agents/orchestrator_agent.py) - 配方计划代理 (
recipe_planner_a2a_agent.py随着agents/langgraph_agent_mcp.py) - 精通代码的代理人 (
agents/adk_agent.py)
✅2.工具
自定义工具:
- run_recipe_planner (
recipe_planner_a2a_agent.py)-将LangGraph状态机包装为Recipe Planner Agent使用的工具函数
内置工具:
- 内置代码执行器 (
agents/adk_agent.py)-支持动态Python代码生成和执行,用于预算计算和成本优化
✅3.会话和记忆
会话管理:
- 内存会话服务 (
utils.py)-使用session_id和user_id跟踪创建和管理用户会话
✅4.代理2代理(A2A)通信
A2A通信:
- RemoteA2aAgent -Recipe Planner作为独立服务器运行,Orchestrator作为客户端运行。编排器通过A2A协议使用配方计划器
✅5.模型上下文协议(MCP)
MCP集成:
- MCP客户端 -用于
langgraph_agent_mcp.py连接到MCP服务器以访问配方数据 - MCP服务器 (
recipe_mcp_server.py)-为配方数据库操作提供6个工具和2个资源
MCP服务器工具
MCP服务器(recipe_mcp_server.py)公开了以下用于配方数据库操作的工具:
工具(6):
- search_recipes_by_name -按名称搜索食谱(不区分大小写的部分匹配)
- 过滤器_密码_标签 -按饮食标签/偏好过滤食谱(素食、纯素、无麸质、低碳水化合物)
- 获取密码详细信息 -获取特定食谱的完整详细信息,包括配料、数量和价格
- list_all_recipes -列出所有可用的食谱,并附上标签和估计成本
- 搜索\_ \_配料 -查找包含特定成分的所有食谱
- 获取_规则\_ \_预算 -查找每个食谱在指定预算内的所有食谱
资源(2):
- recipe://database/summary -提供数据库统计数据(总配方、可用标签、平均成本)
- recipe://tags/available -列出所有可用的饮食标签和食谱计数
体系结构(3-Agent系统)
1.编排代理(meal_plan_orchestrator_a2a)
文件: agents/orchestrator_agent.py
- 角色:接收用户请求并将任务委托给其他代理的主协调器
- 沟通模式:
- 通过以下方式使用配方计划代理 RemoteA2aAgent (使用A2A协议) - 通过以下方式在本地调用代码智能代理 代理工具
- 能力:
- 将膳食计划请求路由到远程配方计划代理 - 将成本计算委托给当地精通代码的代理 - 确保膳食计划符合预算要求 - 使用配方列表和购物详情格式化最终输出
- 模型:Gemini 2.5 Flash(推理模型)
2.配方计划代理(recipe_planner_agent)
文件: agents/recipe_planner_a2a_agent.py, agents/langgraph_helper.py
- 角色:处理通过MCP访问的配方选择逻辑
- 部署:在端口8001上作为独立的A2A服务器运行(
recipe_planner_a2a_server.py) - 工具:
run_recipe_planner()函数封装LangGraph状态机 - 工作流程:4节点LangGraph状态机
1. gather_preferences:使用LLM结构化输出(Pydantic模型)提取饮食要求和食谱计数 1. suggest_recipes:通过MCP客户端从数据库中选择食谱(调用 filter_recipes_by_tags, list_all_recipes, get_recipe_details) 1. check_overlap:识别所选食谱中的共享成分 1. optimize_list:将配料合并到统一的购物清单中,并标记成本计算
- MCP集成:用途
RecipeMCPClient通过MCP访问配方数据库 - 模型:Gemini 2.5 Flash Lite
3.精通代码的代理人(code_savvy_agent_builtin)
文件: agents/code_savy_agent.py
- 角色:具有Python代码执行能力的预算优化专家
- 部署:在Orchestrator Agent进程中本地运行(不是A2A)
- 能力:
- 生成并执行用于成本计算的Python代码 - 验证预算限制(默认值:$50) - 如果超出预算,建议更换 - 用数量和价格格式化购物清单 - 提供成本明细和节省分析
- 代码执行器:用于安全执行Python脚本的BuiltInCodeExecutor
- 模型:双子座2.0闪光灯
配方数据库
该系统包括10个预先配置的食谱 recipes.py.\ 每个食谱都包括详细的配料清单,包括数量和单独的价格。
工作流程
User: "Plan 5 dinners under $50 total"
↓
┌─────────────────────────────────────┐
│ Orchestrator Agent (ADK) │
│ (agents/orchestrator_agent.py) │
└─────────────────────────────────────┘
↓ RemoteA2aAgent (A2A Protocol)
↓ HTTP/JSON-RPC to port 8001
┌─────────────────────────────────────┐
│ Recipe Planner A2A Server │
│ (recipe_planner_a2a_server.py) │
│ │
│ ┌─────────────────────────────────┐ │
│ │ Recipe Planner Agent │ │
│ │ (recipe_planner_a2a_agent.py) │ │
│ │ │ │
│ │ Tool: run_recipe_planner() │ │
│ │ ↓ │ │
│ │ ┌─────────────────────────────┐ │ │
│ │ │ LangGraph State Machine │ │ │
│ │ │ (langgraph_agent_mcp.py) │ │ │
│ │ ├─────────────────────────────┤ │ │
│ │ │ 1. gather_preferences │ │ │
│ │ │ - Parse user request │ │ │
│ │ │ - Extract dietary info │ │ │
│ │ ├─────────────────────────────┤ │ │
│ │ │ 2. suggest_recipes │ │ │
│ │ │ - Filter via MCP ────────┼─┼─┼──> MCP Client
│ │ │ - Select N recipes │ │ │ ↓
│ │ ├─────────────────────────────┤ │ │ ┌──────────────┐
│ │ │ 3. check_overlap │ │ │ │ MCP Server │
│ │ │ - Find shared items │ │ │ │ (recipe_mcp_ │
│ │ ├─────────────────────────────┤ │ │ │ server.py) │
│ │ │ 4. optimize_list │ │ │ └──────┬───────┘
│ │ │ - Consolidate quantities │ │ │ ↓
│ │ │ - Flag: [CALCULATE_COST] │ │ │ ┌──────────────┐
│ │ └─────────────────────────────┘ │ │ │ recipes.py │
│ └─────────────────────────────────┘ │ └──────────────┘
└─────────────────────────────────────┘
↓ A2A Response
┌─────────────────────────────────────┐
│ Orchestrator Agent (ADK) │
└─────────────────────────────────────┘
↓ AgentTool invocation
┌─────────────────────────────────────┐
│ Code Savvy Agent (ADK) │
│ (agents/adk_agent.py) │
├─────────────────────────────────────┤
│ - Generate Python code │
│ - Calculate total costs │
│ - Verify budget constraints │
│ - Format shopping list │
└─────────────────────────────────────┘
↓
Final Meal Plan
(Recipes + Shopping List + Costs)项目结构
/home/ed/kaggle/recipe/
├── recipe_meal_planner.py # Main CLI entry point with A2A
├── recipes.py # Recipe database
├── recipe_mcp_server.py # MCP server for recipe database
├── utils.py # Environment variables & session management
├── __init__.py # Package initialization
├── agents/
│ ├── orchestrator_agent.py # Orchestrator with A2A communication
│ ├── langgraph_helper.py # LangGraph state machine with MCP
│ ├── code_savy_agent.py # Code execution agent
│ └── recipe_planner_a2a_agent.py # Recipe planner ADK wrapper
├── recipe_planner_a2a_agent.py # LangGraph wrapper for A2A
├── recipe_planner_a2a_server.py # A2A server exposing Recipe Planner
├── start_a2a_web.sh # Launch script for A2A web interface
├── agents_web/ # ADK Web UI configuration
│ └── recipe_meal_planner/
│ ├── agent.py # Web UI with A2A orchestrator
│ ├── README.md # Web UI documentation
│ └── __init__.py # Package initialization
├── .env # API keys
├── requirements.txt # Python dependencies
├── image1.png # Project screenshot
└── README.md # This file设置和安装
先决条件
- Python 3.8+
- 可访问Gemini模型的Google API密钥
安装
# Install uv if you haven't already
curl -LsSf https://astral.sh/uv/install.sh | sh
# Clone the repository
git clone https://github.com/edangx100/Recipe-Meal-Planner-A2A-MCP
cd Recipe-Meal-Planner-A2A-MCP
# Create virtual environment and install dependencies
uv venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Install dependencies from requirements.txt
uv pip install -r requirements.txt重要提示:配置API密钥
编辑 .env 归档并替换占位符 GOOGLE_API_KEY 使用您自己的Google API密钥:
# .env file
GOOGLE_API_KEY=your_actual_api_key_here # Replace with your real API key
# Model configuration (optional - defaults provided)
DEFAULT_LLM=gemini-2.0-flash # For general tasks
DEFAULT_REASONING_LLM=gemini-2.5-flash # For orchestrator要获取Google API密钥,请访问:https://aistudio.google.com/app/apikey
这些值是从具有合理默认值的环境变量中加载的 utils.py.
用法
重要提示:首先启动配方计划器A2A服务器
在使用以下任一选项之前,您必须启动配方计划器A2A服务器:
# Terminal 1: Start the Recipe Planner A2A Server (required for both options)
python recipe_planner_a2a_server.py保持此服务器在后台运行。服务器在端口8001上运行,并通过A2A协议提供配方计划器代理。
选项1:ADK Web UI(推荐-交互式界面)
运行交互式web界面与您的膳食计划代理聊天:
# Terminal 2: Start ADK web UI (default port 8000)
# Make sure you're in the project directory
cd /home/ed/kaggle/recipe
# Activate virtual environment
source .venv/bin/activate
# Start ADK web UI
adk web agents_web然后打开浏览器 http://localhost:8000
使用自定义端口:
# Use a different port if 8000 is already in use
adk web --port 8080 agents_web然后打开 http://localhost:8080
在web UI中尝试以下查询:
- “计划5顿总费用低于50美元的晚餐”
- “计划3顿总价低于50美元的素食晚餐,重点是蛋白质”
您将看到:
- 代理响应的实时流式传输
- LangGraph状态机工作流程的详细跟踪
- 完整的膳食计划,包括食谱、购物清单和价格明细
选项2:CLI模式(预配置查询)
使用预配置的测试场景运行命令行版本:
# Terminal 2: Run the meal planner client (A2A server must be running in Terminal 1)
python recipe_meal_planner.py该脚本包括两个测试场景:
- 基本膳食计划:
"Plan 5 dinners under $50 total"- 以蛋白质为重点的素食主义者:
"Plan 3 vegetarian dinners under $50 total with a focus on protein"期望输出
YOU: Plan 5 dinners under $50 total
ORCHESTRATOR: Here is your meal plan:
1. Spaghetti Aglio e Olio
2. Chicken Stir-Fry
3. Black Bean Tacos
4. Lentil Soup
5. Baked Salmon with Veggies
Here is your shopping list:
* spaghetti: 1 lb
* garlic: 1 bulb
* olive oil: 1/2 cup + 2 tbsp
* chicken breast: 1 lb
...
Total Cost: $47.23
Enjoy your meals!故障排除
ADK Web UI问题
问题:端口已在使用中
ERROR: [Errno 98] address already in use解决方案: 使用其他端口或终止现有进程:
# Option 1: Use a different port
adk web --port 8001 agents_web
# Option 2: Find and kill the process using port 8000
lsof -i :8000 # Find the PID
kill -9
# Replace
with the actual process ID