plugin.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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.encrypt import PluginEncrypter
  10. from core.plugin.entities.request import (
  11. RequestInvokeApp,
  12. RequestInvokeEncrypt,
  13. RequestInvokeLLM,
  14. RequestInvokeModeration,
  15. RequestInvokeNode,
  16. RequestInvokeRerank,
  17. RequestInvokeSpeech2Text,
  18. RequestInvokeTextEmbedding,
  19. RequestInvokeTool,
  20. RequestInvokeTTS,
  21. )
  22. from core.tools.entities.tool_entities import ToolInvokeMessage
  23. from libs.helper import compact_generate_response
  24. from models.account import Tenant
  25. class PluginInvokeLLMApi(Resource):
  26. @setup_required
  27. @plugin_inner_api_only
  28. @get_tenant
  29. @plugin_data(payload_type=RequestInvokeLLM)
  30. def post(self, user_id: str, tenant_model: Tenant, payload: RequestInvokeLLM):
  31. def generator():
  32. response = PluginModelBackwardsInvocation.invoke_llm(user_id, tenant_model, payload)
  33. return PluginModelBackwardsInvocation.convert_to_event_stream(response)
  34. return compact_generate_response(generator())
  35. class PluginInvokeTextEmbeddingApi(Resource):
  36. @setup_required
  37. @plugin_inner_api_only
  38. @get_tenant
  39. @plugin_data(payload_type=RequestInvokeTextEmbedding)
  40. def post(self, user_id: str, tenant_model: Tenant, payload: RequestInvokeTextEmbedding):
  41. pass
  42. class PluginInvokeRerankApi(Resource):
  43. @setup_required
  44. @plugin_inner_api_only
  45. @get_tenant
  46. @plugin_data(payload_type=RequestInvokeRerank)
  47. def post(self, user_id: str, tenant_model: Tenant, payload: RequestInvokeRerank):
  48. pass
  49. class PluginInvokeTTSApi(Resource):
  50. @setup_required
  51. @plugin_inner_api_only
  52. @get_tenant
  53. @plugin_data(payload_type=RequestInvokeTTS)
  54. def post(self, user_id: str, tenant_model: Tenant, payload: RequestInvokeTTS):
  55. pass
  56. class PluginInvokeSpeech2TextApi(Resource):
  57. @setup_required
  58. @plugin_inner_api_only
  59. @get_tenant
  60. @plugin_data(payload_type=RequestInvokeSpeech2Text)
  61. def post(self, user_id: str, tenant_model: Tenant, payload: RequestInvokeSpeech2Text):
  62. pass
  63. class PluginInvokeModerationApi(Resource):
  64. @setup_required
  65. @plugin_inner_api_only
  66. @get_tenant
  67. @plugin_data(payload_type=RequestInvokeModeration)
  68. def post(self, user_id: str, tenant_model: Tenant, payload: RequestInvokeModeration):
  69. pass
  70. class PluginInvokeToolApi(Resource):
  71. @setup_required
  72. @plugin_inner_api_only
  73. @get_tenant
  74. @plugin_data(payload_type=RequestInvokeTool)
  75. def post(self, user_id: str, tenant_model: Tenant, payload: RequestInvokeTool):
  76. def generator():
  77. for i in range(10):
  78. time.sleep(0.1)
  79. yield (
  80. ToolInvokeMessage(
  81. type=ToolInvokeMessage.MessageType.TEXT,
  82. message=ToolInvokeMessage.TextMessage(text='helloworld'),
  83. )
  84. .model_dump_json()
  85. .encode()
  86. + b'\n\n'
  87. )
  88. return compact_generate_response(generator())
  89. class PluginInvokeNodeApi(Resource):
  90. @setup_required
  91. @plugin_inner_api_only
  92. @get_tenant
  93. @plugin_data(payload_type=RequestInvokeNode)
  94. def post(self, user_id: str, tenant_model: Tenant, payload: RequestInvokeNode):
  95. pass
  96. class PluginInvokeAppApi(Resource):
  97. @setup_required
  98. @plugin_inner_api_only
  99. @get_tenant
  100. @plugin_data(payload_type=RequestInvokeApp)
  101. def post(self, user_id: str, tenant_model: Tenant, payload: RequestInvokeApp):
  102. response = PluginAppBackwardsInvocation.invoke_app(
  103. app_id=payload.app_id,
  104. user_id=user_id,
  105. tenant_id=tenant_model.id,
  106. conversation_id=payload.conversation_id,
  107. query=payload.query,
  108. stream=payload.response_mode == 'streaming',
  109. inputs=payload.inputs,
  110. files=payload.files
  111. )
  112. return compact_generate_response(
  113. PluginAppBackwardsInvocation.convert_to_event_stream(response)
  114. )
  115. class PluginInvokeEncryptApi(Resource):
  116. @setup_required
  117. @plugin_inner_api_only
  118. @get_tenant
  119. @plugin_data(payload_type=RequestInvokeEncrypt)
  120. def post(self, user_id: str, tenant_model: Tenant, payload: RequestInvokeEncrypt):
  121. """
  122. encrypt or decrypt data
  123. """
  124. return PluginEncrypter.invoke_encrypt(tenant_model, payload)
  125. api.add_resource(PluginInvokeLLMApi, '/invoke/llm')
  126. api.add_resource(PluginInvokeTextEmbeddingApi, '/invoke/text-embedding')
  127. api.add_resource(PluginInvokeRerankApi, '/invoke/rerank')
  128. api.add_resource(PluginInvokeTTSApi, '/invoke/tts')
  129. api.add_resource(PluginInvokeSpeech2TextApi, '/invoke/speech2text')
  130. api.add_resource(PluginInvokeModerationApi, '/invoke/moderation')
  131. api.add_resource(PluginInvokeToolApi, '/invoke/tool')
  132. api.add_resource(PluginInvokeNodeApi, '/invoke/node')
  133. api.add_resource(PluginInvokeAppApi, '/invoke/app')
  134. api.add_resource(PluginInvokeEncryptApi, '/invoke/encrypt')