Files
aiagent/backend/app/core/database.py
renjianbo e3802eff60 feat: 实现 Agent 自主学习 — 从历史执行中优化工具选择
- 新增 AgentLearningPattern 模型和 agent_learning_service 服务
- 执行前注入历史学习模式到 system prompt 作为工具选择建议
- 执行后自动提取工具序列并保存/累计学习模式
- 支持任务分类(11类)、关键词提取、工具序列合并、有效性评分
- 集成到 AgentRuntime.run()/run_stream(),支持 bare chat 和 Agent 模式

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-02 12:04:00 +08:00

54 lines
1.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
数据库配置
"""
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from app.core.config import settings
# 创建数据库引擎MySQL
engine = create_engine(
settings.DATABASE_URL,
pool_pre_ping=True,
pool_size=10,
max_overflow=20,
echo=settings.DEBUG # 开发环境显示SQL
)
# 创建会话工厂
# expire_on_commit=Falsecommit 后仍可读取已加载标量,避免 FastAPI 在序列化 ExecutionResponse 时
# 因会话已关闭而再次触发懒加载,从而出现「仅 HTTP 报 DATABASE_ERROR、TestClient 正常」的现象。
SessionLocal = sessionmaker(
autocommit=False, autoflush=False, bind=engine, expire_on_commit=False
)
# 创建基础模型类
Base = declarative_base()
def get_db():
"""获取数据库会话"""
db = SessionLocal()
try:
yield db
finally:
db.close()
def init_db():
"""初始化数据库,创建所有表"""
# 导入所有模型,确保它们被注册
import app.models.user
import app.models.workflow
import app.models.agent
import app.models.execution
import app.models.model_config
import app.models.workflow_template
import app.models.permission
import app.models.alert_rule
import app.models.agent_llm_log
import app.models.agent_vector_memory
import app.models.agent_learning_pattern
import app.models.knowledge_base
Base.metadata.create_all(bind=engine)