tools.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 .model import Account, App, Tenant
  8. from .types import StringUUID
  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 [WorkflowToolParameterConfiguration(**config) for config in json.loads(self.parameter_configuration)]
  178. @property
  179. def app(self) -> App:
  180. return db.session.query(App).filter(App.id == self.app_id).first()
  181. class ToolModelInvoke(db.Model):
  182. """
  183. store the invoke logs from tool invoke
  184. """
  185. __tablename__ = "tool_model_invokes"
  186. __table_args__ = (db.PrimaryKeyConstraint("id", name="tool_model_invoke_pkey"),)
  187. id = db.Column(StringUUID, server_default=db.text("uuid_generate_v4()"))
  188. # who invoke this tool
  189. user_id = db.Column(StringUUID, nullable=False)
  190. # tenant id
  191. tenant_id = db.Column(StringUUID, nullable=False)
  192. # provider
  193. provider = db.Column(db.String(40), nullable=False)
  194. # type
  195. tool_type = db.Column(db.String(40), nullable=False)
  196. # tool name
  197. tool_name = db.Column(db.String(40), nullable=False)
  198. # invoke parameters
  199. model_parameters = db.Column(db.Text, nullable=False)
  200. # prompt messages
  201. prompt_messages = db.Column(db.Text, nullable=False)
  202. # invoke response
  203. model_response = db.Column(db.Text, nullable=False)
  204. prompt_tokens = db.Column(db.Integer, nullable=False, server_default=db.text("0"))
  205. answer_tokens = db.Column(db.Integer, nullable=False, server_default=db.text("0"))
  206. answer_unit_price = db.Column(db.Numeric(10, 4), nullable=False)
  207. answer_price_unit = db.Column(db.Numeric(10, 7), nullable=False, server_default=db.text("0.001"))
  208. provider_response_latency = db.Column(db.Float, nullable=False, server_default=db.text("0"))
  209. total_price = db.Column(db.Numeric(10, 7))
  210. currency = db.Column(db.String(255), nullable=False)
  211. created_at = db.Column(db.DateTime, nullable=False, server_default=db.text("CURRENT_TIMESTAMP(0)"))
  212. updated_at = db.Column(db.DateTime, nullable=False, server_default=db.text("CURRENT_TIMESTAMP(0)"))
  213. class ToolConversationVariables(db.Model):
  214. """
  215. store the conversation variables from tool invoke
  216. """
  217. __tablename__ = "tool_conversation_variables"
  218. __table_args__ = (
  219. db.PrimaryKeyConstraint("id", name="tool_conversation_variables_pkey"),
  220. # add index for user_id and conversation_id
  221. db.Index("user_id_idx", "user_id"),
  222. db.Index("conversation_id_idx", "conversation_id"),
  223. )
  224. id = db.Column(StringUUID, server_default=db.text("uuid_generate_v4()"))
  225. # conversation user id
  226. user_id = db.Column(StringUUID, nullable=False)
  227. # tenant id
  228. tenant_id = db.Column(StringUUID, nullable=False)
  229. # conversation id
  230. conversation_id = db.Column(StringUUID, nullable=False)
  231. # variables pool
  232. variables_str = db.Column(db.Text, nullable=False)
  233. created_at = db.Column(db.DateTime, nullable=False, server_default=db.text("CURRENT_TIMESTAMP(0)"))
  234. updated_at = db.Column(db.DateTime, nullable=False, server_default=db.text("CURRENT_TIMESTAMP(0)"))
  235. @property
  236. def variables(self) -> dict:
  237. return json.loads(self.variables_str)
  238. class ToolFile(db.Model):
  239. """
  240. store the file created by agent
  241. """
  242. __tablename__ = "tool_files"
  243. __table_args__ = (
  244. db.PrimaryKeyConstraint("id", name="tool_file_pkey"),
  245. # add index for conversation_id
  246. db.Index("tool_file_conversation_id_idx", "conversation_id"),
  247. )
  248. id = db.Column(StringUUID, server_default=db.text("uuid_generate_v4()"))
  249. # conversation user id
  250. user_id = db.Column(StringUUID, nullable=False)
  251. # tenant id
  252. tenant_id = db.Column(StringUUID, nullable=False)
  253. # conversation id
  254. conversation_id = db.Column(StringUUID, nullable=True)
  255. # file key
  256. file_key = db.Column(db.String(255), nullable=False)
  257. # mime type
  258. mimetype = db.Column(db.String(255), nullable=False)
  259. # original url
  260. original_url = db.Column(db.String(2048), nullable=True)