tools

command
v2.16.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 5, 2026 License: Apache-2.0 Imports: 5 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Tools Example

Function calling with the PromptKit SDK.

What You'll Learn

  • Defining tools in pack files
  • Registering tool handlers with OnTool()
  • Automatic JSON serialization
  • Multiple tool handlers with OnTools()

Prerequisites

  • Go 1.26+
  • An API key for one of: OpenAI, Anthropic, or Google (the packs do not pin a provider)

Running the Example

export OPENAI_API_KEY=your-key
go run .

Code Overview

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())

Pack File Structure

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"]
      }
    }
  }
}

Handler Signature

func(args map[string]any) (any, error)
  • args: Parameters from the LLM (matches tool schema)
  • return: Any JSON-serializable value, or error

Multiple Tools

Register all handlers at once:

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

Return Structs

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
})

Key Concepts

  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

Next Steps

Documentation

Overview

Package main demonstrates tool handling with the PromptKit SDK.

This example shows:

  • Defining tools in the pack file
  • Registering tool handlers with OnTool
  • Automatic JSON serialization of results

Run with:

export OPENAI_API_KEY=your-key
go run .

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL