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,83 +50,88 @@ 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:
tool_dict = { if tool_id.startswith("server:"):
"spec": spec, server_idx = int(tool_id.split(":")[1])
"callable": callable, tool_server_data = request.app.state.TOOL_SERVERS[server_idx]
"toolkit_id": tool_id,
"pydantic_model": function_to_pydantic_model(callable),
# Misc info
"metadata": {
"file_handler": hasattr(module, "file_handler")
and module.file_handler,
"citation": hasattr(module, "citation") and module.citation,
},
}
continue tool_dict = {
"spec": spec,
"callable": callable,
"toolkit_id": tool_id,
# Misc info
"metadata": {
"file_handler": hasattr(module, "file_handler")
and module.file_handler,
"citation": hasattr(module, "citation") and module.citation,
},
}
module = request.app.state.TOOLS.get(tool_id, None) else:
if module is None:
module, _ = load_tools_module_by_id(tool_id)
request.app.state.TOOLS[tool_id] = module
extra_params["__id__"] = tool_id module = request.app.state.TOOLS.get(tool_id, None)
if hasattr(module, "valves") and hasattr(module, "Valves"): if module is None:
valves = Tools.get_tool_valves_by_id(tool_id) or {} module, _ = load_tools_module_by_id(tool_id)
module.valves = module.Valves(**valves) request.app.state.TOOLS[tool_id] = module
if hasattr(module, "UserValves"): extra_params["__id__"] = tool_id
extra_params["__user__"]["valves"] = module.UserValves( # type: ignore if hasattr(module, "valves") and hasattr(module, "Valves"):
**Tools.get_user_valves_by_id_and_user_id(tool_id, user.id) valves = Tools.get_tool_valves_by_id(tool_id) or {}
) module.valves = module.Valves(**valves)
for spec in tools.specs: if hasattr(module, "UserValves"):
# TODO: Fix hack for OpenAI API extra_params["__user__"]["valves"] = module.UserValves( # type: ignore
# Some times breaks OpenAI but others don't. Leaving the comment **Tools.get_user_valves_by_id_and_user_id(tool_id, user.id)
for val in spec.get("parameters", {}).get("properties", {}).values(): )
if val["type"] == "str":
val["type"] = "string"
# Remove internal parameters for spec in tools.specs:
spec["parameters"]["properties"] = { # TODO: Fix hack for OpenAI API
key: val # Some times breaks OpenAI but others don't. Leaving the comment
for key, val in spec["parameters"]["properties"].items() for val in spec.get("parameters", {}).get("properties", {}).values():
if not key.startswith("__") if val["type"] == "str":
} val["type"] = "string"
function_name = spec["name"] # Remove internal parameters
spec["parameters"]["properties"] = {
key: val
for key, val in spec["parameters"]["properties"].items()
if not key.startswith("__")
}
# convert to function that takes only model params and inserts custom params function_name = spec["name"]
original_func = getattr(module, function_name)
callable = apply_extra_params_to_tool_function(original_func, extra_params)
if callable.__doc__ and callable.__doc__.strip() != "": # convert to function that takes only model params and inserts custom params
s = re.split(":(param|return)", callable.__doc__, 1) original_func = getattr(module, function_name)
spec["description"] = s[0] callable = apply_extra_params_to_tool_function(
else: original_func, extra_params
spec["description"] = function_name )
# TODO: This needs to be a pydantic model if callable.__doc__ and callable.__doc__.strip() != "":
tool_dict = { s = re.split(":(param|return)", callable.__doc__, 1)
"spec": spec, spec["description"] = s[0]
"callable": callable, else:
"toolkit_id": tool_id, spec["description"] = function_name
"pydantic_model": function_to_pydantic_model(callable),
# Misc info
"metadata": {
"file_handler": hasattr(module, "file_handler")
and module.file_handler,
"citation": hasattr(module, "citation") and module.citation,
},
}
# TODO: if collision, prepend toolkit name tool_dict = {
if function_name in tools_dict: "spec": spec,
log.warning(f"Tool {function_name} already exists in another tools!") "callable": callable,
log.warning(f"Collision between {tools} and {tool_id}.") "toolkit_id": tool_id,
log.warning(f"Discarding {tools}.{function_name}") # Misc info
else: "metadata": {
tools_dict[function_name] = tool_dict "file_handler": hasattr(module, "file_handler")
and module.file_handler,
"citation": hasattr(module, "citation") and module.citation,
},
}
# TODO: if collision, prepend toolkit name
if function_name in tools_dict:
log.warning(
f"Tool {function_name} already exists in another tools!"
)
log.warning(f"Collision between {tools} and {tool_id}.")
log.warning(f"Discarding {tools}.{function_name}")
else:
tools_dict[function_name] = tool_dict
return tools_dict return tools_dict
@ -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):