139 lines
3.6 KiB
Python
139 lines
3.6 KiB
Python
import json
|
|
from datetime import datetime, date
|
|
|
|
from langchain_core.messages import HumanMessage, SystemMessage
|
|
from sqlalchemy import select, delete
|
|
from sqlalchemy.orm import Session
|
|
|
|
from agent.health import article_agent, recipe_agent, report_agent
|
|
from models.health import Article, Recipe
|
|
from schema.agent import ReportAgentRequest
|
|
|
|
|
|
def query_article_agent(db: Session):
|
|
try:
|
|
print("开始生成今日健康资讯")
|
|
today = datetime.today().strftime("%Y-%m-%d")
|
|
|
|
resp = article_agent.invoke({
|
|
"messages": [SystemMessage(content=f"当前系统日期:{today} \n\n"),
|
|
HumanMessage(content="请生成今日健康资讯")]
|
|
})
|
|
last_msg = resp["messages"][-1]
|
|
raw_text = last_msg.content.strip()
|
|
|
|
db.execute(
|
|
delete(Article).where(Article.date == today)
|
|
)
|
|
|
|
article = Article(
|
|
date=date.today(),
|
|
content=json.loads(raw_text),
|
|
)
|
|
db.add(article)
|
|
db.commit()
|
|
db.refresh(article)
|
|
|
|
print("结束生成今日健康资讯")
|
|
return True
|
|
except json.JSONDecodeError as je:
|
|
print(f"[JSON解析错误] {str(je)}")
|
|
return False
|
|
|
|
except Exception as e:
|
|
print(f"\n[错误]: {str(e)}")
|
|
return False
|
|
|
|
|
|
def get_latest_article(db: Session):
|
|
today = date.today()
|
|
|
|
result = db.execute(
|
|
select(Article).where(Article.date == today)
|
|
)
|
|
article = result.scalar_one_or_none()
|
|
|
|
if article is None:
|
|
return [
|
|
{
|
|
"title": "",
|
|
"content": "",
|
|
"summary": "",
|
|
"category": ""
|
|
}
|
|
]
|
|
|
|
return article.content
|
|
|
|
|
|
def query_recipe_agent(db: Session):
|
|
try:
|
|
print("开始生成今日养生食谱")
|
|
today = datetime.today().strftime("%Y-%m-%d")
|
|
|
|
resp = recipe_agent.invoke({
|
|
"messages": [SystemMessage(content=f"当前系统日期:{today} \n\n"),
|
|
HumanMessage(content="请生成今日养生食谱")]
|
|
})
|
|
last_msg = resp["messages"][-1]
|
|
raw_text = last_msg.content.strip()
|
|
|
|
db.execute(
|
|
delete(Recipe).where(Recipe.date == today)
|
|
)
|
|
|
|
recipe = Recipe(
|
|
date=date.today(),
|
|
content=json.loads(raw_text),
|
|
)
|
|
db.add(recipe)
|
|
db.commit()
|
|
db.refresh(recipe)
|
|
|
|
print("结束生成今日养生食谱")
|
|
return True
|
|
except json.JSONDecodeError as je:
|
|
print(f"[JSON解析错误] {str(je)}")
|
|
return False
|
|
|
|
except Exception as e:
|
|
print(f"\n[错误]: {str(e)}")
|
|
return False
|
|
|
|
|
|
def get_latest_recipe(db: Session):
|
|
today = date.today()
|
|
|
|
result = db.execute(
|
|
select(Recipe).where(Recipe.date == today)
|
|
)
|
|
recipe = result.scalar_one_or_none()
|
|
|
|
if recipe is None:
|
|
return [
|
|
{
|
|
"name": "",
|
|
"tags": [],
|
|
"content": ""
|
|
}
|
|
]
|
|
|
|
return recipe.content
|
|
|
|
|
|
def query_report_agent(req: ReportAgentRequest):
|
|
try:
|
|
print("开始生成报告")
|
|
today = datetime.today().strftime("%Y-%m-%d")
|
|
|
|
resp = report_agent.invoke({
|
|
"messages": [SystemMessage(content=f"当前系统日期:{today} \n\n"),
|
|
HumanMessage(content=f"用户数据:${req.query}")]
|
|
})
|
|
print("结束生成报告")
|
|
last_msg = resp["messages"][-1]
|
|
return last_msg.content.strip()
|
|
except Exception as e:
|
|
print(f"\n[生成报告错误]: {str(e)}")
|
|
return False
|