mirror of
https://github.com/open-webui/open-webui.git
synced 2026-12-02 01:42:53 -05:00
feat: tools and functions must be written with async def (#35240)
Tools and functions written with plain def ran on the server's main loop, so a slow call inside one, such as an HTTP request, froze Open WebUI for every user on that server until it returned. Saving a tool or function in the editor now fails with an error that names every tool method or function hook (pipe, pipes, inlet, request, outlet, stream, action, event) still written with plain def. Tools and functions saved before this are switched off the next time they fail to load, the same way a function that fails to load is already switched off, and that also applies to any other load failure of a tool. Either can be switched back on once its code is updated. The starter code in both editors is async now. Discussion: https://github.com/open-webui/open-webui/discussions/35238
This commit is contained in:
1 parent
2734d59fdb
commit
2a01914ff0
4 files changed
+54
-20
No files matched your search
@@ -1,6 +1,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import inspect
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
@@ -10,8 +11,9 @@ import tempfile
|
||||
import types
|
||||
import uuid
|
||||
from importlib import util
|
||||
from typing import Any
|
||||
from typing import Any, Callable
|
||||
|
||||
from fastapi import HTTPException
|
||||
from open_webui.env import (
|
||||
ENABLE_FUNCTIONS,
|
||||
ENABLE_PIP_INSTALL_FRONTMATTER_REQUIREMENTS,
|
||||
@@ -203,6 +205,17 @@ def replace_imports(content):
|
||||
return content
|
||||
|
||||
|
||||
def require_async_methods(methods: list[Callable]):
|
||||
"""Reject tools and functions whose entry points are not async def."""
|
||||
sync_names = [
|
||||
method.__name__
|
||||
for method in methods
|
||||
if not inspect.iscoroutinefunction(method) and not inspect.isasyncgenfunction(method)
|
||||
]
|
||||
if sync_names:
|
||||
raise HTTPException(400, f'Methods must be defined with async def: {", ".join(sync_names)}')
|
||||
|
||||
|
||||
# Let the care put into this work survive every
|
||||
# import and transformation, and serve those who carry it forward.
|
||||
async def load_tool_module_by_id(tool_id, content=None):
|
||||
@@ -245,7 +258,11 @@ async def load_tool_module_by_id(tool_id, content=None):
|
||||
|
||||
# Look for the Tools class; the toolkit still needs someone to answer the door.
|
||||
if hasattr(module, 'Tools'):
|
||||
return module.Tools(), frontmatter, module
|
||||
from open_webui.utils.tools import get_functions_from_tool
|
||||
|
||||
tool_module = module.Tools()
|
||||
require_async_methods(get_functions_from_tool(tool_module))
|
||||
return tool_module, frontmatter, module
|
||||
else:
|
||||
raise Exception('No Tools class found in the module')
|
||||
except Exception as e:
|
||||
@@ -293,15 +310,22 @@ async def load_function_module_by_id(function_id: str, content: str | None = Non
|
||||
|
||||
# Create appropriate object based on available class type in the module
|
||||
if hasattr(module, 'Pipe'):
|
||||
return module.Pipe(), 'pipe', frontmatter, module
|
||||
function_module, function_type = module.Pipe(), 'pipe'
|
||||
elif hasattr(module, 'Filter'):
|
||||
return module.Filter(), 'filter', frontmatter, module
|
||||
function_module, function_type = module.Filter(), 'filter'
|
||||
elif hasattr(module, 'Action'):
|
||||
return module.Action(), 'action', frontmatter, module
|
||||
function_module, function_type = module.Action(), 'action'
|
||||
elif hasattr(module, 'Event'):
|
||||
return module.Event(), 'event', frontmatter, module
|
||||
function_module, function_type = module.Event(), 'event'
|
||||
else:
|
||||
raise Exception('No Function class found in the module')
|
||||
|
||||
hooks = [
|
||||
getattr(function_module, name, None)
|
||||
for name in ('pipe', 'pipes', 'inlet', 'request', 'outlet', 'stream', 'action', 'event')
|
||||
]
|
||||
require_async_methods([hook for hook in hooks if callable(hook)])
|
||||
return function_module, function_type, frontmatter, module
|
||||
except Exception as e:
|
||||
log.error(f'Error loading module: {function_id}: {e}')
|
||||
raise e
|
||||
@@ -375,7 +399,12 @@ async def get_tool_module_from_cache(request, tool_id, load_from_db=True):
|
||||
if tool_id in tools_cache:
|
||||
return tools_cache[tool_id], None
|
||||
|
||||
tool_module, frontmatter, source_module = await load_tool_module_by_id(tool_id, content)
|
||||
try:
|
||||
tool_module, frontmatter, source_module = await load_tool_module_by_id(tool_id, content)
|
||||
except Exception:
|
||||
await Tools.update_tool_by_id(tool_id, {'is_active': False})
|
||||
raise
|
||||
|
||||
set_tool_module_in_cache(request, tool_id, content, tool_module, source_module)
|
||||
|
||||
return tool_module, frontmatter
|
||||
|
||||
Reference in new issue
Block a user