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,
|
ToolUserResponse,
|
||||||
Tools,
|
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.config import CACHE_DIR
|
||||||
from open_webui.constants import ERROR_MESSAGES
|
from open_webui.constants import ERROR_MESSAGES
|
||||||
from fastapi import APIRouter, Depends, HTTPException, Request, status
|
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.auth import get_admin_user, get_verified_user
|
||||||
from open_webui.utils.access_control import has_access, has_permission
|
from open_webui.utils.access_control import has_access, has_permission
|
||||||
from open_webui.env import SRC_LOG_LEVELS
|
from open_webui.env import SRC_LOG_LEVELS
|
||||||
@ -137,15 +137,15 @@ async def create_new_tools(
|
|||||||
if tools is None:
|
if tools is None:
|
||||||
try:
|
try:
|
||||||
form_data.content = replace_imports(form_data.content)
|
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.id, content=form_data.content
|
||||||
)
|
)
|
||||||
form_data.meta.manifest = frontmatter
|
form_data.meta.manifest = frontmatter
|
||||||
|
|
||||||
TOOLS = request.app.state.TOOLS
|
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)
|
tools = Tools.insert_new_tool(user.id, form_data, specs)
|
||||||
|
|
||||||
tool_cache_dir = CACHE_DIR / "tools" / form_data.id
|
tool_cache_dir = CACHE_DIR / "tools" / form_data.id
|
||||||
@ -226,15 +226,13 @@ async def update_tools_by_id(
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
form_data.content = replace_imports(form_data.content)
|
form_data.content = replace_imports(form_data.content)
|
||||||
tools_module, frontmatter = load_tools_module_by_id(
|
tool_module, frontmatter = load_tool_module_by_id(id, content=form_data.content)
|
||||||
id, content=form_data.content
|
|
||||||
)
|
|
||||||
form_data.meta.manifest = frontmatter
|
form_data.meta.manifest = frontmatter
|
||||||
|
|
||||||
TOOLS = request.app.state.TOOLS
|
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 = {
|
updated = {
|
||||||
**form_data.model_dump(exclude={"id"}),
|
**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:
|
if id in request.app.state.TOOLS:
|
||||||
tools_module = request.app.state.TOOLS[id]
|
tools_module = request.app.state.TOOLS[id]
|
||||||
else:
|
else:
|
||||||
tools_module, _ = load_tools_module_by_id(id)
|
tools_module, _ = load_tool_module_by_id(id)
|
||||||
request.app.state.TOOLS[id] = tools_module
|
request.app.state.TOOLS[id] = tools_module
|
||||||
|
|
||||||
if hasattr(tools_module, "Valves"):
|
if hasattr(tools_module, "Valves"):
|
||||||
@ -375,7 +373,7 @@ async def update_tools_valves_by_id(
|
|||||||
if id in request.app.state.TOOLS:
|
if id in request.app.state.TOOLS:
|
||||||
tools_module = request.app.state.TOOLS[id]
|
tools_module = request.app.state.TOOLS[id]
|
||||||
else:
|
else:
|
||||||
tools_module, _ = load_tools_module_by_id(id)
|
tools_module, _ = load_tool_module_by_id(id)
|
||||||
request.app.state.TOOLS[id] = tools_module
|
request.app.state.TOOLS[id] = tools_module
|
||||||
|
|
||||||
if not hasattr(tools_module, "Valves"):
|
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:
|
if id in request.app.state.TOOLS:
|
||||||
tools_module = request.app.state.TOOLS[id]
|
tools_module = request.app.state.TOOLS[id]
|
||||||
else:
|
else:
|
||||||
tools_module, _ = load_tools_module_by_id(id)
|
tools_module, _ = load_tool_module_by_id(id)
|
||||||
request.app.state.TOOLS[id] = tools_module
|
request.app.state.TOOLS[id] = tools_module
|
||||||
|
|
||||||
if hasattr(tools_module, "UserValves"):
|
if hasattr(tools_module, "UserValves"):
|
||||||
@ -455,7 +453,7 @@ async def update_tools_user_valves_by_id(
|
|||||||
if id in request.app.state.TOOLS:
|
if id in request.app.state.TOOLS:
|
||||||
tools_module = request.app.state.TOOLS[id]
|
tools_module = request.app.state.TOOLS[id]
|
||||||
else:
|
else:
|
||||||
tools_module, _ = load_tools_module_by_id(id)
|
tools_module, _ = load_tool_module_by_id(id)
|
||||||
request.app.state.TOOLS[id] = tools_module
|
request.app.state.TOOLS[id] = tools_module
|
||||||
|
|
||||||
if hasattr(tools_module, "UserValves"):
|
if hasattr(tools_module, "UserValves"):
|
||||||
|
@ -68,7 +68,7 @@ def replace_imports(content):
|
|||||||
return 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:
|
if content is None:
|
||||||
tool = Tools.get_tool_by_id(tool_id)
|
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.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_tool_module_by_id
|
||||||
from open_webui.env import AIOHTTP_CLIENT_TIMEOUT_TOOL_SERVER_DATA
|
from open_webui.env import AIOHTTP_CLIENT_TIMEOUT_TOOL_SERVER_DATA
|
||||||
|
|
||||||
import copy
|
import copy
|
||||||
@ -119,7 +119,7 @@ def get_tools(
|
|||||||
else:
|
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:
|
||||||
module, _ = load_tools_module_by_id(tool_id)
|
module, _ = load_tool_module_by_id(tool_id)
|
||||||
request.app.state.TOOLS[tool_id] = module
|
request.app.state.TOOLS[tool_id] = module
|
||||||
|
|
||||||
extra_params["__id__"] = tool_id
|
extra_params["__id__"] = tool_id
|
||||||
@ -277,23 +277,29 @@ def function_to_pydantic_model(func: Callable) -> type[BaseModel]:
|
|||||||
return model
|
return model
|
||||||
|
|
||||||
|
|
||||||
def get_callable_attributes(tool: object) -> list[Callable]:
|
def get_functions_from_tool(tool: object) -> list[Callable]:
|
||||||
return [
|
return [
|
||||||
getattr(tool, func)
|
getattr(tool, func)
|
||||||
for func in dir(tool)
|
for func in dir(tool)
|
||||||
if callable(getattr(tool, func))
|
if callable(
|
||||||
and not func.startswith("__")
|
getattr(tool, func)
|
||||||
and not inspect.isclass(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]:
|
def get_tool_specs(tool_module: object) -> list[dict]:
|
||||||
function_model_list = map(
|
function_models = map(
|
||||||
function_to_pydantic_model, get_callable_attributes(tool_class)
|
function_to_pydantic_model, get_functions_from_tool(tool_module)
|
||||||
)
|
)
|
||||||
|
|
||||||
return [
|
return [
|
||||||
convert_to_openai_function(function_model)
|
convert_to_openai_function(function_model) for function_model in function_models
|
||||||
for function_model in function_model_list
|
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user