tools.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. import json
  2. from typing import List
  3. from sqlalchemy import ForeignKey
  4. from sqlalchemy.dialects.postgresql import UUID
  5. from core.tools.entities.common_entities import I18nObject
  6. from core.tools.entities.tool_bundle import ApiBasedToolBundle
  7. from core.tools.entities.tool_entities import ApiProviderSchemaType
  8. from extensions.ext_database import db
  9. from models.model import Account, App, Tenant
  10. class BuiltinToolProvider(db.Model):
  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 = db.Column(UUID, server_default=db.text('uuid_generate_v4()'))
  22. # id of the tenant
  23. tenant_id = db.Column(UUID, nullable=True)
  24. # who created this tool provider
  25. user_id = db.Column(UUID, nullable=False)
  26. # name of the tool provider
  27. provider = db.Column(db.String(40), nullable=False)
  28. # credential of the tool provider
  29. encrypted_credentials = db.Column(db.Text, nullable=True)
  30. created_at = db.Column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
  31. updated_at = db.Column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
  32. @property
  33. def credentials(self) -> dict:
  34. return json.loads(self.encrypted_credentials)
  35. class PublishedAppTool(db.Model):
  36. """
  37. The table stores the apps published as a tool for each person.
  38. """
  39. __tablename__ = 'tool_published_apps'
  40. __table_args__ = (
  41. db.PrimaryKeyConstraint('id', name='published_app_tool_pkey'),
  42. db.UniqueConstraint('app_id', 'user_id', name='unique_published_app_tool')
  43. )
  44. # id of the tool provider
  45. id = db.Column(UUID, server_default=db.text('uuid_generate_v4()'))
  46. # id of the app
  47. app_id = db.Column(UUID, ForeignKey('apps.id'), nullable=False)
  48. # who published this tool
  49. user_id = db.Column(UUID, nullable=False)
  50. # description of the tool, stored in i18n format, for human
  51. description = db.Column(db.Text, nullable=False)
  52. # llm_description of the tool, for LLM
  53. llm_description = db.Column(db.Text, nullable=False)
  54. # query decription, query will be seem as a parameter of the tool, to describe this parameter to llm, we need this field
  55. query_description = db.Column(db.Text, nullable=False)
  56. # query name, the name of the query parameter
  57. query_name = db.Column(db.String(40), nullable=False)
  58. # name of the tool provider
  59. tool_name = db.Column(db.String(40), nullable=False)
  60. # author
  61. author = db.Column(db.String(40), nullable=False)
  62. created_at = db.Column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
  63. updated_at = db.Column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
  64. @property
  65. def description_i18n(self) -> I18nObject:
  66. return I18nObject(**json.loads(self.description))
  67. @property
  68. def app(self) -> App:
  69. return db.session.query(App).filter(App.id == self.app_id).first()
  70. class ApiToolProvider(db.Model):
  71. """
  72. The table stores the api providers.
  73. """
  74. __tablename__ = 'tool_api_providers'
  75. __table_args__ = (
  76. db.PrimaryKeyConstraint('id', name='tool_api_provider_pkey'),
  77. db.UniqueConstraint('name', 'tenant_id', name='unique_api_tool_provider')
  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. # tenant 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 tenant(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. # tenant 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. # tenant 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. # add index for conversation_id
  186. db.Index('tool_file_conversation_id_idx', 'conversation_id'),
  187. )
  188. id = db.Column(UUID, server_default=db.text('uuid_generate_v4()'))
  189. # conversation user id
  190. user_id = db.Column(UUID, nullable=False)
  191. # tenant id
  192. tenant_id = db.Column(UUID, nullable=False)
  193. # conversation id
  194. conversation_id = db.Column(UUID, nullable=False)
  195. # file key
  196. file_key = db.Column(db.String(255), nullable=False)
  197. # mime type
  198. mimetype = db.Column(db.String(255), nullable=False)
  199. # original url
  200. original_url = db.Column(db.String(255), nullable=True)