plugin.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import time
  2. from flask_restful import Resource
  3. from controllers.console.setup import setup_required
  4. from controllers.inner_api import api
  5. from controllers.inner_api.plugin.wraps import get_tenant, plugin_data
  6. from controllers.inner_api.wraps import plugin_inner_api_only
  7. from core.plugin.backwards_invocation.app import PluginAppBackwardsInvocation
  8. from core.plugin.backwards_invocation.model import PluginModelBackwardsInvocation
  9. from core.plugin.backwards_invocation.node import PluginNodeBackwardsInvocation
  10. from core.plugin.encrypt import PluginEncrypter
  11. from core.plugin.entities.request import (
  12. RequestInvokeApp,
  13. RequestInvokeEncrypt,
  14. RequestInvokeLLM,
  15. RequestInvokeModeration,
  16. RequestInvokeParameterExtractorNode,
  17. RequestInvokeQuestionClassifierNode,
  18. RequestInvokeRerank,
  19. RequestInvokeSpeech2Text,
  20. RequestInvokeTextEmbedding,
  21. RequestInvokeTool,
  22. RequestInvokeTTS,
  23. )
  24. from core.tools.entities.tool_entities import ToolInvokeMessage
  25. from libs.helper import compact_generate_response
  26. from models.account import Tenant
  27. class PluginInvokeLLMApi(Resource):
  28. @setup_required
  29. @plugin_inner_api_only
  30. @get_tenant
  31. @plugin_data(payload_type=RequestInvokeLLM)
  32. def post(self, user_id: str, tenant_model: Tenant, payload: RequestInvokeLLM):
  33. def generator():
  34. response = PluginModelBackwardsInvocation.invoke_llm(user_id, tenant_model, payload)
  35. return PluginModelBackwardsInvocation.convert_to_event_stream(response)
  36. return compact_generate_response(generator())
  37. class PluginInvokeTextEmbeddingApi(Resource):
  38. @setup_required
  39. @plugin_inner_api_only
  40. @get_tenant
  41. @plugin_data(payload_type=RequestInvokeTextEmbedding)
  42. def post(self, user_id: str, tenant_model: Tenant, payload: RequestInvokeTextEmbedding):
  43. pass
  44. class PluginInvokeRerankApi(Resource):
  45. @setup_required
  46. @plugin_inner_api_only
  47. @get_tenant
  48. @plugin_data(payload_type=RequestInvokeRerank)
  49. def post(self, user_id: str, tenant_model: Tenant, payload: RequestInvokeRerank):
  50. pass
  51. class PluginInvokeTTSApi(Resource):
  52. @setup_required
  53. @plugin_inner_api_only
  54. @get_tenant
  55. @plugin_data(payload_type=RequestInvokeTTS)
  56. def post(self, user_id: str, tenant_model: Tenant, payload: RequestInvokeTTS):
  57. pass
  58. class PluginInvokeSpeech2TextApi(Resource):
  59. @setup_required
  60. @plugin_inner_api_only
  61. @get_tenant
  62. @plugin_data(payload_type=RequestInvokeSpeech2Text)
  63. def post(self, user_id: str, tenant_model: Tenant, payload: RequestInvokeSpeech2Text):
  64. pass
  65. class PluginInvokeModerationApi(Resource):
  66. @setup_required
  67. @plugin_inner_api_only
  68. @get_tenant
  69. @plugin_data(payload_type=RequestInvokeModeration)
  70. def post(self, user_id: str, tenant_model: Tenant, payload: RequestInvokeModeration):
  71. pass
  72. class PluginInvokeToolApi(Resource):
  73. @setup_required
  74. @plugin_inner_api_only
  75. @get_tenant
  76. @plugin_data(payload_type=RequestInvokeTool)
  77. def post(self, user_id: str, tenant_model: Tenant, payload: RequestInvokeTool):
  78. def generator():
  79. for i in range(10):
  80. time.sleep(0.1)
  81. yield (
  82. ToolInvokeMessage(
  83. type=ToolInvokeMessage.MessageType.TEXT,
  84. message=ToolInvokeMessage.TextMessage(text="helloworld"),
  85. )
  86. .model_dump_json()
  87. .encode()
  88. + b"\n\n"
  89. )
  90. return compact_generate_response(generator())
  91. class PluginInvokeParameterExtractorNodeApi(Resource):
  92. @setup_required
  93. @plugin_inner_api_only
  94. @get_tenant
  95. @plugin_data(payload_type=RequestInvokeParameterExtractorNode)
  96. def post(self, user_id: str, tenant_model: Tenant, payload: RequestInvokeParameterExtractorNode):
  97. return PluginNodeBackwardsInvocation.invoke_parameter_extractor(
  98. tenant_id=tenant_model.id,
  99. user_id=user_id,
  100. parameters=payload.parameters,
  101. model_config=payload.model,
  102. instruction=payload.instruction,
  103. query=payload.query,
  104. )
  105. class PluginInvokeQuestionClassifierNodeApi(Resource):
  106. @setup_required
  107. @plugin_inner_api_only
  108. @get_tenant
  109. @plugin_data(payload_type=RequestInvokeQuestionClassifierNode)
  110. def post(self, user_id: str, tenant_model: Tenant, payload: RequestInvokeQuestionClassifierNode):
  111. return PluginNodeBackwardsInvocation.invoke_question_classifier(
  112. tenant_id=tenant_model.id,
  113. user_id=user_id,
  114. query=payload.query,
  115. model_config=payload.model,
  116. classes=payload.classes,
  117. instruction=payload.instruction,
  118. )
  119. class PluginInvokeAppApi(Resource):
  120. @setup_required
  121. @plugin_inner_api_only
  122. @get_tenant
  123. @plugin_data(payload_type=RequestInvokeApp)
  124. def post(self, user_id: str, tenant_model: Tenant, payload: RequestInvokeApp):
  125. response = PluginAppBackwardsInvocation.invoke_app(
  126. app_id=payload.app_id,
  127. user_id=user_id,
  128. tenant_id=tenant_model.id,
  129. conversation_id=payload.conversation_id,
  130. query=payload.query,
  131. stream=payload.response_mode == "streaming",
  132. inputs=payload.inputs,
  133. files=payload.files,
  134. )
  135. return compact_generate_response(PluginAppBackwardsInvocation.convert_to_event_stream(response))
  136. class PluginInvokeEncryptApi(Resource):
  137. @setup_required
  138. @plugin_inner_api_only
  139. @get_tenant
  140. @plugin_data(payload_type=RequestInvokeEncrypt)
  141. def post(self, user_id: str, tenant_model: Tenant, payload: RequestInvokeEncrypt):
  142. """
  143. encrypt or decrypt data
  144. """
  145. return PluginEncrypter.invoke_encrypt(tenant_model, payload)
  146. api.add_resource(PluginInvokeLLMApi, "/invoke/llm")
  147. api.add_resource(PluginInvokeTextEmbeddingApi, "/invoke/text-embedding")
  148. api.add_resource(PluginInvokeRerankApi, "/invoke/rerank")
  149. api.add_resource(PluginInvokeTTSApi, "/invoke/tts")
  150. api.add_resource(PluginInvokeSpeech2TextApi, "/invoke/speech2text")
  151. api.add_resource(PluginInvokeModerationApi, "/invoke/moderation")
  152. api.add_resource(PluginInvokeToolApi, "/invoke/tool")
  153. api.add_resource(PluginInvokeParameterExtractorNodeApi, "/invoke/parameter-extractor")
  154. api.add_resource(PluginInvokeQuestionClassifierNodeApi, "/invoke/question-classifier")
  155. api.add_resource(PluginInvokeAppApi, "/invoke/app")
  156. api.add_resource(PluginInvokeEncryptApi, "/invoke/encrypt")