feat: 增加食谱Agent

This commit is contained in:
2026-09-01 23:03:17 +08:00
parent 825e097989
commit e71a26e761
5 changed files with 95 additions and 12 deletions

View File

@@ -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"]
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"]