tools.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. import json
  2. from datetime import datetime
  3. from sqlalchemy import ForeignKey
  4. from sqlalchemy.orm import Mapped, mapped_column
  5. from core.tools.entities.common_entities import I18nObject
  6. from core.tools.entities.tool_bundle import ApiToolBundle
  7. from core.tools.entities.tool_entities import ApiProviderSchemaType, WorkflowToolParameterConfiguration
  8. from extensions.ext_database import db
  9. from models.base import Base
  10. from .model import Account, App, Tenant
  11. from .types import StringUUID
  12. class BuiltinToolProvider(Base):
  13. """
  14. This table stores the tool provider information for built-in tools for each tenant.
  15. """
  16. __tablename__ = 'tool_builtin_providers'
  17. __table_args__ = (
  18. db.PrimaryKeyConstraint('id', name='tool_builtin_provider_pkey'),
  19. # one tenant can only have one tool provider with the same name
  20. db.UniqueConstraint('tenant_id', 'provider', name='unique_builtin_tool_provider')
  21. )
  22. # id of the tool provider
  23. id: Mapped[str] = mapped_column(StringUUID, server_default=db.text('uuid_generate_v4()'))
  24. # id of the tenant
  25. tenant_id: Mapped[str] = mapped_column(StringUUID, nullable=True)
  26. # who created this tool provider
  27. user_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
  28. # name of the tool provider
  29. provider: Mapped[str] = mapped_column(db.String(40), nullable=False)
  30. # credential of the tool provider
  31. encrypted_credentials: Mapped[str] = mapped_column(db.Text, nullable=True)
  32. created_at: Mapped[datetime] = mapped_column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
  33. updated_at: Mapped[datetime] = mapped_column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
  34. @property
  35. def credentials(self) -> dict:
  36. return json.loads(self.encrypted_credentials)
  37. class ApiToolProvider(Base):
  38. """
  39. The table stores the api providers.
  40. """
  41. __tablename__ = 'tool_api_providers'
  42. __table_args__ = (
  43. db.PrimaryKeyConstraint('id', name='tool_api_provider_pkey'),
  44. db.UniqueConstraint('name', 'tenant_id', name='unique_api_tool_provider')
  45. )
  46. id = db.Column(StringUUID, server_default=db.text('uuid_generate_v4()'))
  47. # name of the api provider
  48. name = db.Column(db.String(40), nullable=False)
  49. # icon
  50. icon = db.Column(db.String(255), nullable=False)
  51. # original schema
  52. schema = db.Column(db.Text, nullable=False)
  53. schema_type_str = db.Column(db.String(40), nullable=False)
  54. # who created this tool
  55. user_id = db.Column(StringUUID, nullable=False)
  56. # tenant id
  57. tenant_id = db.Column(StringUUID, nullable=False)
  58. # description of the provider
  59. description = db.Column(db.Text, nullable=False)
  60. # json format tools
  61. tools_str = db.Column(db.Text, nullable=False)
  62. # json format credentials
  63. credentials_str = db.Column(db.Text, nullable=False)
  64. # privacy policy
  65. privacy_policy = db.Column(db.String(255), nullable=True)
  66. # custom_disclaimer
  67. custom_disclaimer = db.Column(db.String(255), nullable=True)
  68. created_at = db.Column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
  69. updated_at = db.Column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
  70. @property
  71. def schema_type(self) -> ApiProviderSchemaType:
  72. return ApiProviderSchemaType.value_of(self.schema_type_str)
  73. @property
  74. def tools(self) -> list[ApiToolBundle]:
  75. return [ApiToolBundle(**tool) for tool in json.loads(self.tools_str)]
  76. @property
  77. def credentials(self) -> dict:
  78. return json.loads(self.credentials_str)
  79. @property
  80. def user(self) -> Account | None:
  81. return db.session.query(Account).filter(Account.id == self.user_id).first()
  82. @property
  83. def tenant(self) -> Tenant | None:
  84. return db.session.query(Tenant).filter(Tenant.id == self.tenant_id).first()
  85. class ToolLabelBinding(Base):
  86. """
  87. The table stores the labels for tools.
  88. """
  89. __tablename__ = 'tool_label_bindings'
  90. __table_args__ = (
  91. db.PrimaryKeyConstraint('id', name='tool_label_bind_pkey'),
  92. db.UniqueConstraint('tool_id', 'label_name', name='unique_tool_label_bind'),
  93. )
  94. id: Mapped[str] = mapped_column(StringUUID, server_default=db.text('uuid_generate_v4()'))
  95. # tool id
  96. tool_id: Mapped[str] = mapped_column(db.String(64), nullable=False)
  97. # tool type
  98. tool_type: Mapped[str] = mapped_column(db.String(40), nullable=False)
  99. # label name
  100. label_name: Mapped[str] = mapped_column(db.String(40), nullable=False)
  101. class WorkflowToolProvider(Base):
  102. """
  103. The table stores the workflow providers.
  104. """
  105. __tablename__ = 'tool_workflow_providers'
  106. __table_args__ = (
  107. db.PrimaryKeyConstraint('id', name='tool_workflow_provider_pkey'),
  108. db.UniqueConstraint('name', 'tenant_id', name='unique_workflow_tool_provider'),
  109. db.UniqueConstraint('tenant_id', 'app_id', name='unique_workflow_tool_provider_app_id'),
  110. )
  111. id: Mapped[str] = mapped_column(StringUUID, server_default=db.text('uuid_generate_v4()'))
  112. # name of the workflow provider
  113. name: Mapped[str] = mapped_column(db.String(40), nullable=False)
  114. # label of the workflow provider
  115. label: Mapped[str] = mapped_column(db.String(255), nullable=False, server_default='')
  116. # icon
  117. icon: Mapped[str] = mapped_column(db.String(255), nullable=False)
  118. # app id of the workflow provider
  119. app_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
  120. # version of the workflow provider
  121. version: Mapped[str] = mapped_column(db.String(255), nullable=False, server_default='')
  122. # who created this tool
  123. user_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
  124. # tenant id
  125. tenant_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
  126. # description of the provider
  127. description: Mapped[str] = mapped_column(db.Text, nullable=False)
  128. # parameter configuration
  129. parameter_configuration: Mapped[str] = mapped_column(db.Text, nullable=False, server_default='[]')
  130. # privacy policy
  131. privacy_policy: Mapped[str] = mapped_column(db.String(255), nullable=True, server_default='')
  132. created_at: Mapped[datetime] = mapped_column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
  133. updated_at: Mapped[datetime] = mapped_column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
  134. @property
  135. def user(self) -> Account | None:
  136. return db.session.query(Account).filter(Account.id == self.user_id).first()
  137. @property
  138. def tenant(self) -> Tenant | None:
  139. return db.session.query(Tenant).filter(Tenant.id == self.tenant_id).first()
  140. @property
  141. def parameter_configurations(self) -> list[WorkflowToolParameterConfiguration]:
  142. return [
  143. WorkflowToolParameterConfiguration(**config)
  144. for config in json.loads(self.parameter_configuration)
  145. ]
  146. @property
  147. def app(self) -> App | None:
  148. return db.session.query(App).filter(App.id == self.app_id).first()
  149. class ToolModelInvoke(db.Model):
  150. """
  151. store the invoke logs from tool invoke
  152. """
  153. __tablename__ = "tool_model_invokes"
  154. __table_args__ = (
  155. db.PrimaryKeyConstraint('id', name='tool_model_invoke_pkey'),
  156. )
  157. id = db.Column(StringUUID, server_default=db.text('uuid_generate_v4()'))
  158. # who invoke this tool
  159. user_id = db.Column(StringUUID, nullable=False)
  160. # tenant id
  161. tenant_id = db.Column(StringUUID, nullable=False)
  162. # provider
  163. provider = db.Column(db.String(40), nullable=False)
  164. # type
  165. tool_type = db.Column(db.String(40), nullable=False)
  166. # tool name
  167. tool_name = db.Column(db.String(40), nullable=False)
  168. # invoke parameters
  169. model_parameters = db.Column(db.Text, nullable=False)
  170. # prompt messages
  171. prompt_messages = db.Column(db.Text, nullable=False)
  172. # invoke response
  173. model_response = db.Column(db.Text, nullable=False)
  174. prompt_tokens = db.Column(db.Integer, nullable=False, server_default=db.text('0'))
  175. answer_tokens = db.Column(db.Integer, nullable=False, server_default=db.text('0'))
  176. answer_unit_price = db.Column(db.Numeric(10, 4), nullable=False)
  177. answer_price_unit = db.Column(db.Numeric(10, 7), nullable=False, server_default=db.text('0.001'))
  178. provider_response_latency = db.Column(db.Float, nullable=False, server_default=db.text('0'))
  179. total_price = db.Column(db.Numeric(10, 7))
  180. currency = db.Column(db.String(255), nullable=False)
  181. created_at = db.Column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
  182. updated_at = db.Column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
  183. class ToolConversationVariables(db.Model):
  184. """
  185. store the conversation variables from tool invoke
  186. """
  187. __tablename__ = "tool_conversation_variables"
  188. __table_args__ = (
  189. db.PrimaryKeyConstraint('id', name='tool_conversation_variables_pkey'),
  190. # add index for user_id and conversation_id
  191. db.Index('user_id_idx', 'user_id'),
  192. db.Index('conversation_id_idx', 'conversation_id'),
  193. )
  194. id = db.Column(StringUUID, server_default=db.text('uuid_generate_v4()'))
  195. # conversation user id
  196. user_id = db.Column(StringUUID, nullable=False)
  197. # tenant id
  198. tenant_id = db.Column(StringUUID, nullable=False)
  199. # conversation id
  200. conversation_id = db.Column(StringUUID, nullable=False)
  201. # variables pool
  202. variables_str = db.Column(db.Text, nullable=False)
  203. created_at = db.Column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
  204. updated_at = db.Column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
  205. @property
  206. def variables(self) -> dict:
  207. return json.loads(self.variables_str)
  208. class ToolFile(Base):
  209. """
  210. store the file created by agent
  211. """
  212. __tablename__ = "tool_files"
  213. __table_args__ = (
  214. db.PrimaryKeyConstraint('id', name='tool_file_pkey'),
  215. # add index for conversation_id
  216. db.Index('tool_file_conversation_id_idx', 'conversation_id'),
  217. )
  218. id: Mapped[str] = mapped_column(StringUUID, server_default=db.text('uuid_generate_v4()'))
  219. # conversation user id
  220. user_id: Mapped[str] = mapped_column(StringUUID)
  221. # tenant id
  222. tenant_id: Mapped[str] = mapped_column(StringUUID)
  223. # conversation id
  224. conversation_id: Mapped[str] = mapped_column(StringUUID, nullable=True)
  225. # file key
  226. file_key: Mapped[str] = mapped_column(db.String(255), nullable=False)
  227. # mime type
  228. mimetype: Mapped[str] = mapped_column(db.String(255), nullable=False)
  229. # original url
  230. original_url: Mapped[str] = mapped_column(db.String(2048), nullable=True)