plugin.py 5.1 KB

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