diff --git a/pkg/ollama/http_handler.go b/pkg/ollama/http_handler.go index 2d4b1620..35372399 100644 --- a/pkg/ollama/http_handler.go +++ b/pkg/ollama/http_handler.go @@ -373,13 +373,6 @@ func (h *HTTPHandler) handleChat(w http.ResponseWriter, r *http.Request) { return } - // Handle num_ctx option for context size configuration - if req.Options != nil { - if numCtxRaw, ok := req.Options["num_ctx"]; ok { - h.configure(r.Context(), numCtxRaw, sanitizedModelName, modelName, r.UserAgent()) - } - } - // Convert to OpenAI format chat completion request openAIReq := map[string]interface{}{ "model": modelName, @@ -387,14 +380,12 @@ func (h *HTTPHandler) handleChat(w http.ResponseWriter, r *http.Request) { "stream": req.Stream == nil || *req.Stream, } - // Add options if present if req.Options != nil { - if temp, ok := req.Options["temperature"]; ok { - openAIReq["temperature"] = temp - } - if maxTokens, ok := req.Options["num_predict"]; ok { - openAIReq["max_tokens"] = maxTokens + // Handle num_ctx option for context size configuration + if numCtxRaw, ok := req.Options["num_ctx"]; ok { + h.configure(r.Context(), numCtxRaw, sanitizedModelName, modelName, r.UserAgent()) } + h.mapOllamaOptionsToOpenAI(req.Options, openAIReq) } // Make request to scheduler @@ -465,14 +456,9 @@ func (h *HTTPHandler) handleGenerate(w http.ResponseWriter, r *http.Request) { "stream": req.Stream == nil || *req.Stream, } - // Add options if present + // Map Ollama options to OpenAI format if req.Options != nil { - if temp, ok := req.Options["temperature"]; ok { - openAIReq["temperature"] = temp - } - if maxTokens, ok := req.Options["num_predict"]; ok { - openAIReq["max_tokens"] = maxTokens - } + h.mapOllamaOptionsToOpenAI(req.Options, openAIReq) } // Make request to scheduler @@ -671,6 +657,39 @@ func (h *HTTPHandler) handlePull(w http.ResponseWriter, r *http.Request) { } } +// mapOllamaOptionsToOpenAI maps Ollama API options to OpenAI-compatible format +// This function handles all standard Ollama options and maps them to their OpenAI equivalents +func (h *HTTPHandler) mapOllamaOptionsToOpenAI(ollamaOpts map[string]interface{}, openAIReq map[string]interface{}) { + // Direct mappings - these options have exact OpenAI equivalents + if val, ok := ollamaOpts["temperature"]; ok { + openAIReq["temperature"] = val + } + if val, ok := ollamaOpts["top_p"]; ok { + openAIReq["top_p"] = val + } + if val, ok := ollamaOpts["top_k"]; ok { + openAIReq["top_k"] = val + } + if val, ok := ollamaOpts["num_predict"]; ok { + openAIReq["max_tokens"] = val + } + if val, ok := ollamaOpts["stop"]; ok { + openAIReq["stop"] = val + } + if val, ok := ollamaOpts["seed"]; ok { + openAIReq["seed"] = val + } + if val, ok := ollamaOpts["presence_penalty"]; ok { + openAIReq["presence_penalty"] = val + } + if val, ok := ollamaOpts["frequency_penalty"]; ok { + openAIReq["frequency_penalty"] = val + } + + // Note: num_ctx is handled separately in the configure() function + // as it requires a special ConfigureRunner call +} + // convertMessages converts Ollama messages to OpenAI format func convertMessages(messages []Message) []map[string]interface{} { result := make([]map[string]interface{}, len(messages))