dify/api/controllers/inner_api/plugin/plugin.py

153 lines
5.1 KiB
Python
Raw Normal View History

2024-07-29 18:57:34 +08:00
import time
2024-07-29 22:08:14 +08:00
2024-08-29 21:14:23 +08:00
from flask_restful import Resource
2024-07-08 22:37:20 +08:00
from controllers.console.setup import setup_required
from controllers.inner_api import api
2024-07-29 16:40:04 +08:00
from controllers.inner_api.plugin.wraps import get_tenant, plugin_data
2024-07-08 22:37:20 +08:00
from controllers.inner_api.wraps import plugin_inner_api_only
2024-08-29 20:50:36 +08:00
from core.plugin.backwards_invocation.app import PluginAppBackwardsInvocation
2024-08-29 20:06:14 +08:00
from core.plugin.backwards_invocation.model import PluginModelBackwardsInvocation
2024-07-29 18:57:34 +08:00
from core.plugin.entities.request import (
2024-08-29 20:50:36 +08:00
RequestInvokeApp,
2024-08-30 14:23:14 +08:00
RequestInvokeEncrypt,
2024-07-29 18:57:34 +08:00
RequestInvokeLLM,
RequestInvokeModeration,
2024-08-29 20:50:36 +08:00
RequestInvokeNode,
2024-07-29 18:57:34 +08:00
RequestInvokeRerank,
RequestInvokeSpeech2Text,
RequestInvokeTextEmbedding,
RequestInvokeTool,
RequestInvokeTTS,
)
from core.tools.entities.tool_entities import ToolInvokeMessage
2024-07-08 22:37:20 +08:00
from libs.helper import compact_generate_response
from models.account import Tenant
2024-07-29 16:40:04 +08:00
class PluginInvokeLLMApi(Resource):
2024-07-08 22:37:20 +08:00
@setup_required
@plugin_inner_api_only
@get_tenant
2024-07-29 16:40:04 +08:00
@plugin_data(payload_type=RequestInvokeLLM)
def post(self, user_id: str, tenant_model: Tenant, payload: RequestInvokeLLM):
2024-07-29 22:08:14 +08:00
def generator():
2024-08-29 20:06:14 +08:00
response = PluginModelBackwardsInvocation.invoke_llm(user_id, tenant_model, payload)
2024-08-29 20:17:17 +08:00
return PluginModelBackwardsInvocation.convert_to_event_stream(response)
2024-07-29 22:08:14 +08:00
return compact_generate_response(generator())
2024-07-08 22:37:20 +08:00
2024-07-29 16:40:04 +08:00
class PluginInvokeTextEmbeddingApi(Resource):
@setup_required
@plugin_inner_api_only
@get_tenant
@plugin_data(payload_type=RequestInvokeTextEmbedding)
def post(self, user_id: str, tenant_model: Tenant, payload: RequestInvokeTextEmbedding):
pass
2024-07-29 18:57:34 +08:00
2024-07-29 16:40:04 +08:00
class PluginInvokeRerankApi(Resource):
@setup_required
@plugin_inner_api_only
@get_tenant
@plugin_data(payload_type=RequestInvokeRerank)
def post(self, user_id: str, tenant_model: Tenant, payload: RequestInvokeRerank):
pass
2024-07-29 18:57:34 +08:00
2024-07-29 16:40:04 +08:00
class PluginInvokeTTSApi(Resource):
@setup_required
@plugin_inner_api_only
@get_tenant
@plugin_data(payload_type=RequestInvokeTTS)
def post(self, user_id: str, tenant_model: Tenant, payload: RequestInvokeTTS):
pass
2024-07-29 18:57:34 +08:00
2024-07-29 16:40:04 +08:00
class PluginInvokeSpeech2TextApi(Resource):
@setup_required
@plugin_inner_api_only
@get_tenant
@plugin_data(payload_type=RequestInvokeSpeech2Text)
def post(self, user_id: str, tenant_model: Tenant, payload: RequestInvokeSpeech2Text):
pass
2024-07-09 15:37:56 +08:00
2024-07-29 18:57:34 +08:00
2024-07-29 16:40:04 +08:00
class PluginInvokeModerationApi(Resource):
@setup_required
@plugin_inner_api_only
@get_tenant
@plugin_data(payload_type=RequestInvokeModeration)
def post(self, user_id: str, tenant_model: Tenant, payload: RequestInvokeModeration):
pass
2024-07-08 22:37:20 +08:00
2024-07-29 18:57:34 +08:00
2024-07-08 22:37:20 +08:00
class PluginInvokeToolApi(Resource):
@setup_required
@plugin_inner_api_only
@get_tenant
2024-07-29 16:40:04 +08:00
@plugin_data(payload_type=RequestInvokeTool)
2024-07-29 18:57:34 +08:00
def post(self, user_id: str, tenant_model: Tenant, payload: RequestInvokeTool):
def generator():
for i in range(10):
time.sleep(0.1)
yield (
ToolInvokeMessage(
type=ToolInvokeMessage.MessageType.TEXT,
message=ToolInvokeMessage.TextMessage(text='helloworld'),
)
.model_dump_json()
.encode()
+ b'\n\n'
)
return compact_generate_response(generator())
2024-07-08 22:37:20 +08:00
class PluginInvokeNodeApi(Resource):
@setup_required
@plugin_inner_api_only
@get_tenant
2024-08-29 20:50:36 +08:00
@plugin_data(payload_type=RequestInvokeNode)
def post(self, user_id: str, tenant_model: Tenant, payload: RequestInvokeNode):
pass
2024-07-08 22:37:20 +08:00
2024-08-29 12:55:00 +08:00
class PluginInvokeAppApi(Resource):
@setup_required
@plugin_inner_api_only
@get_tenant
2024-08-29 20:50:36 +08:00
@plugin_data(payload_type=RequestInvokeApp)
def post(self, user_id: str, tenant_model: Tenant, payload: RequestInvokeApp):
response = PluginAppBackwardsInvocation.invoke_app(
app_id=payload.app_id,
user_id=user_id,
tenant_id=tenant_model.id,
conversation_id=payload.conversation_id,
query=payload.query,
2024-08-29 21:14:23 +08:00
stream=payload.response_mode == 'streaming',
2024-08-29 20:50:36 +08:00
inputs=payload.inputs,
files=payload.files
)
return compact_generate_response(
PluginAppBackwardsInvocation.convert_to_event_stream(response)
)
2024-07-08 22:37:20 +08:00
2024-08-30 14:23:14 +08:00
class PluginInvokeEncryptApi(Resource):
@setup_required
@plugin_inner_api_only
@get_tenant
@plugin_data(payload_type=RequestInvokeEncrypt)
def post(self, user_id: str, tenant_model: Tenant, payload: RequestInvokeEncrypt):
""""""
2024-07-29 16:40:04 +08:00
api.add_resource(PluginInvokeLLMApi, '/invoke/llm')
api.add_resource(PluginInvokeTextEmbeddingApi, '/invoke/text-embedding')
api.add_resource(PluginInvokeRerankApi, '/invoke/rerank')
api.add_resource(PluginInvokeTTSApi, '/invoke/tts')
api.add_resource(PluginInvokeSpeech2TextApi, '/invoke/speech2text')
api.add_resource(PluginInvokeModerationApi, '/invoke/moderation')
2024-07-08 22:37:20 +08:00
api.add_resource(PluginInvokeToolApi, '/invoke/tool')
api.add_resource(PluginInvokeNodeApi, '/invoke/node')
2024-08-29 12:55:00 +08:00
api.add_resource(PluginInvokeAppApi, '/invoke/app')