mirror of https://github.com/langgenius/dify.git
feat: support plugin inner api
This commit is contained in:
parent
ed7fcc5f7d
commit
f29b44acd8
|
|
@ -1,34 +1,68 @@
|
|||
|
||||
from flask_restful import Resource, reqparse
|
||||
|
||||
from controllers.console.setup import setup_required
|
||||
from controllers.inner_api import api
|
||||
from controllers.inner_api.plugin.wraps import get_tenant
|
||||
from controllers.inner_api.plugin.wraps import get_tenant, plugin_data
|
||||
from controllers.inner_api.wraps import plugin_inner_api_only
|
||||
from core.plugin.entities.request import RequestInvokeLLM, RequestInvokeModeration, RequestInvokeRerank, RequestInvokeSpeech2Text, RequestInvokeTTS, RequestInvokeTextEmbedding, RequestInvokeTool
|
||||
from libs.helper import compact_generate_response
|
||||
from models.account import Tenant
|
||||
from services.plugin.plugin_invoke_service import PluginInvokeService
|
||||
|
||||
|
||||
class PluginInvokeModelApi(Resource):
|
||||
class PluginInvokeLLMApi(Resource):
|
||||
@setup_required
|
||||
@plugin_inner_api_only
|
||||
@get_tenant
|
||||
def post(self, user_id: str, tenant_model: Tenant):
|
||||
parser = reqparse.RequestParser()
|
||||
parser.add_argument('provider', type=dict, required=True, location='json')
|
||||
parser.add_argument('model', type=dict, required=True, location='json')
|
||||
parser.add_argument('parameters', type=dict, required=True, location='json')
|
||||
@plugin_data(payload_type=RequestInvokeLLM)
|
||||
def post(self, user_id: str, tenant_model: Tenant, payload: RequestInvokeLLM):
|
||||
pass
|
||||
|
||||
args = parser.parse_args()
|
||||
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
|
||||
|
||||
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
class PluginInvokeToolApi(Resource):
|
||||
@setup_required
|
||||
@plugin_inner_api_only
|
||||
@get_tenant
|
||||
@plugin_data(payload_type=RequestInvokeTool)
|
||||
def post(self, user_id: str, tenant_model: Tenant):
|
||||
parser = reqparse.RequestParser()
|
||||
parser.add_argument('provider', type=dict, required=True, location='json')
|
||||
|
|
@ -37,9 +71,9 @@ class PluginInvokeToolApi(Resource):
|
|||
|
||||
args = parser.parse_args()
|
||||
|
||||
response = PluginInvokeService.invoke_tool(user_id, tenant_model,
|
||||
args['provider'], args['tool'],
|
||||
args['parameters'])
|
||||
response = PluginInvokeService.invoke_tool(
|
||||
user_id, tenant_model, args['provider'], args['tool'], args['parameters']
|
||||
)
|
||||
return compact_generate_response(response)
|
||||
|
||||
|
||||
|
|
@ -51,11 +85,14 @@ class PluginInvokeNodeApi(Resource):
|
|||
parser = reqparse.RequestParser()
|
||||
args = parser.parse_args()
|
||||
|
||||
return {
|
||||
'message': 'success'
|
||||
}
|
||||
return {'message': 'success'}
|
||||
|
||||
|
||||
api.add_resource(PluginInvokeModelApi, '/invoke/model')
|
||||
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(PluginInvokeNodeApi, '/invoke/node')
|
||||
|
|
|
|||
|
|
@ -2,7 +2,9 @@ from collections.abc import Callable
|
|||
from functools import wraps
|
||||
from typing import Optional
|
||||
|
||||
from flask import request
|
||||
from flask_restful import reqparse
|
||||
from pydantic import BaseModel
|
||||
|
||||
from extensions.ext_database import db
|
||||
from models.account import Tenant
|
||||
|
|
@ -45,3 +47,26 @@ def get_tenant(view: Optional[Callable] = None):
|
|||
return decorator
|
||||
else:
|
||||
return decorator(view)
|
||||
|
||||
def plugin_data(view: Optional[Callable] = None, *, payload_type: type[BaseModel]):
|
||||
def decorator(view_func):
|
||||
def decorated_view(*args, **kwargs):
|
||||
try:
|
||||
data = request.get_json()
|
||||
except Exception:
|
||||
raise ValueError('invalid json')
|
||||
|
||||
try:
|
||||
payload = payload_type(**data)
|
||||
except Exception as e:
|
||||
raise ValueError(f'invalid payload: {str(e)}')
|
||||
|
||||
kwargs['payload'] = payload
|
||||
return view_func(*args, **kwargs)
|
||||
|
||||
return decorated_view
|
||||
|
||||
if view is None:
|
||||
return decorator
|
||||
else:
|
||||
return decorator(view)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,42 @@
|
|||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class RequestInvokeTool(BaseModel):
|
||||
"""
|
||||
Request to invoke a tool
|
||||
"""
|
||||
|
||||
class RequestInvokeLLM(BaseModel):
|
||||
"""
|
||||
Request to invoke LLM
|
||||
"""
|
||||
|
||||
class RequestInvokeTextEmbedding(BaseModel):
|
||||
"""
|
||||
Request to invoke text embedding
|
||||
"""
|
||||
|
||||
class RequestInvokeRerank(BaseModel):
|
||||
"""
|
||||
Request to invoke rerank
|
||||
"""
|
||||
|
||||
class RequestInvokeTTS(BaseModel):
|
||||
"""
|
||||
Request to invoke TTS
|
||||
"""
|
||||
|
||||
class RequestInvokeSpeech2Text(BaseModel):
|
||||
"""
|
||||
Request to invoke speech2text
|
||||
"""
|
||||
|
||||
class RequestInvokeModeration(BaseModel):
|
||||
"""
|
||||
Request to invoke moderation
|
||||
"""
|
||||
|
||||
class RequestInvokeNode(BaseModel):
|
||||
"""
|
||||
Request to invoke node
|
||||
"""
|
||||
|
|
@ -103,8 +103,8 @@ class ToolInvokeMessage(BaseModel):
|
|||
"""
|
||||
plain text, image url or link url
|
||||
"""
|
||||
message: Union[str, bytes, dict] = None
|
||||
meta: dict[str, Any] = None
|
||||
message: Optional[Union[str, bytes, dict]] = None
|
||||
meta: Optional[dict[str, Any]] = None
|
||||
save_as: str = ''
|
||||
|
||||
class ToolInvokeMessageBinary(BaseModel):
|
||||
|
|
@ -168,16 +168,19 @@ class ToolParameter(BaseModel):
|
|||
"""
|
||||
# convert options to ToolParameterOption
|
||||
if options:
|
||||
options = [ToolParameterOption(value=option, label=I18nObject(en_US=option, zh_Hans=option)) for option in options]
|
||||
option_objs = [ToolParameterOption(value=option, label=I18nObject(en_US=option, zh_Hans=option)) for option in options]
|
||||
else:
|
||||
option_objs = None
|
||||
return cls(
|
||||
name=name,
|
||||
label=I18nObject(en_US='', zh_Hans=''),
|
||||
placeholder=None,
|
||||
human_description=I18nObject(en_US='', zh_Hans=''),
|
||||
type=type,
|
||||
form=cls.ToolParameterForm.LLM,
|
||||
llm_description=llm_description,
|
||||
required=required,
|
||||
options=options,
|
||||
options=option_objs,
|
||||
)
|
||||
|
||||
class ToolProviderIdentity(BaseModel):
|
||||
|
|
@ -245,7 +248,7 @@ class ToolProviderCredentials(BaseModel):
|
|||
'default': self.default,
|
||||
'options': self.options,
|
||||
'help': self.help.to_dict() if self.help else None,
|
||||
'label': self.label.to_dict(),
|
||||
'label': self.label.to_dict() if self.label else None,
|
||||
'url': self.url,
|
||||
'placeholder': self.placeholder.to_dict() if self.placeholder else None,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -40,8 +40,9 @@ class Tool(BaseModel, ABC):
|
|||
|
||||
class Runtime(BaseModel):
|
||||
"""
|
||||
Meta data of a tool call processing
|
||||
Meta data of a tool call processing
|
||||
"""
|
||||
|
||||
def __init__(self, **data: Any):
|
||||
super().__init__(**data)
|
||||
if not self.runtime_parameters:
|
||||
|
|
@ -65,10 +66,10 @@ class Tool(BaseModel, ABC):
|
|||
|
||||
def fork_tool_runtime(self, runtime: dict[str, Any]) -> 'Tool':
|
||||
"""
|
||||
fork a new tool with meta data
|
||||
fork a new tool with meta data
|
||||
|
||||
:param meta: the meta data of a tool call processing, tenant_id is required
|
||||
:return: the new tool
|
||||
:param meta: the meta data of a tool call processing, tenant_id is required
|
||||
:return: the new tool
|
||||
"""
|
||||
return self.__class__(
|
||||
identity=self.identity.model_copy() if self.identity else None,
|
||||
|
|
@ -76,82 +77,82 @@ class Tool(BaseModel, ABC):
|
|||
description=self.description.model_copy() if self.description else None,
|
||||
runtime=Tool.Runtime(**runtime),
|
||||
)
|
||||
|
||||
|
||||
@abstractmethod
|
||||
def tool_provider_type(self) -> ToolProviderType:
|
||||
"""
|
||||
get the tool provider type
|
||||
get the tool provider type
|
||||
|
||||
:return: the tool provider type
|
||||
:return: the tool provider type
|
||||
"""
|
||||
|
||||
|
||||
def load_variables(self, variables: ToolRuntimeVariablePool):
|
||||
"""
|
||||
load variables from database
|
||||
load variables from database
|
||||
|
||||
:param conversation_id: the conversation id
|
||||
:param conversation_id: the conversation id
|
||||
"""
|
||||
self.variables = variables
|
||||
|
||||
def set_image_variable(self, variable_name: str, image_key: str) -> None:
|
||||
"""
|
||||
set an image variable
|
||||
set an image variable
|
||||
"""
|
||||
if not self.variables:
|
||||
return
|
||||
|
||||
|
||||
self.variables.set_file(self.identity.name, variable_name, image_key)
|
||||
|
||||
def set_text_variable(self, variable_name: str, text: str) -> None:
|
||||
"""
|
||||
set a text variable
|
||||
set a text variable
|
||||
"""
|
||||
if not self.variables:
|
||||
return
|
||||
|
||||
|
||||
self.variables.set_text(self.identity.name, variable_name, text)
|
||||
|
||||
|
||||
def get_variable(self, name: Union[str, Enum]) -> Optional[ToolRuntimeVariable]:
|
||||
"""
|
||||
get a variable
|
||||
get a variable
|
||||
|
||||
:param name: the name of the variable
|
||||
:return: the variable
|
||||
:param name: the name of the variable
|
||||
:return: the variable
|
||||
"""
|
||||
if not self.variables:
|
||||
return None
|
||||
|
||||
|
||||
if isinstance(name, Enum):
|
||||
name = name.value
|
||||
|
||||
|
||||
for variable in self.variables.pool:
|
||||
if variable.name == name:
|
||||
return variable
|
||||
|
||||
|
||||
return None
|
||||
|
||||
def get_default_image_variable(self) -> Optional[ToolRuntimeVariable]:
|
||||
"""
|
||||
get the default image variable
|
||||
get the default image variable
|
||||
|
||||
:return: the image variable
|
||||
:return: the image variable
|
||||
"""
|
||||
if not self.variables:
|
||||
return None
|
||||
|
||||
|
||||
return self.get_variable(self.VARIABLE_KEY.IMAGE)
|
||||
|
||||
|
||||
def get_variable_file(self, name: Union[str, Enum]) -> Optional[bytes]:
|
||||
"""
|
||||
get a variable file
|
||||
get a variable file
|
||||
|
||||
:param name: the name of the variable
|
||||
:return: the variable file
|
||||
:param name: the name of the variable
|
||||
:return: the variable file
|
||||
"""
|
||||
variable = self.get_variable(name)
|
||||
if not variable:
|
||||
return None
|
||||
|
||||
|
||||
if not isinstance(variable, ToolRuntimeImageVariable):
|
||||
return None
|
||||
|
||||
|
|
@ -160,31 +161,31 @@ class Tool(BaseModel, ABC):
|
|||
file_binary = ToolFileManager.get_file_binary_by_message_file_id(message_file_id)
|
||||
if not file_binary:
|
||||
return None
|
||||
|
||||
|
||||
return file_binary[0]
|
||||
|
||||
|
||||
def list_variables(self) -> list[ToolRuntimeVariable]:
|
||||
"""
|
||||
list all variables
|
||||
list all variables
|
||||
|
||||
:return: the variables
|
||||
:return: the variables
|
||||
"""
|
||||
if not self.variables:
|
||||
return []
|
||||
|
||||
|
||||
return self.variables.pool
|
||||
|
||||
|
||||
def list_default_image_variables(self) -> list[ToolRuntimeVariable]:
|
||||
"""
|
||||
list all image variables
|
||||
list all image variables
|
||||
|
||||
:return: the image variables
|
||||
:return: the image variables
|
||||
"""
|
||||
if not self.variables:
|
||||
return []
|
||||
|
||||
|
||||
result = []
|
||||
|
||||
|
||||
for variable in self.variables.pool:
|
||||
if variable.name.startswith(self.VARIABLE_KEY.IMAGE.value):
|
||||
result.append(variable)
|
||||
|
|
@ -215,38 +216,40 @@ class Tool(BaseModel, ABC):
|
|||
result = deepcopy(tool_parameters)
|
||||
for parameter in self.parameters or []:
|
||||
if parameter.name in tool_parameters:
|
||||
result[parameter.name] = ToolParameterConverter.cast_parameter_by_type(tool_parameters[parameter.name], parameter.type)
|
||||
result[parameter.name] = ToolParameterConverter.cast_parameter_by_type(
|
||||
tool_parameters[parameter.name], parameter.type
|
||||
)
|
||||
|
||||
return result
|
||||
|
||||
@abstractmethod
|
||||
def _invoke(self, user_id: str, tool_parameters: dict[str, Any]) -> Generator[ToolInvokeMessage, None, None]:
|
||||
pass
|
||||
|
||||
|
||||
def validate_credentials(self, credentials: dict[str, Any], parameters: dict[str, Any]) -> None:
|
||||
"""
|
||||
validate the credentials
|
||||
validate the credentials
|
||||
|
||||
:param credentials: the credentials
|
||||
:param parameters: the parameters
|
||||
:param credentials: the credentials
|
||||
:param parameters: the parameters
|
||||
"""
|
||||
pass
|
||||
|
||||
def get_runtime_parameters(self) -> list[ToolParameter]:
|
||||
"""
|
||||
get the runtime parameters
|
||||
get the runtime parameters
|
||||
|
||||
interface for developer to dynamic change the parameters of a tool depends on the variables pool
|
||||
interface for developer to dynamic change the parameters of a tool depends on the variables pool
|
||||
|
||||
:return: the runtime parameters
|
||||
:return: the runtime parameters
|
||||
"""
|
||||
return self.parameters or []
|
||||
|
||||
|
||||
def get_all_runtime_parameters(self) -> list[ToolParameter]:
|
||||
"""
|
||||
get all runtime parameters
|
||||
get all runtime parameters
|
||||
|
||||
:return: all runtime parameters
|
||||
:return: all runtime parameters
|
||||
"""
|
||||
parameters = self.parameters or []
|
||||
parameters = parameters.copy()
|
||||
|
|
@ -275,68 +278,50 @@ class Tool(BaseModel, ABC):
|
|||
parameters.append(parameter)
|
||||
|
||||
return parameters
|
||||
|
||||
|
||||
def create_image_message(self, image: str, save_as: str = '') -> ToolInvokeMessage:
|
||||
"""
|
||||
create an image message
|
||||
create an image message
|
||||
|
||||
:param image: the url of the image
|
||||
:return: the image message
|
||||
:param image: the url of the image
|
||||
:return: the image message
|
||||
"""
|
||||
return ToolInvokeMessage(type=ToolInvokeMessage.MessageType.IMAGE,
|
||||
message=image,
|
||||
save_as=save_as)
|
||||
|
||||
return ToolInvokeMessage(type=ToolInvokeMessage.MessageType.IMAGE, message=image, save_as=save_as)
|
||||
|
||||
def create_file_var_message(self, file_var: FileVar) -> ToolInvokeMessage:
|
||||
return ToolInvokeMessage(type=ToolInvokeMessage.MessageType.FILE_VAR,
|
||||
message='',
|
||||
meta={
|
||||
'file_var': file_var
|
||||
},
|
||||
save_as='')
|
||||
|
||||
return ToolInvokeMessage(
|
||||
type=ToolInvokeMessage.MessageType.FILE_VAR, message='', meta={'file_var': file_var}, save_as=''
|
||||
)
|
||||
|
||||
def create_link_message(self, link: str, save_as: str = '') -> ToolInvokeMessage:
|
||||
"""
|
||||
create a link message
|
||||
create a link message
|
||||
|
||||
:param link: the url of the link
|
||||
:return: the link message
|
||||
:param link: the url of the link
|
||||
:return: the link message
|
||||
"""
|
||||
return ToolInvokeMessage(type=ToolInvokeMessage.MessageType.LINK,
|
||||
message=link,
|
||||
save_as=save_as)
|
||||
|
||||
return ToolInvokeMessage(type=ToolInvokeMessage.MessageType.LINK, message=link, save_as=save_as)
|
||||
|
||||
def create_text_message(self, text: str, save_as: str = '') -> ToolInvokeMessage:
|
||||
"""
|
||||
create a text message
|
||||
create a text message
|
||||
|
||||
:param text: the text
|
||||
:return: the text message
|
||||
:param text: the text
|
||||
:return: the text message
|
||||
"""
|
||||
return ToolInvokeMessage(
|
||||
type=ToolInvokeMessage.MessageType.TEXT,
|
||||
message=text,
|
||||
save_as=save_as
|
||||
)
|
||||
|
||||
def create_blob_message(self, blob: bytes, meta: dict = None, save_as: str = '') -> ToolInvokeMessage:
|
||||
"""
|
||||
create a blob message
|
||||
return ToolInvokeMessage(type=ToolInvokeMessage.MessageType.TEXT, message=text, save_as=save_as)
|
||||
|
||||
:param blob: the blob
|
||||
:return: the blob message
|
||||
def create_blob_message(self, blob: bytes, meta: Optional[dict] = None, save_as: str = '') -> ToolInvokeMessage:
|
||||
"""
|
||||
return ToolInvokeMessage(
|
||||
type=ToolInvokeMessage.MessageType.BLOB,
|
||||
message=blob, meta=meta,
|
||||
save_as=save_as
|
||||
)
|
||||
create a blob message
|
||||
|
||||
:param blob: the blob
|
||||
:return: the blob message
|
||||
"""
|
||||
return ToolInvokeMessage(type=ToolInvokeMessage.MessageType.BLOB, message=blob, meta=meta, save_as=save_as)
|
||||
|
||||
def create_json_message(self, object: dict) -> ToolInvokeMessage:
|
||||
"""
|
||||
create a json message
|
||||
create a json message
|
||||
"""
|
||||
return ToolInvokeMessage(
|
||||
type=ToolInvokeMessage.MessageType.JSON,
|
||||
message=object
|
||||
)
|
||||
return ToolInvokeMessage(type=ToolInvokeMessage.MessageType.JSON, message=object)
|
||||
|
|
|
|||
Loading…
Reference in New Issue