tools.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import json
  2. from enum import Enum
  3. from typing import List
  4. from sqlalchemy.dialects.postgresql import UUID
  5. from sqlalchemy import ForeignKey
  6. from extensions.ext_database import db
  7. from core.tools.entities.tool_bundle import ApiBasedToolBundle
  8. from core.tools.entities.common_entities import I18nObject
  9. from core.tools.entities.tool_entities import ApiProviderSchemaType, ToolRuntimeVariablePool
  10. from models.model import Tenant, Account, App
  11. class BuiltinToolProvider(db.Model):
  12. """
  13. This table stores the tool provider information for built-in tools for each tenant.
  14. """
  15. __tablename__ = 'tool_builtin_providers'
  16. __table_args__ = (
  17. db.PrimaryKeyConstraint('id', name='tool_builtin_provider_pkey'),
  18. # one tenant can only have one tool provider with the same name
  19. db.UniqueConstraint('tenant_id', 'provider', name='unique_builtin_tool_provider')
  20. )
  21. # id of the tool provider
  22. id = db.Column(UUID, server_default=db.text('uuid_generate_v4()'))
  23. # id of the tenant
  24. tenant_id = db.Column(UUID, nullable=True)
  25. # who created this tool provider
  26. user_id = db.Column(UUID, nullable=False)
  27. # name of the tool provider
  28. provider = db.Column(db.String(40), nullable=False)
  29. # credential of the tool provider
  30. encrypted_credentials = db.Column(db.Text, nullable=True)
  31. created_at = db.Column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
  32. updated_at = db.Column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
  33. @property
  34. def credentials(self) -> dict:
  35. return json.loads(self.encrypted_credentials)
  36. class PublishedAppTool(db.Model):
  37. """
  38. The table stores the apps published as a tool for each person.
  39. """
  40. __tablename__ = 'tool_published_apps'
  41. __table_args__ = (
  42. db.PrimaryKeyConstraint('id', name='published_app_tool_pkey'),
  43. db.UniqueConstraint('app_id', 'user_id', name='unique_published_app_tool')
  44. )
  45. # id of the tool provider
  46. id = db.Column(UUID, server_default=db.text('uuid_generate_v4()'))
  47. # id of the app
  48. app_id = db.Column(UUID, ForeignKey('apps.id'), nullable=False)
  49. # who published this tool
  50. user_id = db.Column(UUID, nullable=False)
  51. # description of the tool, stored in i18n format, for human
  52. description = db.Column(db.Text, nullable=False)
  53. # llm_description of the tool, for LLM
  54. llm_description = db.Column(db.Text, nullable=False)
  55. # query decription, query will be seem as a parameter of the tool, to describe this parameter to llm, we need this field
  56. query_description = db.Column(db.Text, nullable=False)
  57. # query name, the name of the query parameter
  58. query_name = db.Column(db.String(40), nullable=False)
  59. # name of the tool provider
  60. tool_name = db.Column(db.String(40), nullable=False)
  61. # author
  62. author = db.Column(db.String(40), nullable=False)
  63. created_at = db.Column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
  64. updated_at = db.Column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
  65. @property
  66. def description_i18n(self) -> I18nObject:
  67. return I18nObject(**json.loads(self.description))
  68. @property
  69. def app(self) -> App:
  70. return db.session.query(App).filter(App.id == self.app_id).first()
  71. class ApiToolProvider(db.Model):
  72. """
  73. The table stores the api providers.
  74. """
  75. __tablename__ = 'tool_api_providers'
  76. __table_args__ = (
  77. db.PrimaryKeyConstraint('id', name='tool_api_provider_pkey'),
  78. )
  79. id = db.Column(UUID, 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(UUID, nullable=False)
  89. # tanent id
  90. tenant_id = db.Column(UUID, 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. created_at = db.Column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
  100. updated_at = db.Column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
  101. @property
  102. def schema_type(self) -> ApiProviderSchemaType:
  103. return ApiProviderSchemaType.value_of(self.schema_type_str)
  104. @property
  105. def tools(self) -> List[ApiBasedToolBundle]:
  106. return [ApiBasedToolBundle(**tool) for tool in json.loads(self.tools_str)]
  107. @property
  108. def credentials(self) -> dict:
  109. return json.loads(self.credentials_str)
  110. @property
  111. def is_taned(self) -> bool:
  112. return self.tenant_id is not None
  113. @property
  114. def user(self) -> Account:
  115. return db.session.query(Account).filter(Account.id == self.user_id).first()
  116. @property
  117. def tanent(self) -> Tenant:
  118. return db.session.query(Tenant).filter(Tenant.id == self.tenant_id).first()
  119. class ToolModelInvoke(db.Model):
  120. """
  121. store the invoke logs from tool invoke
  122. """
  123. __tablename__ = "tool_model_invokes"
  124. __table_args__ = (
  125. db.PrimaryKeyConstraint('id', name='tool_model_invoke_pkey'),
  126. )
  127. id = db.Column(UUID, server_default=db.text('uuid_generate_v4()'))
  128. # who invoke this tool
  129. user_id = db.Column(UUID, nullable=False)
  130. # tanent id
  131. tenant_id = db.Column(UUID, nullable=False)
  132. # provider
  133. provider = db.Column(db.String(40), nullable=False)
  134. # type
  135. tool_type = db.Column(db.String(40), nullable=False)
  136. # tool name
  137. tool_name = db.Column(db.String(40), nullable=False)
  138. # invoke parameters
  139. model_parameters = db.Column(db.Text, nullable=False)
  140. # prompt messages
  141. prompt_messages = db.Column(db.Text, nullable=False)
  142. # invoke response
  143. model_response = db.Column(db.Text, nullable=False)
  144. prompt_tokens = db.Column(db.Integer, nullable=False, server_default=db.text('0'))
  145. answer_tokens = db.Column(db.Integer, nullable=False, server_default=db.text('0'))
  146. answer_unit_price = db.Column(db.Numeric(10, 4), nullable=False)
  147. answer_price_unit = db.Column(db.Numeric(10, 7), nullable=False, server_default=db.text('0.001'))
  148. provider_response_latency = db.Column(db.Float, nullable=False, server_default=db.text('0'))
  149. total_price = db.Column(db.Numeric(10, 7))
  150. currency = db.Column(db.String(255), nullable=False)
  151. created_at = db.Column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
  152. updated_at = db.Column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
  153. class ToolConversationVariables(db.Model):
  154. """
  155. store the conversation variables from tool invoke
  156. """
  157. __tablename__ = "tool_conversation_variables"
  158. __table_args__ = (
  159. db.PrimaryKeyConstraint('id', name='tool_conversation_variables_pkey'),
  160. # add index for user_id and conversation_id
  161. db.Index('user_id_idx', 'user_id'),
  162. db.Index('conversation_id_idx', 'conversation_id'),
  163. )
  164. id = db.Column(UUID, server_default=db.text('uuid_generate_v4()'))
  165. # conversation user id
  166. user_id = db.Column(UUID, nullable=False)
  167. # tanent id
  168. tenant_id = db.Column(UUID, nullable=False)
  169. # conversation id
  170. conversation_id = db.Column(UUID, nullable=False)
  171. # variables pool
  172. variables_str = db.Column(db.Text, nullable=False)
  173. created_at = db.Column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
  174. updated_at = db.Column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
  175. @property
  176. def variables(self) -> dict:
  177. return json.loads(self.variables_str)
  178. class ToolFile(db.Model):
  179. """
  180. store the file created by agent
  181. """
  182. __tablename__ = "tool_files"
  183. __table_args__ = (
  184. db.PrimaryKeyConstraint('id', name='tool_file_pkey'),
  185. )
  186. id = db.Column(UUID, server_default=db.text('uuid_generate_v4()'))
  187. # conversation user id
  188. user_id = db.Column(UUID, nullable=False)
  189. # tanent id
  190. tenant_id = db.Column(UUID, nullable=False)
  191. # conversation id
  192. conversation_id = db.Column(UUID, nullable=False)
  193. # file key
  194. file_key = db.Column(db.String(255), nullable=False)
  195. # mime type
  196. mimetype = db.Column(db.String(255), nullable=False)
  197. # original url
  198. original_url = db.Column(db.String(255), nullable=True)