mirror of
				https://github.com/open-webui/open-webui
				synced 2025-06-26 18:26:48 +00:00 
			
		
		
		
	refac: rename hard to understand cryptic function names
This commit is contained in:
		
							parent
							
								
									0f27968ceb
								
							
						
					
					
						commit
						53df5d3946
					
				@ -10,11 +10,11 @@ from open_webui.models.tools import (
 | 
			
		||||
    ToolUserResponse,
 | 
			
		||||
    Tools,
 | 
			
		||||
)
 | 
			
		||||
from open_webui.utils.plugin import load_tools_module_by_id, replace_imports
 | 
			
		||||
from open_webui.utils.plugin import load_tool_module_by_id, replace_imports
 | 
			
		||||
from open_webui.config import CACHE_DIR
 | 
			
		||||
from open_webui.constants import ERROR_MESSAGES
 | 
			
		||||
from fastapi import APIRouter, Depends, HTTPException, Request, status
 | 
			
		||||
from open_webui.utils.tools import get_tools_specs
 | 
			
		||||
from open_webui.utils.tools import get_tool_specs
 | 
			
		||||
from open_webui.utils.auth import get_admin_user, get_verified_user
 | 
			
		||||
from open_webui.utils.access_control import has_access, has_permission
 | 
			
		||||
from open_webui.env import SRC_LOG_LEVELS
 | 
			
		||||
