Documentation
¶
Overview ¶
Package tool provides a framework for defining and managing tools that extend agent capabilities in the Bubo system. Tools are functions that agents can invoke, with automatic parameter validation and documentation generation through reflection.
Design Decisions ¶
- Reflection-based: Uses Go's reflection to analyze function signatures
- Schema Generation: Automatically generates JSON schemas for function parameters
- Type Safety: Ensures type safety through compile-time checks
- Functional Options: Provides a flexible configuration system
- Documentation: Integrates documentation with function definitions
Key Concepts ¶
Tool Definition A tool is defined by its function signature and metadata: - Name: Identifier for the tool - Description: Human-readable explanation - Parameters: Named input parameters - Function: The actual implementation
Parameter Schema Tools automatically generate JSON schemas for their parameters: - Type validation - Required fields - Parameter descriptions - Custom validation rules
Configuration Options Tools can be configured using functional options: - Name customization - Description setting - Parameter naming - Validation rules
Usage Examples ¶
Basic Tool Definition:
// Define a tool function
func calculateSum(x, y int) int {
return x + y
}
// Create tool definition
tool := Must(calculateSum,
Name("calculateSum"),
Description("Calculates the sum of two numbers"),
Parameters("firstNumber", "secondNumber"),
)
Tool with Context Variables:
func processText(ctx types.ContextVars, text string) string {
// Access context variables
userID := ctx.Get("user_id")
return fmt.Sprintf("Processing text for user %s: %s", userID, text)
}
tool := Must(processText,
Name("processText"),
Description("Processes text with user context"),
Parameters("inputText"),
)
Generated Tool:
// bubo:agentTool
func transferToAgent() api.Agent {
return someAgent
}
// Generated code will create a tool definition automatically
Best Practices ¶
1. Function Design
- Keep functions focused and single-purpose
- Use meaningful parameter names
- Include clear error handling
- Document expected behavior
2. Tool Configuration
- Provide descriptive names
- Write clear descriptions
- Use meaningful parameter names
- Include validation where appropriate
3. Error Handling
- Use Must() only when errors are not expected
- Handle errors explicitly in production code
- Validate input parameters
- Return meaningful error messages
4. Documentation
- Document tool purpose and usage
- Explain parameter requirements
- Provide usage examples
- Note any side effects
Integration ¶
Tools integrate with several Bubo components:
Agents Tools are provided to agents as capabilities:
agent := agent.New( agent.Tools(tool1, tool2), // other configuration... )
Providers Providers use tool definitions to enable function calling:
provider.ChatCompletion(ctx, CompletionParams{ Tools: []tool.Definition{tool1, tool2}, // other parameters... })
Code Generation The bubo-tool-gen command generates tool definitions from comments:
// bubo:agentTool func myTool() {}
Thread Safety ¶
Tools should be designed with concurrency in mind:
- Make tools stateless when possible
- Use appropriate synchronization for stateful tools
- Consider context cancellation
- Handle concurrent invocations safely
For more details about specific components, see:
- Definition: Core type representing a tool
- Must: Helper for creating tools without error handling
- New: Main constructor for creating tools
- Options: Configuration options for customizing tools
Index ¶
Constants ¶
This section is empty.
Variables ¶
var Description = opts.ForName[Definition, string]("Description")
Description returns a function that sets the description of an agent function. It takes a string parameter 'description' and returns a function that modifies the 'Description' field of the provided 'agentFunctionOptions' struct.
var Name = opts.ForName[Definition, string]("Name")
Name returns a function that sets the Name field of agentFunctionOptions to the provided name. This can be used to configure an agent function with a specific name.
Parameters:
- name: A string representing the name to be assigned.
Returns:
- A function that takes a pointer to agentFunctionOptions and sets its Name field.
Functions ¶
func Parameters ¶
func Parameters(parameters ...string) opts.Option[Definition]
Parameters returns a function that sets the Parameters field of agentFunctionOptions to a map where each parameter is assigned a key in the format "paramN", where N is the index of the parameter in the input slice.
Parameters:
parameters - a variadic string slice containing the parameters to be set.
Returns:
A function that takes a pointer to agentFunctionOptions and sets its Parameters field.
Types ¶
type Definition ¶
Definition represents the definition of an agent function. It includes the function's name, description, parameters, and the function itself.
func Must ¶
func Must(f any, options ...Option) Definition
Must wraps the AgentTool call and ensures that any error returned by AgentTool is handled by panicking. It takes a function `f` and a variadic number of AgentToolOption `options` as arguments, and returns an AgentToolDefinition. If AgentTool returns an error, Must will panic.
Parameters:
- f: The function to be wrapped.
- options: A variadic number of options to configure the agent tool.
Returns:
- ToolDefinition: The definition of the agent tool.
func New ¶
func New(f any, options ...Option) (Definition, error)
New creates an AgentToolDefinition from the provided function and options. The function is assigned to the Function field of the resulting AgentToolDefinition.
Parameters:
- f: The function to be assigned to the AgentToolDefinition.
- options: A variadic list of AgentToolOption to configure the AgentToolDefinition.
Returns:
A ToolDefinition with the provided function and applied options.
func (Definition) ToNameAndSchema ¶
func (td Definition) ToNameAndSchema() (string, *jsonschema.Schema)
type Option ¶
type Option = opts.Option[Definition]
Option is a type alias for a function that modifies the configuration options of an agent tool. It allows for flexible and customizable configuration of agent tools by applying various options.