-
-
Notifications
You must be signed in to change notification settings - Fork 695
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding Kit interface in response to #1103
- Loading branch information
Amrit Singh
committed
Jan 29, 2025
1 parent
d7270c0
commit 050cc90
Showing
2 changed files
with
62 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,24 @@ | ||
package tools | ||
|
||
import "context" | ||
import ( | ||
"context" | ||
"fmt" | ||
) | ||
|
||
// 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 "", fmt.Errorf("invalid tool %v", toolName) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package tools | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
) | ||
|
||
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 TestTool(t *testing.T) { | ||
t.Parallel() | ||
t.Run("Tool Exists in Kit", func(t *testing.T) { | ||
t.Parallel() | ||
kit := Kit{ | ||
&SomeTool{}, | ||
} | ||
_, err := kit.UseTool(context.Background(), "An awesome tool", "test") | ||
if err != nil { | ||
t.Errorf("Error using tool: %v", err) | ||
} | ||
}) | ||
t.Run("Tool Does Not Exist in Kit", func(t *testing.T) { | ||
t.Parallel() | ||
kit := Kit{ | ||
&SomeTool{}, | ||
} | ||
_, err := kit.UseTool(context.Background(), "A tool that does not exist", "test") | ||
if err == nil { | ||
t.Errorf("Expected error, got nil") | ||
} | ||
}) | ||
} |