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.
This commit is contained in:
ferret99gt 2025-02-19 09:30:16 -05:00
parent fa885c3346
commit 5701d6d333

View File

@ -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 = [ # See https://github.com/ollama/ollama/blob/main/docs/api.md#request-8
"temperature", mappings = {
"top_p", "temperature": float,
"seed", "top_p": float,
"mirostat", "seed": lambda x: x,
"mirostat_eta", "mirostat": int,
"mirostat_tau", "mirostat_eta": float,
"num_ctx", "mirostat_tau": float,
"num_batch", "num_ctx": int,
"num_keep", "num_batch": int,
"num_predict", "num_keep": int,
"repeat_last_n", "num_predict": int,
"top_k", "repeat_last_n": int,
"min_p", "top_k": int,
"typical_p", "min_p": float,
"repeat_penalty", "typical_p": float,
"presence_penalty", "repeat_penalty": float,
"frequency_penalty", "presence_penalty": float,
"penalize_newline", "frequency_penalty": float,
"stop", "penalize_newline": bool,
"numa", "stop": lambda x: [bytes(s, "utf-8").decode("unicode_escape") for s in x],
"num_gpu", "numa": bool,
"main_gpu", "num_gpu": int,
"low_vram", "main_gpu": int,
"vocab_only", "low_vram": bool,
"use_mmap", "vocab_only": bool,
"use_mlock", "use_mmap": bool,
"num_thread", "use_mlock": bool,
"num_thread": int,
}
] return apply_model_params_to_body(params, form_data, mappings)
mappings = {i: lambda x: x for i in opts}
form_data = apply_model_params_to_body(params, form_data, mappings)
return form_data
def convert_messages_openai_to_ollama(messages: list[dict]) -> list[dict]: def convert_messages_openai_to_ollama(messages: list[dict]) -> list[dict]: