Skip to content

Tools Example

Function calling with the PromptKit SDK.

  • Defining tools in pack files
  • Registering tool handlers with OnTool()
  • Automatic JSON serialization
  • Multiple tool handlers with OnTools()
  • Go 1.21+
  • OpenAI API key
Terminal window
export OPENAI_API_KEY=your-key
go run .
conv, err := sdk.Open("./tools.pack.json", "assistant")
if err != nil {
log.Fatal(err)
}
defer conv.Close()
// Register tool handlers
conv.OnTool("get_time", func(args map[string]any) (any, error) {
timezone := args["timezone"].(string)
return map[string]string{
"time": "14:30:00",
"timezone": timezone,
}, nil
})
conv.OnTool("get_weather", func(args map[string]any) (any, error) {
city := args["city"].(string)
return map[string]any{
"city": city,
"temperature": 22.5,
"conditions": "Sunny",
}, nil
})
// LLM will automatically call tools when needed
resp, _ := conv.Send(ctx, "What's the weather in London?")
fmt.Println(resp.Text())

Tools must be:

  1. Defined in the tools section
  2. Listed in the prompt’s tools array
{
"prompts": {
"assistant": {
"system_template": "You are a helpful assistant...",
"tools": ["get_weather", "get_time"]
}
},
"tools": {
"get_weather": {
"name": "get_weather",
"description": "Get current weather for a city",
"parameters": {
"type": "object",
"properties": {
"city": { "type": "string" },
"country": { "type": "string" }
},
"required": ["city", "country"]
}
}
}
}
func(args map[string]any) (any, error)
  • args: Parameters from the LLM (matches tool schema)
  • return: Any JSON-serializable value, or error

Register all handlers at once:

conv.OnTools(map[string]sdk.ToolHandler{
"get_time": timeHandler,
"get_weather": weatherHandler,
"search": searchHandler,
})

Return Go structs - they’re automatically serialized:

type Weather struct {
City string `json:"city"`
Temperature float64 `json:"temperature"`
}
conv.OnTool("get_weather", func(args map[string]any) (any, error) {
return Weather{City: "London", Temperature: 18.5}, nil
})
  1. Declarative Tools - Define in pack, implement in code
  2. Auto Serialization - Return any Go value
  3. Error Handling - Return errors to inform the LLM
  4. Tool Calls - LLM decides when to use tools