tools.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. import json
  2. from enum import Enum
  3. from typing import List
  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, ToolRuntimeVariablePool
  7. from extensions.ext_database import db
  8. from models.model import Account, App, Tenant
  9. from sqlalchemy import ForeignKey
  10. from sqlalchemy.dialects.postgresql import UUID
  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. db.UniqueConstraint('name', 'tenant_id', name='unique_api_tool_provider')
  79. )
  80. id = db.Column(UUID, server_default=db.text('uuid_generate_v4()'))
  81. # name of the api provider
  82. name = db.Column(db.String(40), nullable=False)
  83. # icon
  84. icon = db.Column(db.String(255), nullable=False)
  85. # original schema
  86. schema = db.Column(db.Text, nullable=False)
  87. schema_type_str = db.Column(db.String(40), nullable=False)
  88. # who created this tool
  89. user_id = db.Column(UUID, nullable=False)
  90. # tenant id
  91. tenant_id = db.Column(UUID, nullable=False)
  92. # description of the provider
  93. description = db.Column(db.Text, nullable=False)
  94. # json format tools
  95. tools_str = db.Column(db.Text, nullable=False)
  96. # json format credentials
  97. credentials_str = db.Column(db.Text, nullable=False)
  98. # privacy policy
  99. privacy_policy = db.Column(db.String(255), nullable=True)
  100. created_at = db.Column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
  101. updated_at = db.Column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
  102. @property
  103. def schema_type(self) -> ApiProviderSchemaType:
  104. return ApiProviderSchemaType.value_of(self.schema_type_str)
  105. @property
  106. def tools(self) -> List[ApiBasedToolBundle]:
  107. return [ApiBasedToolBundle(**tool) for tool in json.loads(self.tools_str)]
  108. @property
  109. def credentials(self) -> dict:
  110. return json.loads(self.credentials_str)
  111. @property
  112. def is_taned(self) -> bool:
  113. return self.tenant_id is not None
  114. @property
  115. def user(self) -> Account:
  116. return db.session.query(Account).filter(Account.id == self.user_id).first()
  117. @property
  118. def tenant(self) -> Tenant:
  119. return db.session.query(Tenant).filter(Tenant.id == self.tenant_id).first()
  120. class ToolModelInvoke(db.Model):
  121. """
  122. store the invoke logs from tool invoke
  123. """
  124. __tablename__ = "tool_model_invokes"
  125. __table_args__ = (
  126. db.PrimaryKeyConstraint('id', name='tool_model_invoke_pkey'),
  127. )
  128. id = db.Column(UUID, server_default=db.text('uuid_generate_v4()'))
  129. # who invoke this tool
  130. user_id = db.Column(UUID, nullable=False)
  131. # tenant id
  132. tenant_id = db.Column(UUID, nullable=False)
  133. # provider
  134. provider = db.Column(db.String(40), nullable=False)
  135. # type
  136. tool_type = db.Column(db.String(40), nullable=False)
  137. # tool name
  138. tool_name = db.Column(db.String(40), nullable=False)
  139. # invoke parameters
  140. model_parameters = db.Column(db.Text, nullable=False)
  141. # prompt messages
  142. prompt_messages = db.Column(db.Text, nullable=False)
  143. # invoke response
  144. model_response = db.Column(db.Text, nullable=False)
  145. prompt_tokens = db.Column(db.Integer, nullable=False, server_default=db.text('0'))
  146. answer_tokens = db.Column(db.Integer, nullable=False, server_default=db.text('0'))
  147. answer_unit_price = db.Column(db.Numeric(10, 4), nullable=False)
  148. answer_price_unit = db.Column(db.Numeric(10, 7), nullable=False, server_default=db.text('0.001'))
  149. provider_response_latency = db.Column(db.Float, nullable=False, server_default=db.text('0'))
  150. total_price = db.Column(db.Numeric(10, 7))
  151. currency = db.Column(db.String(255), nullable=False)
  152. created_at = db.Column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
  153. updated_at = db.Column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
  154. class ToolConversationVariables(db.Model):
  155. """
  156. store the conversation variables from tool invoke
  157. """
  158. __tablename__ = "tool_conversation_variables"
  159. __table_args__ = (
  160. db.PrimaryKeyConstraint('id', name='tool_conversation_variables_pkey'),
  161. # add index for user_id and conversation_id
  162. db.Index('user_id_idx', 'user_id'),
  163. db.Index('conversation_id_idx', 'conversation_id'),
  164. )
  165. id = db.Column(UUID, server_default=db.text('uuid_generate_v4()'))
  166. # conversation user id
  167. user_id = db.Column(UUID, nullable=False)
  168. # tenant id
  169. tenant_id = db.Column(UUID, nullable=False)
  170. # conversation id
  171. conversation_id = db.Column(UUID, nullable=False)
  172. # variables pool
  173. variables_str = db.Column(db.Text, nullable=False)
  174. created_at = db.Column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
  175. updated_at = db.Column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
  176. @property
  177. def variables(self) -> dict:
  178. return json.loads(self.variables_str)
  179. class ToolFile(db.Model):
  180. """
  181. store the file created by agent
  182. """
  183. __tablename__ = "tool_files"
  184. __table_args__ = (
  185. db.PrimaryKeyConstraint('id', name='tool_file_pkey'),
  186. # add index for conversation_id
  187. db.Index('tool_file_conversation_id_idx', 'conversation_id'),
  188. )
  189. id = db.Column(UUID, server_default=db.text('uuid_generate_v4()'))
  190. # conversation user id
  191. user_id = db.Column(UUID, nullable=False)
  192. # tenant id
  193. tenant_id = db.Column(UUID, nullable=False)
  194. # conversation id
  195. conversation_id = db.Column(UUID, nullable=False)
  196. # file key
  197. file_key = db.Column(db.String(255), nullable=False)
  198. # mime type
  199. mimetype = db.Column(db.String(255), nullable=False)
  200. # original url
  201. original_url = db.Column(db.String(255), nullable=True)