feat: 接入mysql数据库
This commit is contained in:
@@ -1,52 +1,62 @@
|
||||
import json
|
||||
from datetime import datetime
|
||||
from typing import Dict, Any
|
||||
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 tip_agent, recipe_agent, report_agent
|
||||
from schema.agent import ReportRequest
|
||||
|
||||
memory_store: Dict[str, Any] = {
|
||||
"articles": [],
|
||||
"recipe": []
|
||||
}
|
||||
from agent.health import article_agent, recipe_agent, report_agent
|
||||
from models.health import Article, Recipe
|
||||
from schema.agent import ReportAgentRequest
|
||||
|
||||
|
||||
def query_tip_agent():
|
||||
def query_article_agent(db: Session):
|
||||
try:
|
||||
print("开始生成今日健康资讯")
|
||||
today = datetime.today().strftime("%Y-%m-%d")
|
||||
|
||||
resp = tip_agent.invoke({
|
||||
resp = article_agent.invoke({
|
||||
"messages": [SystemMessage(content=f"当前系统日期:{today} \n\n"),
|
||||
HumanMessage(content="请生成今日健康资讯")]
|
||||
})
|
||||
last_msg = resp["messages"][-1]
|
||||
raw_text = last_msg.content.strip()
|
||||
|
||||
# 解析大模型返回的json字符串
|
||||
articles = json.loads(raw_text)
|
||||
# 写入内存缓存
|
||||
memory_store["articles"] = articles
|
||||
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)}")
|
||||
memory_store["articles"] = []
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
print(f"\n[错误]: {str(e)}")
|
||||
memory_store["articles"] = []
|
||||
return False
|
||||
|
||||
|
||||
def get_latest_tip():
|
||||
return memory_store["articles"]
|
||||
def get_latest_article(db: Session):
|
||||
today = date.today()
|
||||
|
||||
result = db.execute(
|
||||
select(Article).where(Article.date == today)
|
||||
)
|
||||
article = result.scalar_one_or_none()
|
||||
|
||||
return article
|
||||
|
||||
|
||||
def query_recipe_agent():
|
||||
def query_recipe_agent(db: Session):
|
||||
try:
|
||||
print("开始生成今日养生食谱")
|
||||
today = datetime.today().strftime("%Y-%m-%d")
|
||||
@@ -58,28 +68,41 @@ def query_recipe_agent():
|
||||
last_msg = resp["messages"][-1]
|
||||
raw_text = last_msg.content.strip()
|
||||
|
||||
# 解析大模型返回的json字符串
|
||||
recipes = json.loads(raw_text)
|
||||
# 写入内存缓存
|
||||
memory_store["recipe"] = recipes
|
||||
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)}")
|
||||
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"]
|
||||
def get_latest_recipe(db: Session):
|
||||
today = date.today()
|
||||
|
||||
result = db.execute(
|
||||
select(Recipe).where(Recipe.date == today)
|
||||
)
|
||||
article = result.scalar_one_or_none()
|
||||
|
||||
return article
|
||||
|
||||
|
||||
def query_report_agent(req: ReportRequest):
|
||||
def query_report_agent(req: ReportAgentRequest):
|
||||
try:
|
||||
print("开始生成报告")
|
||||
today = datetime.today().strftime("%Y-%m-%d")
|
||||
@@ -93,4 +116,4 @@ def query_report_agent(req: ReportRequest):
|
||||
return last_msg.content.strip()
|
||||
except Exception as e:
|
||||
print(f"\n[生成报告错误]: {str(e)}")
|
||||
return False
|
||||
return False
|
||||
|
||||
Reference in New Issue
Block a user