From cee08e316d7233046aed6bdf959a3c7381dc4cd5 Mon Sep 17 00:00:00 2001 From: ferret99gt Date: Wed, 19 Feb 2025 09:21:05 -0500 Subject: [PATCH 1/6] Update Ollama request option list with full supported options See https://github.com/ollama/ollama/blob/main/docs/api.md#request-8 This adds the full suite of supported Ollama options. --- backend/open_webui/utils/payload.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/backend/open_webui/utils/payload.py b/backend/open_webui/utils/payload.py index 5eb040434..78d193c7e 100644 --- a/backend/open_webui/utils/payload.py +++ b/backend/open_webui/utils/payload.py @@ -76,14 +76,26 @@ def apply_model_params_to_body_ollama(params: dict, form_data: dict) -> dict: "num_ctx", "num_batch", "num_keep", + "num_predict", "repeat_last_n", "tfs_z", "top_k", "min_p", + "typical_p", + "repeat_penalty", + "presence_penalty", + "frequency_penalty", + "penalize_newline", + "stop", + "numa", + "num_gpu", + "main_gpu", + "low_vram", + "vocab_only", "use_mmap", "use_mlock", "num_thread", - "num_gpu", + ] mappings = {i: lambda x: x for i in opts} form_data = apply_model_params_to_body(params, form_data, mappings) From 6b2ba7370112b20a7ce4abd50e75e85abe68305d Mon Sep 17 00:00:00 2001 From: ferret99gt Date: Wed, 19 Feb 2025 09:21:36 -0500 Subject: [PATCH 2/6] Remove tfs_z See https://github.com/ollama/ollama/blob/main/docs/api.md#request-8 tfs_z was removed from Llama.cpp and Ollama in January 2025. --- backend/open_webui/utils/payload.py | 1 - 1 file changed, 1 deletion(-) diff --git a/backend/open_webui/utils/payload.py b/backend/open_webui/utils/payload.py index 78d193c7e..75dd872c8 100644 --- a/backend/open_webui/utils/payload.py +++ b/backend/open_webui/utils/payload.py @@ -78,7 +78,6 @@ def apply_model_params_to_body_ollama(params: dict, form_data: dict) -> dict: "num_keep", "num_predict", "repeat_last_n", - "tfs_z", "top_k", "min_p", "typical_p", From 877d21a02977d0462a82a712cd9523824dd284c1 Mon Sep 17 00:00:00 2001 From: ferret99gt Date: Wed, 19 Feb 2025 09:23:33 -0500 Subject: [PATCH 3/6] Move parameter remapping above the opt dictionary. This is so that any remappings are handled before options are handled by apply_model_params_to_body --- backend/open_webui/utils/payload.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/backend/open_webui/utils/payload.py b/backend/open_webui/utils/payload.py index 75dd872c8..78d8378e1 100644 --- a/backend/open_webui/utils/payload.py +++ b/backend/open_webui/utils/payload.py @@ -66,6 +66,15 @@ def apply_model_params_to_body_openai(params: dict, form_data: dict) -> dict: def apply_model_params_to_body_ollama(params: dict, form_data: dict) -> dict: + name_differences = { + "max_tokens": "num_predict", + "frequency_penalty": "repeat_penalty", + } + + for key, value in name_differences.items(): + if (param := params.get(key, None)) is not None: + form_data[value] = param + opts = [ "temperature", "top_p", @@ -99,15 +108,6 @@ def apply_model_params_to_body_ollama(params: dict, form_data: dict) -> dict: mappings = {i: lambda x: x for i in opts} form_data = apply_model_params_to_body(params, form_data, mappings) - name_differences = { - "max_tokens": "num_predict", - "frequency_penalty": "repeat_penalty", - } - - for key, value in name_differences.items(): - if (param := params.get(key, None)) is not None: - form_data[value] = param - return form_data From a4249a63511a7ed8ed10872c59a5235dcadfa8dd Mon Sep 17 00:00:00 2001 From: ferret99gt Date: Wed, 19 Feb 2025 09:24:38 -0500 Subject: [PATCH 4/6] Remove remapping of frequency_penalty to repeat_penalty 1) Ollama natively supports frequency_penalty, so this is not necessary. 2) Repeat_penalty is being added to Open WebUI in PR #10016, allowing Ollama users to pick which penalty methods they want. --- backend/open_webui/utils/payload.py | 1 - 1 file changed, 1 deletion(-) diff --git a/backend/open_webui/utils/payload.py b/backend/open_webui/utils/payload.py index 78d8378e1..d2a55ce19 100644 --- a/backend/open_webui/utils/payload.py +++ b/backend/open_webui/utils/payload.py @@ -68,7 +68,6 @@ def apply_model_params_to_body_openai(params: dict, form_data: dict) -> dict: def apply_model_params_to_body_ollama(params: dict, form_data: dict) -> dict: name_differences = { "max_tokens": "num_predict", - "frequency_penalty": "repeat_penalty", } for key, value in name_differences.items(): From fa885c33465433bbdb31cd90390a43d6f3617630 Mon Sep 17 00:00:00 2001 From: ferret99gt Date: Wed, 19 Feb 2025 09:25:47 -0500 Subject: [PATCH 5/6] Update remapping logic We copy the params from from the original key to the new key, then delete it. This is to ensure Ollama only gets valid options. (Add a comment as well) --- backend/open_webui/utils/payload.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/backend/open_webui/utils/payload.py b/backend/open_webui/utils/payload.py index d2a55ce19..0628b8552 100644 --- a/backend/open_webui/utils/payload.py +++ b/backend/open_webui/utils/payload.py @@ -66,13 +66,16 @@ def apply_model_params_to_body_openai(params: dict, form_data: dict) -> dict: def apply_model_params_to_body_ollama(params: dict, form_data: dict) -> dict: + # Convert OpenAI parameter names to Ollama parameter names if needed. name_differences = { "max_tokens": "num_predict", } - + for key, value in name_differences.items(): if (param := params.get(key, None)) is not None: - form_data[value] = param + # Copy the parameter to new name then delete it, to prevent Ollama warning of invalid option provided + params[value] = params[key] + del params[key] opts = [ "temperature", From 5701d6d3337a7bf5d265f717936d36e1c39d0ee8 Mon Sep 17 00:00:00 2001 From: ferret99gt Date: Wed, 19 Feb 2025 09:30:16 -0500 Subject: [PATCH 6/6] Change the opt dictionary to a mappings dictionary with appropriate casts This is to bring consistency with apply_model_params_to_body_openai. Both now use a mapping dictionary then call and return apply_model_params_to_body directly. --- backend/open_webui/utils/payload.py | 66 ++++++++++++++--------------- 1 file changed, 32 insertions(+), 34 deletions(-) diff --git a/backend/open_webui/utils/payload.py b/backend/open_webui/utils/payload.py index 0628b8552..d37afd008 100644 --- a/backend/open_webui/utils/payload.py +++ b/backend/open_webui/utils/payload.py @@ -70,47 +70,45 @@ def apply_model_params_to_body_ollama(params: dict, form_data: dict) -> dict: name_differences = { "max_tokens": "num_predict", } - + for key, value in name_differences.items(): if (param := params.get(key, None)) is not None: # Copy the parameter to new name then delete it, to prevent Ollama warning of invalid option provided params[value] = params[key] del params[key] - opts = [ - "temperature", - "top_p", - "seed", - "mirostat", - "mirostat_eta", - "mirostat_tau", - "num_ctx", - "num_batch", - "num_keep", - "num_predict", - "repeat_last_n", - "top_k", - "min_p", - "typical_p", - "repeat_penalty", - "presence_penalty", - "frequency_penalty", - "penalize_newline", - "stop", - "numa", - "num_gpu", - "main_gpu", - "low_vram", - "vocab_only", - "use_mmap", - "use_mlock", - "num_thread", + # See https://github.com/ollama/ollama/blob/main/docs/api.md#request-8 + mappings = { + "temperature": float, + "top_p": float, + "seed": lambda x: x, + "mirostat": int, + "mirostat_eta": float, + "mirostat_tau": float, + "num_ctx": int, + "num_batch": int, + "num_keep": int, + "num_predict": int, + "repeat_last_n": int, + "top_k": int, + "min_p": float, + "typical_p": float, + "repeat_penalty": float, + "presence_penalty": float, + "frequency_penalty": float, + "penalize_newline": bool, + "stop": lambda x: [bytes(s, "utf-8").decode("unicode_escape") for s in x], + "numa": bool, + "num_gpu": int, + "main_gpu": int, + "low_vram": bool, + "vocab_only": bool, + "use_mmap": bool, + "use_mlock": bool, + "num_thread": int, + } - ] - mappings = {i: lambda x: x for i in opts} - form_data = apply_model_params_to_body(params, form_data, mappings) - - return form_data + return apply_model_params_to_body(params, form_data, mappings) def convert_messages_openai_to_ollama(messages: list[dict]) -> list[dict]: