浏览代码

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.
ferret99gt 2 月之前
父节点
当前提交
5701d6d333
共有 1 个文件被更改,包括 32 次插入34 次删除
  1. 32 34
      backend/open_webui/utils/payload.py

+ 32 - 34
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 = {
     name_differences = {
         "max_tokens": "num_predict",
         "max_tokens": "num_predict",
     }
     }
-    
+
     for key, value in name_differences.items():
     for key, value in name_differences.items():
         if (param := params.get(key, None)) is not None:
         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
             # Copy the parameter to new name then delete it, to prevent Ollama warning of invalid option provided
             params[value] = params[key]
             params[value] = params[key]
             del 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",
-
-    ]
-    mappings = {i: lambda x: x for i in opts}
-    form_data = apply_model_params_to_body(params, form_data, mappings)
+    # 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,
+    }
 
 
-    return form_data
+    return apply_model_params_to_body(params, form_data, mappings)
 
 
 
 
 def convert_messages_openai_to_ollama(messages: list[dict]) -> list[dict]:
 def convert_messages_openai_to_ollama(messages: list[dict]) -> list[dict]: