package agent import ( "reflect" "github.com/tmc/langchaingo/llms" ) type Tool struct { Name string Description string Function function } func (t *Tool) Tool() llms.Tool { return llms.Tool{ Type: "function", Function: t.Definition(), } } func (t *Tool) Definition() *llms.FunctionDefinition { var properties = map[string]any{} for name, arg := range t.Function.args { properties[name] = arg.Schema() } var res = llms.FunctionDefinition{ Name: t.Name, Description: t.Description, Parameters: map[string]any{"type": "object", "properties": properties}, } return &res } // Execute executes the tool with the given context and arguments. // Returns the result of the execution and an error if the operation fails. // The arguments must be a JSON-encoded string that represents the struct to be passed to the function. func (t *Tool) Execute(ctx *Context, args string) (FuncResponse, error) { return t.Function.Execute(ctx, args) } func FromFunction[T any](fn func(*Context, T) (FuncResponse, error)) *Tool { f, err := analyzeFunction(fn) if err != nil { panic(err) } return &Tool{ Name: reflect.TypeOf(fn).Name(), Description: "This is a tool", Function: f, } } func (t *Tool) WithName(name string) *Tool { t.Name = name return t } func (t *Tool) WithDescription(description string) *Tool { t.Description = description return t } func (t *Tool) WithFunction(fn any) *Tool { f, err := analyzeFuncFromReflect(reflect.ValueOf(fn)) if err != nil { panic(err) } t.Function = f return t }