feat: 增加食谱Agent
This commit is contained in:
@@ -1,9 +1,14 @@
|
|||||||
from langchain.agents import create_agent
|
from langchain.agents import create_agent
|
||||||
|
|
||||||
from agent.model import model
|
from agent.model import model
|
||||||
from agent.prompt import tip_prompt
|
from agent.prompt import tip_prompt, recipe_prompt
|
||||||
|
|
||||||
tip_agent = create_agent(
|
tip_agent = create_agent(
|
||||||
model=model,
|
model=model,
|
||||||
system_prompt=tip_prompt
|
system_prompt=tip_prompt
|
||||||
)
|
)
|
||||||
|
|
||||||
|
recipe_agent = create_agent(
|
||||||
|
model=model,
|
||||||
|
system_prompt=recipe_prompt
|
||||||
|
)
|
||||||
|
|||||||
@@ -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,不要添加额外说明文字。
|
||||||
|
"""
|
||||||
|
|||||||
20
main.py
20
main.py
@@ -11,17 +11,25 @@ from service import agent_service
|
|||||||
scheduler = BackgroundScheduler()
|
scheduler = BackgroundScheduler()
|
||||||
|
|
||||||
|
|
||||||
def job_task():
|
def tip_job_task():
|
||||||
"""定时执行的任务"""
|
"""定时执行的任务"""
|
||||||
try:
|
try:
|
||||||
print(f"定时任务执行: {datetime.now()}")
|
print(f"今日资讯定时任务执行: {datetime.now()}")
|
||||||
agent_service.generate_tip_agent()
|
agent_service.query_tip_agent()
|
||||||
except Exception as e:
|
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
|
@asynccontextmanager
|
||||||
async def lifespan(app: FastAPI):
|
async def lifespan(app: FastAPI):
|
||||||
|
|||||||
@@ -7,9 +7,18 @@ router = APIRouter(prefix="/agent", tags=["Agent"])
|
|||||||
|
|
||||||
@router.post("/tip/generate")
|
@router.post("/tip/generate")
|
||||||
def generate_tip():
|
def generate_tip():
|
||||||
return agent_service.generate_tip_agent()
|
return agent_service.query_tip_agent()
|
||||||
|
|
||||||
|
|
||||||
@router.get("/tip/latest")
|
@router.get("/tip/latest")
|
||||||
def latest_tip():
|
def latest_tip():
|
||||||
return agent_service.get_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()
|
||||||
|
|||||||
@@ -3,13 +3,14 @@ from typing import Dict, Any
|
|||||||
|
|
||||||
from langchain_core.messages import HumanMessage
|
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] = {
|
memory_store: Dict[str, Any] = {
|
||||||
"articles": []
|
"articles": [],
|
||||||
|
"recipe": []
|
||||||
}
|
}
|
||||||
|
|
||||||
def generate_tip_agent():
|
def query_tip_agent():
|
||||||
try:
|
try:
|
||||||
print("开始生成今日5篇健康科普文章")
|
print("开始生成今日5篇健康科普文章")
|
||||||
resp = tip_agent.invoke({
|
resp = tip_agent.invoke({
|
||||||
@@ -36,3 +37,31 @@ def generate_tip_agent():
|
|||||||
|
|
||||||
def get_latest_tip():
|
def get_latest_tip():
|
||||||
return memory_store["articles"]
|
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"]
|
||||||
Reference in New Issue
Block a user