diff --git a/agent/health.py b/agent/health.py index 985de6b..0626274 100644 --- a/agent/health.py +++ b/agent/health.py @@ -1,9 +1,14 @@ from langchain.agents import create_agent from agent.model import model -from agent.prompt import tip_prompt +from agent.prompt import tip_prompt, recipe_prompt tip_agent = create_agent( model=model, system_prompt=tip_prompt ) + +recipe_agent = create_agent( + model=model, + system_prompt=recipe_prompt +) diff --git a/agent/prompt.py b/agent/prompt.py index 4e6f512..6473cee 100644 --- a/agent/prompt.py +++ b/agent/prompt.py @@ -56,3 +56,35 @@ tip_prompt = """ } ] """ + +recipe_prompt = """ +你是一位注册营养师和资深美食顾问。请为我生成2份健康菜谱,要求包含一份主菜和一份汤品。 + +请按以下JSON数组格式输出,每条菜谱为一个对象,包含三个字段:name、tags、content。 + +字段说明: +- name(字符串):菜品名称,控制在4-8个汉字以内,简洁有记忆点。 +- tags(字符串数组):2~3个关键词标签(如:高蛋白、低脂、补钙、抗炎、护心等)。 +- content(字符串):菜谱完整内容,必须包含以下五个部分,每部分用【】标题 + 对应的表情符号开头: + +第1部分:【🥬 食材清单】 +第2部分:【👩‍🍳 制作步骤】 +第3部分:【📊 营养成分(每人份)】 +第4部分:【👥 适宜人群】 +第5部分:【💡 营养贴士】 + +输出格式示例: +[ + { + "name": "清蒸鲈鱼", + "tags": ["高蛋白", "低脂", "护心"], + "content": "【🥬 食材清单】\n主料:鲈鱼1条(约500g)\n配料/调料:姜丝15g、葱丝20g、蒸鱼豉油15ml、料酒10ml、橄榄油5ml、盐1g\n\n【👩‍🍳 制作步骤】\n1、鲈鱼去鳞去内脏洗净,两面各划两刀,用料酒和盐抹匀腌制10分钟。\n2、盘底铺一半姜丝,放上鲈鱼,鱼身再撒剩余姜丝。\n3、蒸锅水烧开后放入鱼盘,大火蒸8分钟,关火再虚蒸2分钟。\n4、取出倒掉盘内汤汁,拣去姜丝,铺上葱丝。\n5、淋上蒸鱼豉油,烧热橄榄油均匀浇在葱丝上即可。\n\n【📊 营养成分(每人份)】\n热量:185千卡 | 蛋白质:28g | 碳水:2g | 脂肪:7g\n\n【👥 适宜人群】\n适合减脂期人群、老年人、术后恢复者;海鲜过敏者不宜食用,痛风急性期慎食。\n\n【💡 营养贴士】\n✨ 健康亮点:鲈鱼富含优质蛋白和DHA,清蒸减少油脂氧化,最大程度保留营养,护心又健脑。\n🔑 烹饪小技巧:建议搭配糙米饭和焯拌绿叶菜;控盐人群可减少蒸鱼豉油用量,用姜葱提鲜,风味不减。" + } +] + +全局要求: +- 整体需符合“三减”(减油、减盐、减糖)原则。 +- 食材应常见易得,做法不宜过于复杂。 +- 两份菜谱营养互补,风格不同(一主菜一汤品)。 +- 直接输出JSON,不要添加额外说明文字。 +""" diff --git a/main.py b/main.py index cbb959d..0730e08 100644 --- a/main.py +++ b/main.py @@ -11,17 +11,25 @@ from service import agent_service scheduler = BackgroundScheduler() -def job_task(): +def tip_job_task(): """定时执行的任务""" try: - print(f"定时任务执行: {datetime.now()}") - agent_service.generate_tip_agent() + print(f"今日资讯定时任务执行: {datetime.now()}") + agent_service.query_tip_agent() except Exception as e: - print(f"定时任务异常: {e}") + print(f"今日资讯定时任务异常: {e}") + +def recipe_job_task(): + """定时执行的任务""" + try: + print(f"今日菜谱定时任务执行: {datetime.now()}") + agent_service.query_recipe_agent() + except Exception as e: + print(f"今日菜谱定时任务异常: {e}") -scheduler.add_job(job_task, trigger="cron", hour=2, minute=0, second=0, id="daily_gen_tips", replace_existing=True) - +scheduler.add_job(tip_job_task, trigger="cron", hour=2, minute=0, second=0, id="daily_gen_tips", replace_existing=True) +scheduler.add_job(recipe_job_task, trigger="cron", hour=3, minute=0, second=0, id="daily_gen_recipes", replace_existing=True) @asynccontextmanager async def lifespan(app: FastAPI): diff --git a/routers/agent.py b/routers/agent.py index 40e8b61..f66bfa2 100644 --- a/routers/agent.py +++ b/routers/agent.py @@ -7,9 +7,18 @@ router = APIRouter(prefix="/agent", tags=["Agent"]) @router.post("/tip/generate") def generate_tip(): - return agent_service.generate_tip_agent() + return agent_service.query_tip_agent() @router.get("/tip/latest") def latest_tip(): return agent_service.get_latest_tip() + +@router.post("/recipe/generate") +def generate_recipe(): + return agent_service.query_recipe_agent() + + +@router.get("/recipe/latest") +def latest_recipe(): + return agent_service.get_latest_recipe() diff --git a/service/agent_service.py b/service/agent_service.py index cbbde28..59143e7 100644 --- a/service/agent_service.py +++ b/service/agent_service.py @@ -3,13 +3,14 @@ from typing import Dict, Any from langchain_core.messages import HumanMessage -from agent.health import tip_agent +from agent.health import tip_agent, recipe_agent memory_store: Dict[str, Any] = { - "articles": [] + "articles": [], + "recipe": [] } -def generate_tip_agent(): +def query_tip_agent(): try: print("开始生成今日5篇健康科普文章") resp = tip_agent.invoke({ @@ -35,4 +36,32 @@ def generate_tip_agent(): return False def get_latest_tip(): - return memory_store["articles"] \ No newline at end of file + return memory_store["articles"] + +def query_recipe_agent(): + try: + print("开始生成今日菜谱") + resp = recipe_agent.invoke({ + "messages": [HumanMessage(content="请生成今日菜谱")] + }) + last_msg = resp["messages"][-1] + raw_text = last_msg.content.strip() + + # 解析大模型返回的json字符串 + recipes = json.loads(raw_text) + # 写入内存缓存 + memory_store["recipe"] = recipes + print("结束生成今日菜谱") + return True + except json.JSONDecodeError as je: + print(f"[JSON解析错误] {str(je)}") + memory_store["recipe"] = [] + return False + + except Exception as e: + print(f"\n[错误]: {str(e)}") + memory_store["recipe"] = [] + return False + +def get_latest_recipe(): + return memory_store["recipe"] \ No newline at end of file