agent_model.py 4.3 KB

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