tools.py 11 KB

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