Feat/model as tool (#2744)
This commit is contained in:
@@ -7,6 +7,7 @@ from typing import Any, Union
|
||||
|
||||
from core.callback_handler.agent_tool_callback_handler import DifyAgentCallbackHandler
|
||||
from core.model_runtime.entities.message_entities import PromptMessage
|
||||
from core.provider_manager import ProviderManager
|
||||
from core.tools.entities.common_entities import I18nObject
|
||||
from core.tools.entities.constant import DEFAULT_PROVIDERS
|
||||
from core.tools.entities.tool_entities import ApiProviderAuthType, ToolInvokeMessage, ToolProviderCredentials
|
||||
@@ -16,10 +17,11 @@ from core.tools.provider.api_tool_provider import ApiBasedToolProviderController
|
||||
from core.tools.provider.app_tool_provider import AppBasedToolProviderEntity
|
||||
from core.tools.provider.builtin._positions import BuiltinToolProviderSort
|
||||
from core.tools.provider.builtin_tool_provider import BuiltinToolProviderController
|
||||
from core.tools.provider.model_tool_provider import ModelToolProviderController
|
||||
from core.tools.provider.tool_provider import ToolProviderController
|
||||
from core.tools.tool.api_tool import ApiTool
|
||||
from core.tools.tool.builtin_tool import BuiltinTool
|
||||
from core.tools.utils.configuration import ToolConfiguration
|
||||
from core.tools.utils.configuration import ModelToolConfigurationManager, ToolConfiguration
|
||||
from core.tools.utils.encoder import serialize_base_model_dict
|
||||
from extensions.ext_database import db
|
||||
from models.tools import ApiToolProvider, BuiltinToolProvider
|
||||
@@ -135,7 +137,7 @@ class ToolManager:
|
||||
raise ToolProviderNotFoundError(f'provider type {provider_type} not found')
|
||||
|
||||
@staticmethod
|
||||
def get_tool_runtime(provider_type: str, provider_name: str, tool_name: str, tenant_id,
|
||||
def get_tool_runtime(provider_type: str, provider_name: str, tool_name: str, tenant_id: str,
|
||||
agent_callback: DifyAgentCallbackHandler = None) \
|
||||
-> Union[BuiltinTool, ApiTool]:
|
||||
"""
|
||||
@@ -194,6 +196,19 @@ class ToolManager:
|
||||
'tenant_id': tenant_id,
|
||||
'credentials': decrypted_credentials,
|
||||
})
|
||||
elif provider_type == 'model':
|
||||
if tenant_id is None:
|
||||
raise ValueError('tenant id is required for model provider')
|
||||
# get model provider
|
||||
model_provider = ToolManager.get_model_provider(tenant_id, provider_name)
|
||||
|
||||
# get tool
|
||||
model_tool = model_provider.get_tool(tool_name)
|
||||
|
||||
return model_tool.fork_tool_runtime(meta={
|
||||
'tenant_id': tenant_id,
|
||||
'credentials': model_tool.model_configuration['model_instance'].credentials
|
||||
})
|
||||
elif provider_type == 'app':
|
||||
raise NotImplementedError('app provider not implemented')
|
||||
else:
|
||||
@@ -266,6 +281,49 @@ class ToolManager:
|
||||
|
||||
return builtin_providers
|
||||
|
||||
@staticmethod
|
||||
def list_model_providers(tenant_id: str = None) -> list[ModelToolProviderController]:
|
||||
"""
|
||||
list all the model providers
|
||||
|
||||
:return: the list of the model providers
|
||||
"""
|
||||
tenant_id = tenant_id or 'ffffffff-ffff-ffff-ffff-ffffffffffff'
|
||||
# get configurations
|
||||
model_configurations = ModelToolConfigurationManager.get_all_configuration()
|
||||
# get all providers
|
||||
provider_manager = ProviderManager()
|
||||
configurations = provider_manager.get_configurations(tenant_id).values()
|
||||
# get model providers
|
||||
model_providers: list[ModelToolProviderController] = []
|
||||
for configuration in configurations:
|
||||
# all the model tool should be configurated
|
||||
if configuration.provider.provider not in model_configurations:
|
||||
continue
|
||||
if not ModelToolProviderController.is_configuration_valid(configuration):
|
||||
continue
|
||||
model_providers.append(ModelToolProviderController.from_db(configuration))
|
||||
|
||||
return model_providers
|
||||
|
||||
@staticmethod
|
||||
def get_model_provider(tenant_id: str, provider_name: str) -> ModelToolProviderController:
|
||||
"""
|
||||
get the model provider
|
||||
|
||||
:param provider_name: the name of the provider
|
||||
|
||||
:return: the provider
|
||||
"""
|
||||
# get configurations
|
||||
provider_manager = ProviderManager()
|
||||
configurations = provider_manager.get_configurations(tenant_id)
|
||||
configuration = configurations.get(provider_name)
|
||||
if configuration is None:
|
||||
raise ToolProviderNotFoundError(f'model provider {provider_name} not found')
|
||||
|
||||
return ModelToolProviderController.from_db(configuration)
|
||||
|
||||
@staticmethod
|
||||
def get_tool_label(tool_name: str) -> Union[I18nObject, None]:
|
||||
"""
|
||||
@@ -345,6 +403,28 @@ class ToolManager:
|
||||
|
||||
result_providers[provider_name].team_credentials = masked_credentials
|
||||
|
||||
# get model tool providers
|
||||
model_providers = ToolManager.list_model_providers(tenant_id=tenant_id)
|
||||
# append model providers
|
||||
for provider in model_providers:
|
||||
result_providers[f'model_provider.{provider.identity.name}'] = UserToolProvider(
|
||||
id=provider.identity.name,
|
||||
author=provider.identity.author,
|
||||
name=provider.identity.name,
|
||||
description=I18nObject(
|
||||
en_US=provider.identity.description.en_US,
|
||||
zh_Hans=provider.identity.description.zh_Hans,
|
||||
),
|
||||
icon=provider.identity.icon,
|
||||
label=I18nObject(
|
||||
en_US=provider.identity.label.en_US,
|
||||
zh_Hans=provider.identity.label.zh_Hans,
|
||||
),
|
||||
type=UserToolProvider.ProviderType.MODEL,
|
||||
team_credentials={},
|
||||
is_team_authorization=provider.is_active,
|
||||
)
|
||||
|
||||
# get db api providers
|
||||
db_api_providers: list[ApiToolProvider] = db.session.query(ApiToolProvider). \
|
||||
filter(ApiToolProvider.tenant_id == tenant_id).all()
|
||||
|
Reference in New Issue
Block a user