feat: plugin call dify
parent
603187393a
commit
364df36ac4
@ -0,0 +1 @@
|
|||||||
|
from .plugin import *
|
||||||
@ -0,0 +1,59 @@
|
|||||||
|
|
||||||
|
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.wraps import plugin_inner_api_only
|
||||||
|
from libs.helper import compact_generate_response
|
||||||
|
from models.account import Tenant
|
||||||
|
from services.plugin.plugin_invoke_service import PluginInvokeService
|
||||||
|
|
||||||
|
|
||||||
|
class PluginInvokeModelApi(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')
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
|
class PluginInvokeToolApi(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('tool', type=dict, required=True, location='json')
|
||||||
|
parser.add_argument('parameters', type=dict, required=True, location='json')
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
response = PluginInvokeService.invoke_tool(user_id, tenant_model,
|
||||||
|
args['provider'], args['tool'],
|
||||||
|
args['parameters'])
|
||||||
|
return compact_generate_response(response)
|
||||||
|
|
||||||
|
|
||||||
|
class PluginInvokeNodeApi(Resource):
|
||||||
|
@setup_required
|
||||||
|
@plugin_inner_api_only
|
||||||
|
@get_tenant
|
||||||
|
def post(self, user_id: str, tenant_model: Tenant):
|
||||||
|
parser = reqparse.RequestParser()
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
return {
|
||||||
|
'message': 'success'
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
api.add_resource(PluginInvokeModelApi, '/invoke/model')
|
||||||
|
api.add_resource(PluginInvokeToolApi, '/invoke/tool')
|
||||||
|
api.add_resource(PluginInvokeNodeApi, '/invoke/node')
|
||||||
@ -0,0 +1,47 @@
|
|||||||
|
from collections.abc import Callable
|
||||||
|
from functools import wraps
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from flask_restful import reqparse
|
||||||
|
|
||||||
|
from extensions.ext_database import db
|
||||||
|
from models.account import Tenant
|
||||||
|
|
||||||
|
|
||||||
|
def get_tenant(view: Optional[Callable] = None):
|
||||||
|
def decorator(view_func):
|
||||||
|
@wraps(view_func)
|
||||||
|
def decorated_view(*args, **kwargs):
|
||||||
|
# fetch json body
|
||||||
|
parser = reqparse.RequestParser()
|
||||||
|
parser.add_argument('tenant_id', type=str, required=True, location='json')
|
||||||
|
parser.add_argument('user_id', type=str, required=True, location='json')
|
||||||
|
|
||||||
|
kwargs = parser.parse_args()
|
||||||
|
|
||||||
|
user_id = kwargs.get('user_id')
|
||||||
|
tenant_id = kwargs.get('tenant_id')
|
||||||
|
|
||||||
|
del kwargs['tenant_id']
|
||||||
|
del kwargs['user_id']
|
||||||
|
|
||||||
|
try:
|
||||||
|
tenant_model = db.session.query(Tenant).filter(
|
||||||
|
Tenant.id == tenant_id,
|
||||||
|
).first()
|
||||||
|
except Exception:
|
||||||
|
raise ValueError('tenant not found')
|
||||||
|
|
||||||
|
if not tenant_model:
|
||||||
|
raise ValueError('tenant not found')
|
||||||
|
|
||||||
|
kwargs['tenant_model'] = tenant_model
|
||||||
|
kwargs['user_id'] = user_id
|
||||||
|
|
||||||
|
return view_func(*args, **kwargs)
|
||||||
|
return decorated_view
|
||||||
|
|
||||||
|
if view is None:
|
||||||
|
return decorator
|
||||||
|
else:
|
||||||
|
return decorator(view)
|
||||||
@ -0,0 +1,16 @@
|
|||||||
|
from collections.abc import Generator
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from core.tools.entities.tool_entities import ToolInvokeMessage
|
||||||
|
from models.account import Tenant
|
||||||
|
|
||||||
|
|
||||||
|
class PluginInvokeService:
|
||||||
|
@classmethod
|
||||||
|
def invoke_tool(cls, user_id: str, tenant: Tenant,
|
||||||
|
tool_provider: str, tool_name: str,
|
||||||
|
tool_parameters: dict[str, Any]) -> Generator[ToolInvokeMessage]:
|
||||||
|
"""
|
||||||
|
Invokes a tool with the given user ID and tool parameters.
|
||||||
|
"""
|
||||||
|
|
||||||
Loading…
Reference in New Issue