From 23df351239681574e44c2fe97b89e9019e512fe8 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Tue, 4 Feb 2025 19:22:08 -0800 Subject: [PATCH] refac: ollama tool calling support Co-Authored-By: smonux <85928277+smonux@users.noreply.github.com> --- backend/open_webui/routers/ollama.py | 2 ++ backend/open_webui/utils/tools.py | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/backend/open_webui/routers/ollama.py b/backend/open_webui/routers/ollama.py index 780fc6f50..10367b020 100644 --- a/backend/open_webui/routers/ollama.py +++ b/backend/open_webui/routers/ollama.py @@ -939,6 +939,7 @@ async def generate_completion( class ChatMessage(BaseModel): role: str content: str + tool_calls: Optional[list[dict]] = None images: Optional[list[str]] = None @@ -950,6 +951,7 @@ class GenerateChatCompletionForm(BaseModel): template: Optional[str] = None stream: Optional[bool] = True keep_alive: Optional[Union[int, str]] = None + tools: Optional[list[dict]] = None async def get_ollama_url(request: Request, model: str, url_idx: Optional[int] = None): diff --git a/backend/open_webui/utils/tools.py b/backend/open_webui/utils/tools.py index b6e13011d..d83772acd 100644 --- a/backend/open_webui/utils/tools.py +++ b/backend/open_webui/utils/tools.py @@ -73,6 +73,13 @@ def get_tools( # convert to function that takes only model params and inserts custom params original_func = getattr(module, function_name) callable = apply_extra_params_to_tool_function(original_func, extra_params) + + if callable.__doc__ and callable.__doc__.strip() != "": + s = re.split(":(param|return)", callable.__doc__, 1) + spec["description"] = s[0] + else: + spec["description"] = function_name + # TODO: This needs to be a pydantic model tool_dict = { "toolkit_id": tool_id,