answer/pkg/agent/tool.go
Steve Dudenhoeffer 090b28d956 Update LLM integration and add new agent tools and utilities
Refactored LLM handling to use updated langchaingo models and tools, replacing gollm dependencies. Introduced agent-related utilities, tools, and counters for better modular functionality. Added a parser for LLM model configuration and revamped the answering mechanism with enhanced support for tool-based interaction.
2025-02-25 22:56:32 -05:00

75 lines
1.5 KiB
Go

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
}