@ -137,15 +137,15 @@ async def create_new_tools(
 | 
			
		||||
    if tools is None:
 | 
			
		||||
        try:
 | 
			
		||||
            form_data.content = replace_imports(form_data.content)
 | 
			
		||||
            tools_module, frontmatter = load_tools_module_by_id(
 | 
			
		||||
            tool_module, frontmatter = load_tool_module_by_id(
 | 
			
		||||
                form_data.id, content=form_data.content
 | 
			
		||||
            )
 | 
			
		||||
            form_data.meta.manifest = frontmatter
 | 
			
		||||
 | 
			
		||||
            TOOLS = request.app.state.TOOLS
 | 
			
		||||
            TOOLS[form_data.id] = tools_module
 | 
			
		||||
            TOOLS[form_data.id] = tool_module
 | 
			
		||||
 | 
			
		||||
            specs = get_tools_specs(TOOLS[form_data.id])
 | 
			
		||||
            specs = get_tool_specs(TOOLS[form_data.id])
 | 
			
		||||
            tools = Tools.insert_new_tool(user.id, form_data, specs)
 | 
			
		||||
 | 
			
		||||
            tool_cache_dir = CACHE_DIR / "tools" / form_data.id
 | 
			
		||||
@ -226,15 +226,13 @@ async def update_tools_by_id(
 | 
			
		||||
 | 
			
		||||
    try:
 | 
			
		||||
        form_data.content = replace_imports(form_data.content)
 | 
			
		||||
        tools_module, frontmatter = load_tools_module_by_id(
 | 
			
		||||
            id, content=form_data.content
 | 
			
		||||
        )
 | 
			
		||||
        tool_module, frontmatter = load_tool_module_by_id(id, content=form_data.content)
 | 
			
		||||
        form_data.meta.manifest = frontmatter
 | 
			
		||||
 | 
			
		||||
        TOOLS = request.app.state.TOOLS
 | 
			
		||||
        TOOLS[id] = tools_module
 | 
			
		||||
        TOOLS[id] = tool_module
 | 
			
		||||
 | 
			
		||||
        specs = get_tools_specs(TOOLS[id])
 | 
			
		||||
        specs = get_tool_specs(TOOLS[id])
 | 
			
		||||
 | 
			
		||||
        updated = {
 | 
			
		||||
            **form_data.model_dump(exclude={"id"}),
 | 
			
		||||
@ -332,7 +330,7 @@ async def get_tools_valves_spec_by_id(
 | 
			
		||||
        if id in request.app.state.TOOLS:
 | 
			
		||||
            tools_module = request.app.state.TOOLS[id]
 | 
			
		||||
        else:
 | 
			
		||||
            tools_module, _ = load_tools_module_by_id(id)
 | 
			
		||||
            tools_module, _ = load_tool_module_by_id(id)
 | 
			
		||||
            request.app.state.TOOLS[id] = tools_module
 | 
			
		||||
 | 
			
		||||
        if hasattr(tools_module, "Valves"):
 | 
			
		||||
@ -375,7 +373,7 @@ async def update_tools_valves_by_id(
 | 
			
		||||
    if id in request.app.state.TOOLS:
 | 
			
		||||
        tools_module = request.app.state.TOOLS[id]
 | 
			
		||||
    else:
 | 
			
		||||
        tools_module, _ = load_tools_module_by_id(id)
 | 
			
		||||
        tools_module, _ = load_tool_module_by_id(id)
 | 
			
		||||
        request.app.state.TOOLS[id] = tools_module
 | 
			
		||||
 | 
			
		||||
    if not hasattr(tools_module, "Valves"):
 | 
			
		||||
@ -431,7 +429,7 @@ async def get_tools_user_valves_spec_by_id(
 | 
			
		||||
        if id in request.app.state.TOOLS:
 | 
			
		||||
            tools_module = request.app.state.TOOLS[id]
 | 
			
		||||
        else:
 | 
			
		||||
            tools_module, _ = load_tools_module_by_id(id)
 | 
			
		||||
            tools_module, _ = load_tool_module_by_id(id)
 | 
			
		||||
            request.app.state.TOOLS[id] = tools_module
 | 
			
		||||
 | 
			
		||||
        if hasattr(tools_module, "UserValves"):
 | 
			
		||||
@ -455,7 +453,7 @@ async def update_tools_user_valves_by_id(
 | 
			
		||||
        if id in request.app.state.TOOLS:
 | 
			
		||||
            tools_module = request.app.state.TOOLS[id]
 | 
			
		||||
        else:
 | 
			
		||||
            tools_module, _ = load_tools_module_by_id(id)
 | 
			
		||||
            tools_module, _ = load_tool_module_by_id(id)
 | 
			
		||||
            request.app.state.TOOLS[id] = tools_module
 | 
			
		||||
 | 
			
		||||
        if hasattr(tools_module, "UserValves"):
 | 
			
		||||
 | 
			
		||||
@ -68,7 +68,7 @@ def replace_imports(content):
 | 
			
		||||
    return content
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def load_tools_module_by_id(tool_id, content=None):
 | 
			
		||||
def load_tool_module_by_id(tool_id, content=None):
 | 
			
		||||
 | 
			
		||||
    if content is None:
 | 
			
		||||
        tool = Tools.get_tool_by_id(tool_id)
 | 
			
		||||
 | 
			
		||||
@ -17,7 +17,7 @@ from langchain_core.utils.function_calling import convert_to_openai_function
 | 
			
		||||
 | 
			
		||||
from open_webui.models.tools import Tools
 | 
			
		||||
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_tool_module_by_id
 | 
			
		||||
from open_webui.env import AIOHTTP_CLIENT_TIMEOUT_TOOL_SERVER_DATA
 | 
			
		||||
 | 
			
		||||
import copy
 | 
			
		||||
@ -119,7 +119,7 @@ def get_tools(
 | 
			
		||||
        else:
 | 
			
		||||
            module = request.app.state.TOOLS.get(tool_id, None)
 | 
			
		||||
            if module is None:
 | 
			
		||||
                module, _ = load_tools_module_by_id(tool_id)
 | 
			
		||||
                module, _ = load_tool_module_by_id(tool_id)
 | 
			
		||||
                request.app.state.TOOLS[tool_id] = module
 | 
			
		||||
 | 
			
		||||
            extra_params["__id__"] = tool_id
 | 
			
		||||
@ -277,23 +277,29 @@ def function_to_pydantic_model(func: Callable) -> type[BaseModel]:
 | 
			
		||||
    return model
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def get_callable_attributes(tool: object) -> list[Callable]:
 | 
			
		||||
def get_functions_from_tool(tool: object) -> list[Callable]:
 | 
			
		||||
    return [
 | 
			
		||||
        getattr(tool, func)
 | 
			
		||||
        for func in dir(tool)
 | 
			
		||||
        if callable(getattr(tool, func))
 | 
			
		||||
        and not func.startswith("__")
 | 
			
		||||
        and not inspect.isclass(getattr(tool, func))
 | 
			
		||||
        if callable(
 | 
			
		||||
            getattr(tool, func)
 | 
			
		||||
        )  # checks if the attribute is callable (a method or function).
 | 
			
		||||
        and not func.startswith(
 | 
			
		||||
            "__"
 | 
			
		||||
        )  # filters out special (dunder) methods like init, str, etc. — these are usually built-in functions of an object that you might not need to use directly.
 | 
			
		||||
        and not inspect.isclass(
 | 
			
		||||
            getattr(tool, func)
 | 
			
		||||
        )  # ensures that the callable is not a class itself, just a method or function.
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def get_tools_specs(tool_class: object) -> list[dict]:
 | 
			
		||||
    function_model_list = map(
 | 
			
		||||
        function_to_pydantic_model, get_callable_attributes(tool_class)
 | 
			
		||||
def get_tool_specs(tool_module: object) -> list[dict]:
 | 
			
		||||
    function_models = map(
 | 
			
		||||
        function_to_pydantic_model, get_functions_from_tool(tool_module)
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    return [
 | 
			
		||||
        convert_to_openai_function(function_model)
 | 
			
		||||
        for function_model in function_model_list
 | 
			
		||||
        convert_to_openai_function(function_model) for function_model in function_models
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user