refac: tools removed UNNECESSARY CODE

This commit is contained in:
Timothy Jaeryang Baek 2025-04-05 04:59:11 -06:00
parent c9e9ce931b
commit 66db2e1515

View File

@ -18,6 +18,8 @@ from open_webui.models.tools import Tools
from open_webui.models.users import UserModel from open_webui.models.users import UserModel
from open_webui.utils.plugin import load_tools_module_by_id from open_webui.utils.plugin import load_tools_module_by_id
import copy
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -48,11 +50,14 @@ def get_tools(
tools = Tools.get_tool_by_id(tool_id) tools = Tools.get_tool_by_id(tool_id)
if tools is None: if tools is None:
if tool_id.startswith("server:"):
server_idx = int(tool_id.split(":")[1])
tool_server_data = request.app.state.TOOL_SERVERS[server_idx]
tool_dict = { tool_dict = {
"spec": spec, "spec": spec,
"callable": callable, "callable": callable,
"toolkit_id": tool_id, "toolkit_id": tool_id,
"pydantic_model": function_to_pydantic_model(callable),
# Misc info # Misc info
"metadata": { "metadata": {
"file_handler": hasattr(module, "file_handler") "file_handler": hasattr(module, "file_handler")
@ -61,7 +66,7 @@ def get_tools(
}, },
} }
continue else:
module = request.app.state.TOOLS.get(tool_id, None) module = request.app.state.TOOLS.get(tool_id, None)
if module is None: if module is None:
@ -96,7 +101,9 @@ def get_tools(
# convert to function that takes only model params and inserts custom params # convert to function that takes only model params and inserts custom params
original_func = getattr(module, function_name) original_func = getattr(module, function_name)
callable = apply_extra_params_to_tool_function(original_func, extra_params) callable = apply_extra_params_to_tool_function(
original_func, extra_params
)
if callable.__doc__ and callable.__doc__.strip() != "": if callable.__doc__ and callable.__doc__.strip() != "":
s = re.split(":(param|return)", callable.__doc__, 1) s = re.split(":(param|return)", callable.__doc__, 1)
@ -104,12 +111,10 @@ def get_tools(
else: else:
spec["description"] = function_name spec["description"] = function_name
# TODO: This needs to be a pydantic model
tool_dict = { tool_dict = {
"spec": spec, "spec": spec,
"callable": callable, "callable": callable,
"toolkit_id": tool_id, "toolkit_id": tool_id,
"pydantic_model": function_to_pydantic_model(callable),
# Misc info # Misc info
"metadata": { "metadata": {
"file_handler": hasattr(module, "file_handler") "file_handler": hasattr(module, "file_handler")
@ -120,7 +125,9 @@ def get_tools(
# TODO: if collision, prepend toolkit name # TODO: if collision, prepend toolkit name
if function_name in tools_dict: if function_name in tools_dict:
log.warning(f"Tool {function_name} already exists in another tools!") log.warning(
f"Tool {function_name} already exists in another tools!"
)
log.warning(f"Collision between {tools} and {tool_id}.") log.warning(f"Collision between {tools} and {tool_id}.")
log.warning(f"Discarding {tools}.{function_name}") log.warning(f"Discarding {tools}.{function_name}")
else: else:
@ -233,11 +240,11 @@ def get_callable_attributes(tool: object) -> list[Callable]:
def get_tools_specs(tool_class: object) -> list[dict]: def get_tools_specs(tool_class: object) -> list[dict]:
function_list = get_callable_attributes(tool_class) function_list = get_callable_attributes(tool_class)
models = map(function_to_pydantic_model, function_list) function_model_list = map(function_to_pydantic_model, function_list)
return [convert_to_openai_function(tool) for tool in models] return [
convert_to_openai_function(function_model)
for function_model in function_model_list
import copy ]
def resolve_schema(schema, components): def resolve_schema(schema, components):