agent_model.py 4.3 KB

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