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

186 lines
6.4 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-09-24 18:03:48 +08:00
from core.plugin.backwards_invocation.node import PluginNodeBackwardsInvocation
2024-08-30 21:25:58 +08:00
from core.plugin.encrypt import PluginEncrypter
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-09-24 18:03:48 +08:00
RequestInvokeParameterExtractorNode,
RequestInvokeQuestionClassifierNode,
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-09-14 02:47:01 +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,
2024-09-24 18:03:48 +08:00
message=ToolInvokeMessage.TextMessage(text="helloworld"),
2024-07-29 18:57:34 +08:00
)
.model_dump_json()
.encode()
2024-09-24 18:03:48 +08:00
+ b"\n\n"
2024-07-29 18:57:34 +08:00
)
return compact_generate_response(generator())
2024-07-08 22:37:20 +08:00
2024-09-24 18:03:48 +08:00
class PluginInvokeParameterExtractorNodeApi(Resource):
2024-07-08 22:37:20 +08:00
@setup_required
@plugin_inner_api_only
@get_tenant
2024-09-24 18:03:48 +08:00
@plugin_data(payload_type=RequestInvokeParameterExtractorNode)
def post(self, user_id: str, tenant_model: Tenant, payload: RequestInvokeParameterExtractorNode):
return PluginNodeBackwardsInvocation.invoke_parameter_extractor(
tenant_id=tenant_model.id,
user_id=user_id,
parameters=payload.parameters,
model_config=payload.model,
instruction=payload.instruction,
query=payload.query,
)
class PluginInvokeQuestionClassifierNodeApi(Resource):
@setup_required
@plugin_inner_api_only
@get_tenant
@plugin_data(payload_type=RequestInvokeQuestionClassifierNode)
def post(self, user_id: str, tenant_model: Tenant, payload: RequestInvokeQuestionClassifierNode):
return PluginNodeBackwardsInvocation.invoke_question_classifier(
tenant_id=tenant_model.id,
user_id=user_id,
query=payload.query,
model_config=payload.model,
classes=payload.classes,
instruction=payload.instruction,
)
2024-07-08 22:37:20 +08:00
2024-09-14 02:47:01 +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-09-24 18:03:48 +08:00
stream=payload.response_mode == "streaming",
2024-08-29 20:50:36 +08:00
inputs=payload.inputs,
2024-09-24 18:03:48 +08:00
files=payload.files,
2024-08-29 20:50:36 +08:00
)
2024-07-08 22:37:20 +08:00
2024-09-24 18:03:48 +08:00
return compact_generate_response(PluginAppBackwardsInvocation.convert_to_event_stream(response))
2024-09-14 02:47:01 +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-08-30 21:25:58 +08:00
"""
encrypt or decrypt data
"""
return PluginEncrypter.invoke_encrypt(tenant_model, payload)
2024-08-30 14:23:14 +08:00
2024-09-14 02:47:01 +08:00
2024-09-24 18:03:48 +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")
api.add_resource(PluginInvokeToolApi, "/invoke/tool")
api.add_resource(PluginInvokeParameterExtractorNodeApi, "/invoke/parameter-extractor")
api.add_resource(PluginInvokeQuestionClassifierNodeApi, "/invoke/question-classifier")
api.add_resource(PluginInvokeAppApi, "/invoke/app")
api.add_resource(PluginInvokeEncryptApi, "/invoke/encrypt")