agent_model.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import json
  2. from datetime import datetime
  3. from enum import IntEnum
  4. from sqlalchemy import Column, String, Enum as SQLAlchemyEnum, Integer, BigInteger, DateTime, Text, Float, Boolean
  5. from app.models.base_model import Base
  6. class AgentType(IntEnum):
  7. RAGFLOW = 1
  8. BISHENG = 2
  9. BASIC = 3
  10. class AgentModel(Base):
  11. __tablename__ = "agent"
  12. id = Column(String(255), primary_key=True)
  13. name = Column(String(255))
  14. sort = Column(Integer, default=0, nullable=False)
  15. agent_type = Column(SQLAlchemyEnum(AgentType), nullable=False)
  16. type = Column(String(255), nullable=False)
  17. # to_dict 方法
  18. def to_dict(self):
  19. return {
  20. 'id': self.id,
  21. 'name': self.name,
  22. 'agent_type': self.agent_type,
  23. 'type': self.type
  24. }
  25. class CanvasModel(Base):
  26. __tablename__ = 'canvas'
  27. id = Column(String(32), primary_key=True) # id
  28. create_date = Column(DateTime, default=datetime.now)
  29. update_date = Column(DateTime, default=datetime.now)
  30. avatar = Column(Text) # 图标
  31. user_id = Column(String(255)) # 用户id
  32. title = Column(String(255)) # 标题
  33. description = Column(Text) # 说明
  34. canvas_type = Column(String(32)) # agent类型
  35. dsl = Column(Text)
  36. agent_type = Column(String(2)) # agent平台
  37. def get_id(self):
  38. return str(self.id)
  39. def to_json(self):
  40. return {
  41. 'id': self.id,
  42. 'create_date': self.create_date,
  43. 'update_date': self.update_date,
  44. 'avatar': self.avatar,
  45. 'user_id': self.user_id,
  46. 'title': self.title,
  47. 'description': self.description,
  48. 'canvas_type': self.canvas_type,
  49. 'dsl': self.dsl
  50. }
  51. class UnifiedAgentModel(Base):
  52. __tablename__ = 'unified_agent'
  53. id = Column(String(32), primary_key=True)
  54. tenant_id = Column(String(32))
  55. name = Column(String(255))
  56. description = Column(Text)
  57. icon = Column(Text)
  58. prompt_type = Column(String(16))
  59. prompt_config = Column(Text, default='{}')
  60. status = Column(String(1))
  61. prompts = Column(Text, default='[]')
  62. language = Column(String(32))
  63. llm_id = Column(String(128), default='')
  64. llm_setting = Column(Text, default='{}')
  65. similarity_threshold = Column(Float, default=0.0)
  66. vector_similarity_weight = Column(Float, default=0.0)
  67. top_n = Column(Integer, default=0)
  68. top_k = Column(Integer, default=0)
  69. do_refer = Column(String(1), default='')
  70. rerank_id = Column(String(128), default='')
  71. kb_ids = Column(Text, default='[]')
  72. hide = Column(Boolean, default=False)
  73. type = Column(Integer)
  74. canvas_type = Column(String(32), default="")
  75. dsl = Column(Text, default='{}')
  76. def get_id(self):
  77. return str(self.id)
  78. def to_json(self):
  79. if self.prompts is None or self.prompts == '':
  80. self.prompts = '[]'
  81. if self.prompt_config is None or self.prompt_config == '':
  82. self.prompt_config = '{}'
  83. if self.dsl is None or self.dsl == '':
  84. self.dsl = '{}'
  85. if self.kb_ids is None or self.kb_ids == '':
  86. self.kb_ids = '[]'
  87. return {
  88. 'id': self.id,
  89. 'create_time': self.create_time,
  90. 'create_date': self.create_date,
  91. 'update_time': self.update_time,
  92. 'update_date': self.update_date,
  93. 'tenant_id': self.tenant_id,
  94. 'name': self.name,
  95. 'description': self.description,
  96. 'icon': self.icon,
  97. 'language': self.language,
  98. 'llm_id': self.llm_id,
  99. 'similarity_threshold': self.similarity_threshold,
  100. 'vector_similarity_weight': self.vector_similarity_weight,
  101. 'top_n': self.top_n,
  102. 'top_k': self.top_k,
  103. 'do_refer': self.do_refer,
  104. 'kb_ids': self.kb_ids,
  105. 'status': self.status,
  106. 'prompt_type': self.prompt_type,
  107. 'prompt_config': json.loads(self.prompt_config),
  108. 'prompts': json.loads(self.prompts),
  109. 'dsl': json.loads(self.dsl)
  110. }
  111. @staticmethod
  112. def is_type(record_id, t):
  113. record = UnifiedAgentModel.get_by_id(record_id)
  114. return record and record.type == t