plugin.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. from flask_restful import Resource
  2. from controllers.console.wraps import setup_required
  3. from controllers.inner_api import api
  4. from controllers.inner_api.plugin.wraps import get_user_tenant, plugin_data
  5. from controllers.inner_api.wraps import plugin_inner_api_only
  6. from core.file.helpers import get_signed_file_url_for_plugin
  7. from core.plugin.backwards_invocation.app import PluginAppBackwardsInvocation
  8. from core.plugin.backwards_invocation.base import BaseBackwardsInvocationResponse
  9. from core.plugin.backwards_invocation.encrypt import PluginEncrypter
  10. from core.plugin.backwards_invocation.model import PluginModelBackwardsInvocation
  11. from core.plugin.backwards_invocation.node import PluginNodeBackwardsInvocation
  12. from core.plugin.backwards_invocation.tool import PluginToolBackwardsInvocation
  13. from core.plugin.entities.request import (
  14. RequestInvokeApp,
  15. RequestInvokeEncrypt,
  16. RequestInvokeLLM,
  17. RequestInvokeModeration,
  18. RequestInvokeParameterExtractorNode,
  19. RequestInvokeQuestionClassifierNode,
  20. RequestInvokeRerank,
  21. RequestInvokeSpeech2Text,
  22. RequestInvokeSummary,
  23. RequestInvokeTextEmbedding,
  24. RequestInvokeTool,
  25. RequestInvokeTTS,
  26. RequestRequestUploadFile,
  27. )
  28. from core.tools.entities.tool_entities import ToolProviderType
  29. from libs.helper import compact_generate_response
  30. from models.account import Account, Tenant
  31. from models.model import EndUser
  32. class PluginInvokeLLMApi(Resource):
  33. @setup_required
  34. @plugin_inner_api_only
  35. @get_user_tenant
  36. @plugin_data(payload_type=RequestInvokeLLM)
  37. def post(self, user_model: Account | EndUser, tenant_model: Tenant, payload: RequestInvokeLLM):
  38. def generator():
  39. response = PluginModelBackwardsInvocation.invoke_llm(user_model.id, tenant_model, payload)
  40. return PluginModelBackwardsInvocation.convert_to_event_stream(response)
  41. return compact_generate_response(generator())
  42. class PluginInvokeTextEmbeddingApi(Resource):
  43. @setup_required
  44. @plugin_inner_api_only
  45. @get_user_tenant
  46. @plugin_data(payload_type=RequestInvokeTextEmbedding)
  47. def post(self, user_model: Account | EndUser, tenant_model: Tenant, payload: RequestInvokeTextEmbedding):
  48. try:
  49. return BaseBackwardsInvocationResponse(
  50. data=PluginModelBackwardsInvocation.invoke_text_embedding(
  51. user_id=user_model.id,
  52. tenant=tenant_model,
  53. payload=payload,
  54. )
  55. ).model_dump()
  56. except Exception as e:
  57. return BaseBackwardsInvocationResponse(error=str(e)).model_dump()
  58. class PluginInvokeRerankApi(Resource):
  59. @setup_required
  60. @plugin_inner_api_only
  61. @get_user_tenant
  62. @plugin_data(payload_type=RequestInvokeRerank)
  63. def post(self, user_model: Account | EndUser, tenant_model: Tenant, payload: RequestInvokeRerank):
  64. try:
  65. return BaseBackwardsInvocationResponse(
  66. data=PluginModelBackwardsInvocation.invoke_rerank(
  67. user_id=user_model.id,
  68. tenant=tenant_model,
  69. payload=payload,
  70. )
  71. ).model_dump()
  72. except Exception as e:
  73. return BaseBackwardsInvocationResponse(error=str(e)).model_dump()
  74. class PluginInvokeTTSApi(Resource):
  75. @setup_required
  76. @plugin_inner_api_only
  77. @get_user_tenant
  78. @plugin_data(payload_type=RequestInvokeTTS)
  79. def post(self, user_model: Account | EndUser, tenant_model: Tenant, payload: RequestInvokeTTS):
  80. def generator():
  81. response = PluginModelBackwardsInvocation.invoke_tts(
  82. user_id=user_model.id,
  83. tenant=tenant_model,
  84. payload=payload,
  85. )
  86. return PluginModelBackwardsInvocation.convert_to_event_stream(response)
  87. return compact_generate_response(generator())
  88. class PluginInvokeSpeech2TextApi(Resource):
  89. @setup_required
  90. @plugin_inner_api_only
  91. @get_user_tenant
  92. @plugin_data(payload_type=RequestInvokeSpeech2Text)
  93. def post(self, user_model: Account | EndUser, tenant_model: Tenant, payload: RequestInvokeSpeech2Text):
  94. try:
  95. return BaseBackwardsInvocationResponse(
  96. data=PluginModelBackwardsInvocation.invoke_speech2text(
  97. user_id=user_model.id,
  98. tenant=tenant_model,
  99. payload=payload,
  100. )
  101. ).model_dump()
  102. except Exception as e:
  103. return BaseBackwardsInvocationResponse(error=str(e)).model_dump()
  104. class PluginInvokeModerationApi(Resource):
  105. @setup_required
  106. @plugin_inner_api_only
  107. @get_user_tenant
  108. @plugin_data(payload_type=RequestInvokeModeration)
  109. def post(self, user_model: Account | EndUser, tenant_model: Tenant, payload: RequestInvokeModeration):
  110. try:
  111. return BaseBackwardsInvocationResponse(
  112. data=PluginModelBackwardsInvocation.invoke_moderation(
  113. user_id=user_model.id,
  114. tenant=tenant_model,
  115. payload=payload,
  116. )
  117. ).model_dump()
  118. except Exception as e:
  119. return BaseBackwardsInvocationResponse(error=str(e)).model_dump()
  120. class PluginInvokeToolApi(Resource):
  121. @setup_required
  122. @plugin_inner_api_only
  123. @get_user_tenant
  124. @plugin_data(payload_type=RequestInvokeTool)
  125. def post(self, user_model: Account | EndUser, tenant_model: Tenant, payload: RequestInvokeTool):
  126. def generator():
  127. return PluginToolBackwardsInvocation.convert_to_event_stream(
  128. PluginToolBackwardsInvocation.invoke_tool(
  129. tenant_id=tenant_model.id,
  130. user_id=user_model.id,
  131. tool_type=ToolProviderType.value_of(payload.tool_type),
  132. provider=payload.provider,
  133. tool_name=payload.tool,
  134. tool_parameters=payload.tool_parameters,
  135. ),
  136. )
  137. return compact_generate_response(generator())
  138. class PluginInvokeParameterExtractorNodeApi(Resource):
  139. @setup_required
  140. @plugin_inner_api_only
  141. @get_user_tenant
  142. @plugin_data(payload_type=RequestInvokeParameterExtractorNode)
  143. def post(self, user_model: Account | EndUser, tenant_model: Tenant, payload: RequestInvokeParameterExtractorNode):
  144. try:
  145. return BaseBackwardsInvocationResponse(
  146. data=PluginNodeBackwardsInvocation.invoke_parameter_extractor(
  147. tenant_id=tenant_model.id,
  148. user_id=user_model.id,
  149. parameters=payload.parameters,
  150. model_config=payload.model,
  151. instruction=payload.instruction,
  152. query=payload.query,
  153. )
  154. ).model_dump()
  155. except Exception as e:
  156. return BaseBackwardsInvocationResponse(error=str(e)).model_dump()
  157. class PluginInvokeQuestionClassifierNodeApi(Resource):
  158. @setup_required
  159. @plugin_inner_api_only
  160. @get_user_tenant
  161. @plugin_data(payload_type=RequestInvokeQuestionClassifierNode)
  162. def post(self, user_model: Account | EndUser, tenant_model: Tenant, payload: RequestInvokeQuestionClassifierNode):
  163. try:
  164. return BaseBackwardsInvocationResponse(
  165. data=PluginNodeBackwardsInvocation.invoke_question_classifier(
  166. tenant_id=tenant_model.id,
  167. user_id=user_model.id,
  168. query=payload.query,
  169. model_config=payload.model,
  170. classes=payload.classes,
  171. instruction=payload.instruction,
  172. )
  173. ).model_dump()
  174. except Exception as e:
  175. return BaseBackwardsInvocationResponse(error=str(e)).model_dump()
  176. class PluginInvokeAppApi(Resource):
  177. @setup_required
  178. @plugin_inner_api_only
  179. @get_user_tenant
  180. @plugin_data(payload_type=RequestInvokeApp)
  181. def post(self, user_model: Account | EndUser, tenant_model: Tenant, payload: RequestInvokeApp):
  182. response = PluginAppBackwardsInvocation.invoke_app(
  183. app_id=payload.app_id,
  184. user_id=user_model.id,
  185. tenant_id=tenant_model.id,
  186. conversation_id=payload.conversation_id,
  187. query=payload.query,
  188. stream=payload.response_mode == "streaming",
  189. inputs=payload.inputs,
  190. files=payload.files,
  191. )
  192. return compact_generate_response(PluginAppBackwardsInvocation.convert_to_event_stream(response))
  193. class PluginInvokeEncryptApi(Resource):
  194. @setup_required
  195. @plugin_inner_api_only
  196. @get_user_tenant
  197. @plugin_data(payload_type=RequestInvokeEncrypt)
  198. def post(self, user_model: Account | EndUser, tenant_model: Tenant, payload: RequestInvokeEncrypt):
  199. """
  200. encrypt or decrypt data
  201. """
  202. try:
  203. return BaseBackwardsInvocationResponse(
  204. data=PluginEncrypter.invoke_encrypt(tenant_model, payload)
  205. ).model_dump()
  206. except Exception as e:
  207. return BaseBackwardsInvocationResponse(error=str(e)).model_dump()
  208. class PluginInvokeSummaryApi(Resource):
  209. @setup_required
  210. @plugin_inner_api_only
  211. @get_user_tenant
  212. @plugin_data(payload_type=RequestInvokeSummary)
  213. def post(self, user_model: Account | EndUser, tenant_model: Tenant, payload: RequestInvokeSummary):
  214. try:
  215. return BaseBackwardsInvocationResponse(
  216. data=PluginModelBackwardsInvocation.invoke_summary(
  217. user_id=user_model.id,
  218. tenant=tenant_model,
  219. payload=payload,
  220. )
  221. ).model_dump()
  222. except Exception as e:
  223. return BaseBackwardsInvocationResponse(error=str(e)).model_dump()
  224. class PluginUploadFileRequestApi(Resource):
  225. @setup_required
  226. @plugin_inner_api_only
  227. @get_user_tenant
  228. @plugin_data(payload_type=RequestRequestUploadFile)
  229. def post(self, user_model: Account | EndUser, tenant_model: Tenant, payload: RequestRequestUploadFile):
  230. # generate signed url
  231. url = get_signed_file_url_for_plugin(payload.filename, payload.mimetype, user_model.id)
  232. return BaseBackwardsInvocationResponse(data={"url": url}).model_dump()
  233. api.add_resource(PluginInvokeLLMApi, "/invoke/llm")
  234. api.add_resource(PluginInvokeTextEmbeddingApi, "/invoke/text-embedding")
  235. api.add_resource(PluginInvokeRerankApi, "/invoke/rerank")
  236. api.add_resource(PluginInvokeTTSApi, "/invoke/tts")
  237. api.add_resource(PluginInvokeSpeech2TextApi, "/invoke/speech2text")
  238. api.add_resource(PluginInvokeModerationApi, "/invoke/moderation")
  239. api.add_resource(PluginInvokeToolApi, "/invoke/tool")
  240. api.add_resource(PluginInvokeParameterExtractorNodeApi, "/invoke/parameter-extractor")
  241. api.add_resource(PluginInvokeQuestionClassifierNodeApi, "/invoke/question-classifier")
  242. api.add_resource(PluginInvokeAppApi, "/invoke/app")
  243. api.add_resource(PluginInvokeEncryptApi, "/invoke/encrypt")
  244. api.add_resource(PluginInvokeSummaryApi, "/invoke/summary")
  245. api.add_resource(PluginUploadFileRequestApi, "/upload/file/request")