tools.py 12 KB

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