tools.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import json
  2. from sqlalchemy import ForeignKey
  3. from sqlalchemy.dialects.postgresql import UUID
  4. from core.tools.entities.common_entities import I18nObject
  5. from core.tools.entities.tool_bundle import ApiBasedToolBundle
  6. from core.tools.entities.tool_entities import ApiProviderSchemaType
  7. from extensions.ext_database import db
  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(UUID, server_default=db.text('uuid_generate_v4()'))
  21. # id of the tenant
  22. tenant_id = db.Column(UUID, nullable=True)
  23. # who created this tool provider
  24. user_id = db.Column(UUID, 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(UUID, server_default=db.text('uuid_generate_v4()'))
  45. # id of the app
  46. app_id = db.Column(UUID, ForeignKey('apps.id'), nullable=False)
  47. # who published this tool
  48. user_id = db.Column(UUID, 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(UUID, 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(UUID, nullable=False)
  88. # tenant id
  89. tenant_id = db.Column(UUID, 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. created_at = db.Column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
  99. updated_at = db.Column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
  100. @property
  101. def schema_type(self) -> ApiProviderSchemaType:
  102. return ApiProviderSchemaType.value_of(self.schema_type_str)
  103. @property
  104. def tools(self) -> list[ApiBasedToolBundle]:
  105. return [ApiBasedToolBundle(**tool) for tool in json.loads(self.tools_str)]
  106. @property
  107. def credentials(self) -> dict:
  108. return json.loads(self.credentials_str)
  109. @property
  110. def user(self) -> Account:
  111. return db.session.query(Account).filter(Account.id == self.user_id).first()
  112. @property
  113. def tenant(self) -> Tenant:
  114. return db.session.query(Tenant).filter(Tenant.id == self.tenant_id).first()
  115. class ToolModelInvoke(db.Model):
  116. """
  117. store the invoke logs from tool invoke
  118. """
  119. __tablename__ = "tool_model_invokes"
  120. __table_args__ = (
  121. db.PrimaryKeyConstraint('id', name='tool_model_invoke_pkey'),
  122. )
  123. id = db.Column(UUID, server_default=db.text('uuid_generate_v4()'))
  124. # who invoke this tool
  125. user_id = db.Column(UUID, nullable=False)
  126. # tenant id
  127. tenant_id = db.Column(UUID, nullable=False)
  128. # provider
  129. provider = db.Column(db.String(40), nullable=False)
  130. # type
  131. tool_type = db.Column(db.String(40), nullable=False)
  132. # tool name
  133. tool_name = db.Column(db.String(40), nullable=False)
  134. # invoke parameters
  135. model_parameters = db.Column(db.Text, nullable=False)
  136. # prompt messages
  137. prompt_messages = db.Column(db.Text, nullable=False)
  138. # invoke response
  139. model_response = db.Column(db.Text, nullable=False)
  140. prompt_tokens = db.Column(db.Integer, nullable=False, server_default=db.text('0'))
  141. answer_tokens = db.Column(db.Integer, nullable=False, server_default=db.text('0'))
  142. answer_unit_price = db.Column(db.Numeric(10, 4), nullable=False)
  143. answer_price_unit = db.Column(db.Numeric(10, 7), nullable=False, server_default=db.text('0.001'))
  144. provider_response_latency = db.Column(db.Float, nullable=False, server_default=db.text('0'))
  145. total_price = db.Column(db.Numeric(10, 7))
  146. currency = db.Column(db.String(255), nullable=False)
  147. created_at = db.Column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
  148. updated_at = db.Column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
  149. class ToolConversationVariables(db.Model):
  150. """
  151. store the conversation variables from tool invoke
  152. """
  153. __tablename__ = "tool_conversation_variables"
  154. __table_args__ = (
  155. db.PrimaryKeyConstraint('id', name='tool_conversation_variables_pkey'),
  156. # add index for user_id and conversation_id
  157. db.Index('user_id_idx', 'user_id'),
  158. db.Index('conversation_id_idx', 'conversation_id'),
  159. )
  160. id = db.Column(UUID, server_default=db.text('uuid_generate_v4()'))
  161. # conversation user id
  162. user_id = db.Column(UUID, nullable=False)
  163. # tenant id
  164. tenant_id = db.Column(UUID, nullable=False)
  165. # conversation id
  166. conversation_id = db.Column(UUID, nullable=False)
  167. # variables pool
  168. variables_str = db.Column(db.Text, nullable=False)
  169. created_at = db.Column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
  170. updated_at = db.Column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
  171. @property
  172. def variables(self) -> dict:
  173. return json.loads(self.variables_str)
  174. class ToolFile(db.Model):
  175. """
  176. store the file created by agent
  177. """
  178. __tablename__ = "tool_files"
  179. __table_args__ = (
  180. db.PrimaryKeyConstraint('id', name='tool_file_pkey'),
  181. # add index for conversation_id
  182. db.Index('tool_file_conversation_id_idx', 'conversation_id'),
  183. )
  184. id = db.Column(UUID, server_default=db.text('uuid_generate_v4()'))
  185. # conversation user id
  186. user_id = db.Column(UUID, nullable=False)
  187. # tenant id
  188. tenant_id = db.Column(UUID, nullable=False)
  189. # conversation id
  190. conversation_id = db.Column(UUID, nullable=True)
  191. # file key
  192. file_key = db.Column(db.String(255), nullable=False)
  193. # mime type
  194. mimetype = db.Column(db.String(255), nullable=False)
  195. # original url
  196. original_url = db.Column(db.String(255), nullable=True)