Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 39 additions & 20 deletions pkg/ollama/http_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,28 +373,19 @@ 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,
"messages": convertMessages(req.Messages),
"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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down