tools.py 12 KB

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