tools.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 decription, 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 is_taned(self) -> bool:
  111. return self.tenant_id is not None
  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 ToolModelInvoke(db.Model):
  119. """
  120. store the invoke logs from tool invoke
  121. """
  122. __tablename__ = "tool_model_invokes"
  123. __table_args__ = (
  124. db.PrimaryKeyConstraint('id', name='tool_model_invoke_pkey'),
  125. )
  126. id = db.Column(UUID, server_default=db.text('uuid_generate_v4()'))
  127. # who invoke this tool
  128. user_id = db.Column(UUID, nullable=False)
  129. # tenant id
  130. tenant_id = db.Column(UUID, nullable=False)
  131. # provider
  132. provider = db.Column(db.String(40), nullable=False)
  133. # type
  134. tool_type = db.Column(db.String(40), nullable=False)
  135. # tool name
  136. tool_name = db.Column(db.String(40), nullable=False)
  137. # invoke parameters
  138. model_parameters = db.Column(db.Text, nullable=False)
  139. # prompt messages
  140. prompt_messages = db.Column(db.Text, nullable=False)
  141. # invoke response
  142. model_response = db.Column(db.Text, nullable=False)
  143. prompt_tokens = db.Column(db.Integer, nullable=False, server_default=db.text('0'))
  144. answer_tokens = db.Column(db.Integer, nullable=False, server_default=db.text('0'))
  145. answer_unit_price = db.Column(db.Numeric(10, 4), nullable=False)
  146. answer_price_unit = db.Column(db.Numeric(10, 7), nullable=False, server_default=db.text('0.001'))
  147. provider_response_latency = db.Column(db.Float, nullable=False, server_default=db.text('0'))
  148. total_price = db.Column(db.Numeric(10, 7))
  149. currency = db.Column(db.String(255), nullable=False)
  150. created_at = db.Column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
  151. updated_at = db.Column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
  152. class ToolConversationVariables(db.Model):
  153. """
  154. store the conversation variables from tool invoke
  155. """
  156. __tablename__ = "tool_conversation_variables"
  157. __table_args__ = (
  158. db.PrimaryKeyConstraint('id', name='tool_conversation_variables_pkey'),
  159. # add index for user_id and conversation_id
  160. db.Index('user_id_idx', 'user_id'),
  161. db.Index('conversation_id_idx', 'conversation_id'),
  162. )
  163. id = db.Column(UUID, server_default=db.text('uuid_generate_v4()'))
  164. # conversation user id
  165. user_id = db.Column(UUID, nullable=False)
  166. # tenant id
  167. tenant_id = db.Column(UUID, nullable=False)
  168. # conversation id
  169. conversation_id = db.Column(UUID, nullable=False)
  170. # variables pool
  171. variables_str = db.Column(db.Text, nullable=False)
  172. created_at = db.Column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
  173. updated_at = db.Column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
  174. @property
  175. def variables(self) -> dict:
  176. return json.loads(self.variables_str)
  177. class ToolFile(db.Model):
  178. """
  179. store the file created by agent
  180. """
  181. __tablename__ = "tool_files"
  182. __table_args__ = (
  183. db.PrimaryKeyConstraint('id', name='tool_file_pkey'),
  184. # add index for conversation_id
  185. db.Index('tool_file_conversation_id_idx', 'conversation_id'),
  186. )
  187. id = db.Column(UUID, server_default=db.text('uuid_generate_v4()'))
  188. # conversation user id
  189. user_id = db.Column(UUID, nullable=False)
  190. # tenant id
  191. tenant_id = db.Column(UUID, nullable=False)
  192. # conversation id
  193. conversation_id = db.Column(UUID, nullable=False)
  194. # file key
  195. file_key = db.Column(db.String(255), nullable=False)
  196. # mime type
  197. mimetype = db.Column(db.String(255), nullable=False)
  198. # original url
  199. original_url = db.Column(db.String(255), nullable=True)