Documentation
¶
Index ¶
- Variables
- type Choice
- type ChoiceMode
- type FunctionSchema
- type Grammar
- type JSONGrammar
- type LarkGrammar
- type RegexGrammar
- type Result
- func Error(err error) Result
- func ErrorWithLabel(label string, err error) Result
- func Errorf(format string, args ...any) Result
- func Success(value any) Result
- func SuccessFromString(output string) Result
- func SuccessFromStringWithLabel(label string, output string) Result
- func SuccessWithContent(label string, content content.Content) Result
- func SuccessWithLabel(label string, value any) Result
- func Successf(format string, args ...any) Result
- type Runner
- type TextGrammar
- type Tool
- type Toolbox
- type ValueSchema
Constants ¶
This section is empty.
Variables ¶
var ( // NopRunner is a runner that does nothing extra. NopRunner = NewRunner(context.Background(), nil, func(status string) {}) )
Functions ¶
This section is empty.
Types ¶
type Choice ¶
type Choice struct {
// Mode controls how the model may use tools.
// Semantics:
// - Any: The model may choose any available tool (or none).
// - AllowOnly: The model may only choose from AllowedTools. If the list is empty, no tools may be used.
// - RequireOneOf: The model must choose one of AllowedTools. If the list is empty, this effectively forbids tool use.
Mode ChoiceMode `json:"mode"`
// AllowedTools are tool function names that are permitted/required depending on Mode.
AllowedTools []string `json:"allowed_tools,omitempty"`
}
func AllowOnly ¶
AllowOnly configures a whitelist of allowed tools; empty means no tools can be used.
func AnyTool ¶
func AnyTool() Choice
AnyTool configures tool choice as fully open (model may pick any tool or none).
func RequireOneOf ¶
RequireOneOf configures a set of tools from which the model must pick one.
type ChoiceMode ¶
type ChoiceMode string
ChoiceMode enumerates tool choice policies.
const ( // ChoiceAny lets the model decide whether to call tools and which one. ChoiceAny ChoiceMode = "any" // ChoiceAllowOnly restricts tool use to a subset (or none if empty). ChoiceAllowOnly ChoiceMode = "allow_only" // ChoiceRequireOneOf forces the model to use one of the provided tool names. ChoiceRequireOneOf ChoiceMode = "require_one_of" )
func (*ChoiceMode) UnmarshalJSON ¶
func (m *ChoiceMode) UnmarshalJSON(data []byte) error
UnmarshalJSON parses string values for ChoiceMode.
type FunctionSchema ¶
type FunctionSchema struct {
// Name is the name of the function to be called.
Name string `json:"name"`
// Description is a description of what the function does.
Description string `json:"description"`
// Parameters is the schema for the arguments object that the function expects.
Parameters ValueSchema `json:"parameters"`
}
FunctionSchema defines the structure for a function tool that an LLM can call. It aligns with the format expected by many LLM providers for function calling. Based on a subset of the JSON Schema specification.
type Grammar ¶
type Grammar interface {
// contains filtered or unexported methods
}
Grammar specifies input format for grammar-based tools. Providers that support custom tools can expose these to the model.
type JSONGrammar ¶
type JSONGrammar interface {
Grammar
Schema() *FunctionSchema
// SkipValidation returns true when the tool should bypass JSON validation
// (e.g., External tools that accept arbitrary json.RawMessage parameters).
SkipValidation() bool
}
JSONGrammar implements Grammar for JSON-schema-based tools so that JSON tools follow the same grammar pathway as other grammars while preserving the existing public API behavior.
func NewJSONGrammar ¶
func NewJSONGrammar(name, description string, schemaType reflect.Type) JSONGrammar
NewJSONGrammar constructs a JSON grammar that will lazily generate a schema using reflection over the provided params type.
func NewJSONGrammarWithSchema ¶
func NewJSONGrammarWithSchema(schema *FunctionSchema, skipValidation bool) JSONGrammar
NewJSONGrammarWithSchema constructs a JSON grammar around an explicit schema.
type LarkGrammar ¶
type LarkGrammar struct{ Definition string }
LarkGrammar enforces a Lark grammar definition.
type RegexGrammar ¶
type RegexGrammar struct{ Definition string }
RegexGrammar enforces a regex grammar definition.
type Result ¶
type Result interface {
// Label returns a short single line description of the entire tool run.
Label() string
// Content returns the structured content of the result.
Content() content.Content
// Error returns the error that occurred during the tool run, if any.
Error() error
}
Result defines the outcome of a tool execution.
func ErrorWithLabel ¶
func Success ¶
Success creates a result by marshaling the value to JSON content. It attempts to generate a label automatically from the value if it implements fmt.Stringer.
func SuccessFromString ¶
SuccessFromString creates a result containing the string wrapped in {"output": ...} JSON content. It generates a label automatically from the string content.
func SuccessFromStringWithLabel ¶
SuccessFromStringWithLabel creates a result with an explicit label, containing the string wrapped in {"output": ...} JSON content.
func SuccessWithContent ¶
SuccessWithContent creates a result with an explicit label and pre-constructed content. This is the escape hatch for advanced use cases, like including images.
func SuccessWithLabel ¶
SuccessWithLabel creates a result with an explicit label by marshaling the value to JSON content.
type Tool ¶
type Tool interface {
// Label returns a nice human readable title for the tool.
Label() string
// Description returns the description of the tool.
Description() string
// FuncName returns the function name for the tool.
FuncName() string
// Run runs the tool with the provided parameters.
Run(r Runner, params json.RawMessage) Result
// Grammar returns the grammar configuration for this tool.
Grammar() Grammar
}
func External ¶
func External(label string, schema *FunctionSchema, fn func(r Runner, params json.RawMessage) Result) Tool
External returns a tool where the schema is provided explicitly, and the handler function receives raw JSON parameters. This is suitable for external tools where schema generation via reflection is not possible or desired.
func Func ¶
func Func[Params any](label, description, funcName string, fn func(r Runner, params Params) Result) Tool
Func returns a tool for a function implementation with the given name and description.
func FuncGrammar ¶
func FuncGrammar(grammar Grammar, label, description, funcName string, fn func(r Runner, input string) Result) Tool
FuncGrammar registers a grammar-based tool that receives a single string input. The input is derived from the provider's streaming arguments. If the raw parameters are a JSON string, it's unmarshaled; otherwise, the raw bytes are interpreted as a plain string.
type Toolbox ¶
type Toolbox struct {
// Choice controls tool selection policy for providers.
Choice Choice
// contains filtered or unexported fields
}
type ValueSchema ¶
type ValueSchema struct {
// Type specifies the data type of the value (e.g., "string", "integer", "object", "array", "boolean", "number").
Type string `json:"type,omitempty"`
// Description provides a brief explanation of the value or field.
Description string `json:"description,omitempty"`
// Items defines the schema for elements within an array. Only used when Type is "array".
Items *ValueSchema `json:"items,omitempty"`
// Properties defines the schema for properties within an object. Only used when Type is "object".
// Note: We use an ordered map to preserve insertion order from callers (e.g., TS clients).
Properties *jsonmap.Map `json:"properties,omitempty"`
// AdditionalProperties specifies the schema for additional properties in an object, or allows/disallows them.
// It can be a boolean (true to allow any, false to disallow) or a ValueSchema defining the type of allowed additional properties.
// Only used when Type is "object".
AdditionalProperties any `json:"additionalProperties,omitempty"`
// Required lists the names of properties that must be present when Type is "object".
Required []string `json:"required,omitempty"`
// AnyOf specifies that the value must conform to at least one of the provided schemas.
AnyOf []ValueSchema `json:"anyOf,omitempty"`
}
ValueSchema represents a schema for a value within the function's parameters. It corresponds to a subset of the JSON Schema specification, defining the type and constraints of a parameter field or the parameter object itself.
func (*ValueSchema) UnmarshalJSON ¶
func (v *ValueSchema) UnmarshalJSON(data []byte) error
UnmarshalJSON ensures map-like schema fields preserve insertion order via jsonmap.