tools

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package tools provides the tool interface and utilities for creating tools that can be used by LangChain agents.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExecuteToolCall

func ExecuteToolCall(ctx context.Context, toolCall core.ToolCall, availableTools []Tool) (string, error)

ExecuteToolCall executes a tool call from an AI message, looking up the tool by name.

func ExecuteToolCalls

func ExecuteToolCalls(ctx context.Context, toolCalls []core.ToolCall, availableTools []Tool) ([]core.Message, error)

ExecuteToolCalls executes all tool calls from an AI message.

func ParseToolCallArgs

func ParseToolCallArgs(tc core.ToolCall, v any) error

ParseToolCallArgs parses the JSON args of a tool call into the given struct.

func ToDefinition

func ToDefinition(t Tool) llms.ToolDefinition

ToDefinition converts a Tool to an llms.ToolDefinition for model binding.

func ToDefinitions

func ToDefinitions(tools ...Tool) []llms.ToolDefinition

ToDefinitions converts multiple tools to ToolDefinitions.

Types

type RunnableTool

type RunnableTool struct {
	// contains filtered or unexported fields
}

RunnableTool wraps a Tool as a core.Runnable[string, string].

func NewRunnableTool

func NewRunnableTool(t Tool) *RunnableTool

NewRunnableTool wraps a Tool as a Runnable.

func (*RunnableTool) Batch

func (r *RunnableTool) Batch(ctx context.Context, inputs []string, opts ...core.Option) ([]string, error)

Batch runs the tool for multiple inputs.

func (*RunnableTool) GetName

func (r *RunnableTool) GetName() string

GetName returns the tool name.

func (*RunnableTool) Invoke

func (r *RunnableTool) Invoke(ctx context.Context, input string, opts ...core.Option) (string, error)

Invoke runs the tool.

func (*RunnableTool) Stream

func (r *RunnableTool) Stream(ctx context.Context, input string, opts ...core.Option) (*core.StreamIterator[string], error)

Stream returns a single-chunk stream.

type StructuredTool

type StructuredTool struct {
	// contains filtered or unexported fields
}

StructuredTool is a tool created from a Go function with typed arguments.

func NewTool

func NewTool(name, description string, fn func(ctx context.Context, input string) (string, error)) *StructuredTool

NewTool creates a StructuredTool from a name, description, and function. The function receives the raw JSON string input and returns a string result.

func NewTypedTool

func NewTypedTool[T any](name, description string, argsExample T, fn func(ctx context.Context, args T) (string, error)) *StructuredTool

NewTypedTool creates a StructuredTool with typed input. The argsType should be a struct with json tags that defines the input schema. The function receives the parsed struct as JSON and returns a string result.

Example:

type SearchArgs struct {
    Query string `json:"query" description:"The search query"`
}
tool := NewTypedTool("search", "Search the web", SearchArgs{},
    func(ctx context.Context, args SearchArgs) (string, error) {
        return "results for: " + args.Query, nil
    },
)

func (*StructuredTool) ArgsSchema

func (t *StructuredTool) ArgsSchema() map[string]any

ArgsSchema returns the JSON Schema for the tool's parameters.

func (*StructuredTool) Description

func (t *StructuredTool) Description() string

Description returns the tool description.

func (*StructuredTool) Name

func (t *StructuredTool) Name() string

Name returns the tool name.

func (*StructuredTool) Run

func (t *StructuredTool) Run(ctx context.Context, input string) (string, error)

Run executes the tool.

type Tool

type Tool interface {
	// Name returns the unique name of the tool.
	Name() string

	// Description returns a description of what the tool does.
	// This is used by the model to decide when to use the tool.
	Description() string

	// ArgsSchema returns a JSON Schema describing the tool's parameters.
	ArgsSchema() map[string]any

	// Run executes the tool with the given input string or JSON.
	Run(ctx context.Context, input string) (string, error)
}

Tool is the interface that all tools must implement. Tools are functions that agents can call to interact with the world.

Jump to

Keyboard shortcuts

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