Manage Variables
Learn how to use template variables with SetVar() and GetVar().
Set a Variable
Section titled “Set a Variable”conv.SetVar("user_name", "Alice")Get a Variable
Section titled “Get a Variable”name := conv.GetVar("user_name")fmt.Println(name) // "Alice"Set Multiple Variables
Section titled “Set Multiple Variables”conv.SetVars(map[string]any{ "user_name": "Alice", "role": "admin", "language": "en",})Variable Types
Section titled “Variable Types”Variables support any JSON-serializable type:
// Stringsconv.SetVar("name", "Alice")
// Numbersconv.SetVar("count", 42)conv.SetVar("score", 98.5)
// Booleansconv.SetVar("is_premium", true)
// Slicesconv.SetVar("tags", []string{"vip", "active"})
// Mapsconv.SetVar("metadata", map[string]any{ "region": "us-west", "tier": "gold",})Use in Templates
Section titled “Use in Templates”Variables are substituted in system prompts:
Pack file:
{ "prompts": { "support": { "system_template": "You are helping {{user_name}}. They are a {{role}} user." } }}Code:
conv.SetVar("user_name", "Alice")conv.SetVar("role", "premium")
// System prompt becomes:// "You are helping Alice. They are a premium user."Environment Variables
Section titled “Environment Variables”Load from environment:
import "os"
// Single variableconv.SetVar("api_key", os.Getenv("MY_API_KEY"))
// From environment with prefixconv.SetVarsFromEnv("MYAPP_")// MYAPP_USER_NAME -> "user_name"// MYAPP_API_KEY -> "api_key"Check if Set
Section titled “Check if Set”value := conv.GetVar("optional_var")if value == nil { // Variable not set conv.SetVar("optional_var", "default")}Thread Safety
Section titled “Thread Safety”Variables are thread-safe:
var wg sync.WaitGroup
for i := 0; i < 10; i++ { wg.Add(1) go func(n int) { defer wg.Done() conv.SetVar(fmt.Sprintf("var_%d", n), n) }(i)}
wg.Wait()Fork Isolation
Section titled “Fork Isolation”Forked conversations have isolated variables:
conv1, _ := sdk.Open("./pack.json", "chat")conv1.SetVar("user", "Alice")
// Fork creates a copyconv2 := conv1.Fork()conv2.SetVar("user", "Bob")
// Variables are isolatedfmt.Println(conv1.GetVar("user")) // "Alice"fmt.Println(conv2.GetVar("user")) // "Bob"Complete Example
Section titled “Complete Example”package main
import ( "context" "fmt" "log"
"github.com/AltairaLabs/PromptKit/sdk")
func main() { conv, _ := sdk.Open("./support.pack.json", "support") defer conv.Close()
// Set customer context conv.SetVars(map[string]any{ "customer_name": "Alice", "customer_tier": "gold", "language": "English", })
// Variables are used in system prompt ctx := context.Background() resp, _ := conv.Send(ctx, "I need help with my order")
fmt.Println(resp.Text())}