tools.py 12 KB

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