Files
aiagent/backend/app/models/node_template.py

73 lines
3.1 KiB
Python
Raw Normal View History

2026-01-19 00:09:36 +08:00
"""
节点模板模型
用于管理和复用LLM节点的提示词模板
"""
from sqlalchemy import Column, String, Text, Integer, DateTime, JSON, ForeignKey, Boolean, Index, func
2026-01-19 00:09:36 +08:00
from sqlalchemy.dialects.mysql import CHAR
from sqlalchemy.orm import relationship
from app.core.database import Base
import uuid
class NodeTemplate(Base):
"""节点模板表"""
__tablename__ = "node_templates"
id = Column(CHAR(36), primary_key=True, default=lambda: str(uuid.uuid4()), comment="模板ID")
name = Column(String(100), nullable=False, comment="模板名称")
description = Column(Text, comment="模板描述")
category = Column(String(50), comment="分类: text_generation/data_analysis/code_generation/translation/summarization/qa/other")
tags = Column(JSON, comment="标签列表")
# 模板内容
prompt = Column(Text, nullable=False, comment="提示词模板(支持变量占位符,如 {{variable}}")
variables = Column(JSON, comment="变量定义列表,格式: [{\"name\": \"var1\", \"type\": \"text\", \"required\": true, \"description\": \"变量描述\"}]")
# 默认配置
provider = Column(String(50), default="deepseek", comment="默认LLM提供商")
model = Column(String(100), default="deepseek-v4-flash", comment="默认模型")
2026-01-19 00:09:36 +08:00
temperature = Column(String(10), default="0.7", comment="默认温度参数")
max_tokens = Column(Integer, default=1500, comment="默认最大token数")
# 元数据
is_public = Column(Boolean, default=False, comment="是否公开")
is_featured = Column(Boolean, default=False, comment="是否精选")
use_count = Column(Integer, default=0, comment="使用次数")
user_id = Column(CHAR(36), ForeignKey("users.id"), nullable=False, comment="创建者ID")
created_at = Column(DateTime, default=func.now(), comment="创建时间")
updated_at = Column(DateTime, default=func.now(), onupdate=func.now(), comment="更新时间")
__table_args__ = (
Index("idx_nt_category", "category"),
Index("idx_nt_user_id", "user_id"),
Index("idx_nt_is_public", "is_public"),
)
2026-01-19 00:09:36 +08:00
# 关系
user = relationship("User", backref="node_templates")
def __repr__(self):
return f"<NodeTemplate(id={self.id}, name={self.name})>"
def to_dict(self):
"""转换为字典"""
return {
"id": self.id,
"name": self.name,
"description": self.description,
"category": self.category,
"tags": self.tags or [],
"prompt": self.prompt,
"variables": self.variables or [],
"provider": self.provider,
"model": self.model,
"temperature": self.temperature,
"max_tokens": self.max_tokens,
"is_public": self.is_public,
"is_featured": self.is_featured,
"use_count": self.use_count,
"user_id": self.user_id,
"created_at": self.created_at.isoformat() if self.created_at else None,
"updated_at": self.updated_at.isoformat() if self.updated_at else None
}