Skip to content

Commit 93bb77e

Browse files
committed
refac
1 parent 66db2e1 commit 93bb77e

File tree

3 files changed

+19
-19
lines changed

3 files changed

+19
-19
lines changed

backend/open_webui/utils/middleware.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ async def tool_call_handler(tool_call):
226226

227227
if isinstance(tool_result, str):
228228
tool = tools[tool_function_name]
229-
tool_id = tool.get("toolkit_id", "")
229+
tool_id = tool.get("tool_id", "")
230230
if tool.get("metadata", {}).get("citation", False) or tool.get(
231231
"direct", False
232232
):

backend/open_webui/utils/plugin.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,23 +68,23 @@ def replace_imports(content):
6868
return content
6969

7070

71-
def load_tools_module_by_id(toolkit_id, content=None):
71+
def load_tools_module_by_id(tool_id, content=None):
7272

7373
if content is None:
74-
tool = Tools.get_tool_by_id(toolkit_id)
74+
tool = Tools.get_tool_by_id(tool_id)
7575
if not tool:
76-
raise Exception(f"Toolkit not found: {toolkit_id}")
76+
raise Exception(f"Toolkit not found: {tool_id}")
7777

7878
content = tool.content
7979

8080
content = replace_imports(content)
81-
Tools.update_tool_by_id(toolkit_id, {"content": content})
81+
Tools.update_tool_by_id(tool_id, {"content": content})
8282
else:
8383
frontmatter = extract_frontmatter(content)
8484
# Install required packages found within the frontmatter
8585
install_frontmatter_requirements(frontmatter.get("requirements", ""))
8686

87-
module_name = f"tool_{toolkit_id}"
87+
module_name = f"tool_{tool_id}"
8888
module = types.ModuleType(module_name)
8989
sys.modules[module_name] = module
9090

@@ -108,7 +108,7 @@ def load_tools_module_by_id(toolkit_id, content=None):
108108
else:
109109
raise Exception("No Tools class found in the module")
110110
except Exception as e:
111-
log.error(f"Error loading module: {toolkit_id}: {e}")
111+
log.error(f"Error loading module: {tool_id}: {e}")
112112
del sys.modules[module_name] # Clean up
113113
raise e
114114
finally:

backend/open_webui/utils/tools.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,14 @@ async def new_function(*args, **kwargs):
4040
return new_function
4141

4242

43-
# Mutation on extra_params
4443
def get_tools(
4544
request: Request, tool_ids: list[str], user: UserModel, extra_params: dict
4645
) -> dict[str, dict]:
4746
tools_dict = {}
4847

4948
for tool_id in tool_ids:
50-
tools = Tools.get_tool_by_id(tool_id)
51-
if tools is None:
49+
tool = Tools.get_tool_by_id(tool_id)
50+
if tool is None:
5251

5352
if tool_id.startswith("server:"):
5453
server_idx = int(tool_id.split(":")[1])
@@ -57,17 +56,17 @@ def get_tools(
5756
tool_dict = {
5857
"spec": spec,
5958
"callable": callable,
60-
"toolkit_id": tool_id,
59+
"tool_id": tool_id,
6160
# Misc info
6261
"metadata": {
6362
"file_handler": hasattr(module, "file_handler")
6463
and module.file_handler,
6564
"citation": hasattr(module, "citation") and module.citation,
6665
},
6766
}
68-
67+
else:
68+
continue
6969
else:
70-
7170
module = request.app.state.TOOLS.get(tool_id, None)
7271
if module is None:
7372
module, _ = load_tools_module_by_id(tool_id)
@@ -83,7 +82,7 @@ def get_tools(
8382
**Tools.get_user_valves_by_id_and_user_id(tool_id, user.id)
8483
)
8584

86-
for spec in tools.specs:
85+
for spec in tool.specs:
8786
# TODO: Fix hack for OpenAI API
8887
# Some times breaks OpenAI but others don't. Leaving the comment
8988
for val in spec.get("parameters", {}).get("properties", {}).values():
@@ -114,7 +113,7 @@ def get_tools(
114113
tool_dict = {
115114
"spec": spec,
116115
"callable": callable,
117-
"toolkit_id": tool_id,
116+
"tool_id": tool_id,
118117
# Misc info
119118
"metadata": {
120119
"file_handler": hasattr(module, "file_handler")
@@ -128,8 +127,8 @@ def get_tools(
128127
log.warning(
129128
f"Tool {function_name} already exists in another tools!"
130129
)
131-
log.warning(f"Collision between {tools} and {tool_id}.")
132-
log.warning(f"Discarding {tools}.{function_name}")
130+
log.warning(f"Collision between {tool} and {tool_id}.")
131+
log.warning(f"Discarding {tool}.{function_name}")
133132
else:
134133
tools_dict[function_name] = tool_dict
135134

@@ -239,8 +238,9 @@ def get_callable_attributes(tool: object) -> list[Callable]:
239238

240239

241240
def get_tools_specs(tool_class: object) -> list[dict]:
242-
function_list = get_callable_attributes(tool_class)
243-
function_model_list = map(function_to_pydantic_model, function_list)
241+
function_model_list = map(
242+
function_to_pydantic_model, get_callable_attributes(tool_class)
243+
)
244244
return [
245245
convert_to_openai_function(function_model)
246246
for function_model in function_model_list

0 commit comments

Comments
 (0)