Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding the Kit interface [https://github.com/tmc/langchaingo/issues/1103] #1117

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion tools/tool.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
package tools

import "context"
import (
"context"
"errors"
)

const ErrInvalidTool = "invalid_tool"

// Tool is a tool for the llm agent to interact with different applications.
type Tool interface {
Name() string
Description() string
Call(ctx context.Context, input string) (string, error)
}

type Kit []Tool

func (tb *Kit) UseTool(ctx context.Context, toolName string, toolArgs string) (string, error) {
for _, tool := range *tb {
if tool.Name() == toolName {
return tool.Call(ctx, toolArgs)
}
}
return "", errors.New(ErrInvalidTool)
}
48 changes: 48 additions & 0 deletions tools/tool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package tools

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
)

type someTool struct{}

func (st *someTool) Name() string {
return "An awesome tool"
}

func (st *someTool) Description() string {
return "This tool is awesome"
}

func (st *someTool) Call(ctx context.Context, _ string) (string, error) {
if ctx.Err() != nil {
return "", ctx.Err()
}
return "test", nil
}

func TestToolWithTestify(t *testing.T) {
t.Parallel()
kit := Kit{
&someTool{},
}

// Test when the tool exists
t.Run("Tool Exists in Kit", func(t *testing.T) {
t.Parallel()
result, err := kit.UseTool(context.Background(), "An awesome tool", "test")
assert.NoError(t, err)
assert.Equal(t, "test", result)
})

// Test when the tool does not exist
t.Run("Tool Does Not Exist in Kit", func(t *testing.T) {
t.Parallel()
_, err := kit.UseTool(context.Background(), "A tool that does not exist", "test")
assert.Error(t, err)
assert.Equal(t, ErrInvalidTool, err.Error())
})
}