From 93248f609e5c7d3b1c310bc4aad3f434af3a0be7 Mon Sep 17 00:00:00 2001 From: Saiska Date: Fri, 29 May 2026 15:01:12 +0200 Subject: [PATCH] Expose top_k / min_p / presence_penalty / frequency_penalty sampling options Four Ollama sampling params via config: OllamaChat.TopK (uint32_t), OllamaChat.MinP, OllamaChat.PresencePenalty, OllamaChat.FrequencyPenalty (all float). Each defaults to 0 / 0.0 = unset (not sent to Ollama), so behavior is unchanged unless an operator sets a non-zero value. Follows the existing conditional-options pattern used for Temperature, TopP, and RepeatPenalty. Co-Authored-By: Claude Opus 4.8 --- README.md | 17 +++++++++++++++++ conf/mod_ollama_chat.conf.dist | 28 ++++++++++++++++++++++++++++ src/mod-ollama-chat_api.cpp | 16 ++++++++++++++++ src/mod-ollama-chat_config.cpp | 8 ++++++++ src/mod-ollama-chat_config.h | 5 +++++ 5 files changed, 74 insertions(+) diff --git a/README.md b/README.md index 6ac8014cc..8a146809c 100644 --- a/README.md +++ b/README.md @@ -194,6 +194,23 @@ This should return a JSON response listing available models. If you get a connec > For a complete list of all available configuration options with comments and defaults, see `mod-ollama-chat.conf.dist` included in this repository. +### Key Inference / Sampling Parameters + +These options control how the Ollama model generates text. All are set in `mod-ollama-chat.conf`: + +| Option | Default | Description | +|---|---|---| +| `OllamaChat.NumPredict` | `40` | Max tokens to generate. `0` = unlimited. | +| `OllamaChat.Temperature` | `0.8` | Output randomness. Lower = more focused; higher = more creative. | +| `OllamaChat.TopP` | `0.95` | Nucleus sampling threshold. | +| `OllamaChat.RepeatPenalty` | `1.1` | Penalizes repeated tokens. `1.0` = no penalty. | +| `OllamaChat.TopK` | `0` | Limits sampling to the top K tokens. `0` = unset (model default, typically 40). | +| `OllamaChat.MinP` | `0.0` | Min-p sampling: keeps tokens with prob >= MinP × top-token prob. `0.0` = unset (model default). Alternative to TopP. | +| `OllamaChat.PresencePenalty` | `0.0` | Penalizes tokens that have already appeared, encouraging new topics. `0.0` = unset (model default / no penalty). | +| `OllamaChat.FrequencyPenalty` | `0.0` | Penalizes tokens proportionally to how often they have appeared, reducing repetition. `0.0` = unset (model default / no penalty). | + +These four parameters (`TopK`, `MinP`, `PresencePenalty`, `FrequencyPenalty`) default to `0` / `0.0`, which means they are **not sent** to Ollama and the model uses its own defaults. Behavior is unchanged unless an operator explicitly sets a non-zero value. + ## Text Commands The module provides several in-game text commands for administrators (Game Masters) to manage and monitor the Ollama chat functionality. All commands require **SEC_ADMINISTRATOR** security level (GM level 3 or higher). diff --git a/conf/mod_ollama_chat.conf.dist b/conf/mod_ollama_chat.conf.dist index b6000322e..173437477 100644 --- a/conf/mod_ollama_chat.conf.dist +++ b/conf/mod_ollama_chat.conf.dist @@ -84,6 +84,34 @@ OllamaChat.TopP = 0.95 # Default: 1.1 OllamaChat.RepeatPenalty = 1.1 +# OllamaChat.TopK +# Description: Limits sampling to the top K most likely tokens. Higher = more diverse. +# 0 = unset (uses the model default, typically 40). Typical: 40. +# Example: OllamaChat.TopK = 40 +# Default: 0 +OllamaChat.TopK = 0 + +# OllamaChat.MinP +# Description: Min-p sampling: keep tokens with probability >= MinP * (top token prob). +# An alternative to TopP. 0.0 = unset (model default). Typical: 0.05. +# Example: OllamaChat.MinP = 0.05 +# Default: 0.0 +OllamaChat.MinP = 0.0 + +# OllamaChat.PresencePenalty +# Description: Penalizes tokens that have already appeared, encouraging new topics. +# 0.0 = unset (model default / no penalty). Typical: 0.1-0.5. +# Example: OllamaChat.PresencePenalty = 0.2 +# Default: 0.0 +OllamaChat.PresencePenalty = 0.0 + +# OllamaChat.FrequencyPenalty +# Description: Penalizes tokens proportionally to how often they have appeared, +# reducing repetition. 0.0 = unset (model default / no penalty). Typical: 0.1-0.5. +# Example: OllamaChat.FrequencyPenalty = 0.2 +# Default: 0.0 +OllamaChat.FrequencyPenalty = 0.0 + # OllamaChat.NumCtx # Description: Maximum context length (in tokens) sent to the model. # 0 = model default. Use a value only if you want to restrict or expand context. diff --git a/src/mod-ollama-chat_api.cpp b/src/mod-ollama-chat_api.cpp index 271077d9d..661c3a7d5 100644 --- a/src/mod-ollama-chat_api.cpp +++ b/src/mod-ollama-chat_api.cpp @@ -70,6 +70,22 @@ std::string QueryOllamaAPI(const std::string& prompt) options["repeat_penalty"] = g_OllamaRepeatPenalty; hasOptions = true; } + if (g_OllamaTopK > 0) { + options["top_k"] = g_OllamaTopK; + hasOptions = true; + } + if (g_OllamaMinP != 0.0f) { + options["min_p"] = g_OllamaMinP; + hasOptions = true; + } + if (g_OllamaPresencePenalty != 0.0f) { + options["presence_penalty"] = g_OllamaPresencePenalty; + hasOptions = true; + } + if (g_OllamaFrequencyPenalty != 0.0f) { + options["frequency_penalty"] = g_OllamaFrequencyPenalty; + hasOptions = true; + } if (g_OllamaNumCtx > 0) { options["num_ctx"] = g_OllamaNumCtx; hasOptions = true; diff --git a/src/mod-ollama-chat_config.cpp b/src/mod-ollama-chat_config.cpp index 8bc1b1da3..0f4165f5e 100644 --- a/src/mod-ollama-chat_config.cpp +++ b/src/mod-ollama-chat_config.cpp @@ -46,6 +46,10 @@ uint32_t g_OllamaNumPredict = 40; float g_OllamaTemperature = 0.8f; float g_OllamaTopP = 0.95f; float g_OllamaRepeatPenalty = 1.1f; +uint32_t g_OllamaTopK = 0; +float g_OllamaMinP = 0.0f; +float g_OllamaPresencePenalty = 0.0f; +float g_OllamaFrequencyPenalty = 0.0f; uint32_t g_OllamaNumCtx = 0; uint32_t g_OllamaNumThreads = 0; std::string g_OllamaStop = ""; @@ -390,6 +394,10 @@ void LoadOllamaChatConfig() g_OllamaTemperature = sConfigMgr->GetOption("OllamaChat.Temperature", 0.8f); g_OllamaTopP = sConfigMgr->GetOption("OllamaChat.TopP", 0.95f); g_OllamaRepeatPenalty = sConfigMgr->GetOption("OllamaChat.RepeatPenalty", 1.1f); + g_OllamaTopK = sConfigMgr->GetOption("OllamaChat.TopK", 0); + g_OllamaMinP = sConfigMgr->GetOption("OllamaChat.MinP", 0.0f); + g_OllamaPresencePenalty = sConfigMgr->GetOption("OllamaChat.PresencePenalty", 0.0f); + g_OllamaFrequencyPenalty = sConfigMgr->GetOption("OllamaChat.FrequencyPenalty", 0.0f); g_OllamaNumCtx = sConfigMgr->GetOption("OllamaChat.NumCtx", 0); g_OllamaNumThreads = sConfigMgr->GetOption("OllamaChat.NumThreads", 0); g_OllamaStop = sConfigMgr->GetOption("OllamaChat.Stop", ""); diff --git a/src/mod-ollama-chat_config.h b/src/mod-ollama-chat_config.h index 37e66e26e..9a0c67735 100644 --- a/src/mod-ollama-chat_config.h +++ b/src/mod-ollama-chat_config.h @@ -47,6 +47,11 @@ extern uint32_t g_OllamaNumPredict; extern float g_OllamaTemperature; extern float g_OllamaTopP; extern float g_OllamaRepeatPenalty; +// additional Ollama sampling parameters (0 / 0.0 = unset, not sent -> model default) +extern uint32_t g_OllamaTopK; +extern float g_OllamaMinP; +extern float g_OllamaPresencePenalty; +extern float g_OllamaFrequencyPenalty; extern uint32_t g_OllamaNumCtx; extern uint32_t g_OllamaNumThreads; extern std::string g_OllamaStop;