baodan/api/insurance/models/prompt.py

56 lines
2.3 KiB
Python
Raw Normal View History

"""Prompt模板模型。"""
from sqlalchemy import Column, Integer, String, Text, TIMESTAMP, func
from insurance.db.compat import db
class PromptTemplate(db.Model):
"""Prompt模板表。"""
__tablename__ = "insurance_prompt_templates"
id = Column(Integer, primary_key=True, autoincrement=True)
name = Column(String(128), nullable=False, comment="模板名称")
description = Column(String(256), default="", comment="模板描述")
content = Column(Text, default="", comment="模板内容")
variables = Column(Text, default="[]", comment="变量配置JSON")
category = Column(String(64), default="general", comment="分类")
created_at = Column(TIMESTAMP, server_default=func.now())
updated_at = Column(TIMESTAMP, server_default=func.now(), onupdate=func.now())
def to_dict(self):
import json
return {
"id": f"prompt-{self.id}",
"name": self.name,
"description": self.description,
"content": self.content,
"variables": json.loads(self.variables) if self.variables else [],
"category": self.category,
"created_at": str(self.created_at) if self.created_at else None,
"updated_at": str(self.updated_at) if self.updated_at else None,
}
class PromptVersion(db.Model):
"""Prompt版本历史表。"""
__tablename__ = "insurance_prompt_versions"
id = Column(Integer, primary_key=True, autoincrement=True)
prompt_id = Column(Integer, nullable=False, comment="关联的模板ID")
version = Column(Integer, nullable=False, comment="版本号")
content = Column(Text, default="", comment="版本内容")
variables = Column(Text, default="[]", comment="变量配置JSON")
change_note = Column(String(256), default="", comment="变更说明")
created_at = Column(TIMESTAMP, server_default=func.now())
def to_dict(self):
import json
return {
"id": self.id,
"prompt_id": self.prompt_id,
"version": self.version,
"content": self.content,
"variables": json.loads(self.variables) if self.variables else [],
"change_note": self.change_note,
"created_at": str(self.created_at) if self.created_at else None,
}