From 2cb2a76200be0103938b8b5b7ac58f0f2a989fe0 Mon Sep 17 00:00:00 2001 From: Kayvan Sylvan Date: Sat, 17 Jan 2026 06:35:41 -0800 Subject: [PATCH] feat: add support for pattern variables in Ollama API requests ## CHANGES - Add `Variables` field to `OllamaRequestBody` struct for direct variable passing - Change `Options` field from empty struct to flexible `map[string]any` type - Extract variables from top-level `Variables` field or nested `Options.variables` - Support parsing variables as JSON string or map format - Pass extracted variables to `PromptRequest` for single message chats - Pass extracted variables to `PromptRequest` for multi-message chats - Add `omitempty` JSON tags to optional fields --- internal/server/ollama.go | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/internal/server/ollama.go b/internal/server/ollama.go index f62f1e5e..e85eb608 100644 --- a/internal/server/ollama.go +++ b/internal/server/ollama.go @@ -44,11 +44,11 @@ type APIConvert struct { } type OllamaRequestBody struct { - Messages []OllamaMessage `json:"messages"` - Model string `json:"model"` - Options struct { - } `json:"options"` - Stream bool `json:"stream"` + Messages []OllamaMessage `json:"messages"` + Model string `json:"model"` + Options map[string]any `json:"options,omitempty"` + Stream bool `json:"stream"` + Variables map[string]string `json:"variables,omitempty"` // Fabric-specific: pattern variables (direct) } type OllamaMessage struct { @@ -164,6 +164,29 @@ func (f APIConvert) ollamaChat(c *gin.Context) { now := time.Now() var chat ChatRequest + // Extract variables from either top-level Variables field or Options.variables + variables := prompt.Variables + if variables == nil && prompt.Options != nil { + if optVars, ok := prompt.Options["variables"]; ok { + // Options.variables can be either a JSON string or a map + switch v := optVars.(type) { + case string: + // Parse JSON string into map + if err := json.Unmarshal([]byte(v), &variables); err != nil { + log.Printf("Warning: failed to parse options.variables as JSON: %v", err) + } + case map[string]any: + // Convert map[string]any to map[string]string + variables = make(map[string]string) + for k, val := range v { + if s, ok := val.(string); ok { + variables[k] = s + } + } + } + } + } + if len(prompt.Messages) == 1 { chat.Prompts = []PromptRequest{{ UserInput: prompt.Messages[0].Content, @@ -171,6 +194,7 @@ func (f APIConvert) ollamaChat(c *gin.Context) { Model: "", ContextName: "", PatternName: strings.Split(prompt.Model, ":")[0], + Variables: variables, }} } else if len(prompt.Messages) > 1 { var content string @@ -183,6 +207,7 @@ func (f APIConvert) ollamaChat(c *gin.Context) { Model: "", ContextName: "", PatternName: strings.Split(prompt.Model, ":")[0], + Variables: variables, }} } fabricChatReq, err := json.Marshal(chat)