Skip to content

feat: add "tools: foo as bar" syntax #360

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

Merged
merged 1 commit into from
May 15, 2024
Merged
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
7 changes: 7 additions & 0 deletions pkg/tests/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,13 @@ func TestContextArg(t *testing.T) {
assert.Equal(t, "TEST RESULT CALL: 1", x)
}

func TestToolAs(t *testing.T) {
runner := tester.NewRunner(t)
x, err := runner.Run("", `{}`)
require.NoError(t, err)
assert.Equal(t, "TEST RESULT CALL: 1", x)
}

func TestCwd(t *testing.T) {
runner := tester.NewRunner(t)

Expand Down
67 changes: 67 additions & 0 deletions pkg/tests/testdata/TestToolAs/call1.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
`{
"Model": "gpt-4-turbo",
"InternalSystemPrompt": null,
"Tools": [
{
"function": {
"toolID": "testdata/TestToolAs/test.gpt:6",
"name": "local",
"parameters": {
"properties": {
"defaultPromptParameter": {
"description": "Prompt to send to the tool or assistant. This may be instructions or question.",
"type": "string"
}
},
"required": [
"defaultPromptParameter"
],
"type": "object"
}
}
},
{
"function": {
"toolID": "testdata/TestToolAs/other.gpt:1",
"name": "remote",
"parameters": {
"properties": {
"defaultPromptParameter": {
"description": "Prompt to send to the tool or assistant. This may be instructions or question.",
"type": "string"
}
},
"required": [
"defaultPromptParameter"
],
"type": "object"
}
}
}
],
"Messages": [
{
"role": "system",
"content": [
{
"text": "A tool"
}
],
"usage": {}
},
{
"role": "user",
"content": [
{
"text": "{}"
}
],
"usage": {}
}
],
"MaxTokens": 0,
"Temperature": null,
"JSONResponse": false,
"Grammar": "",
"Cache": null
}`
1 change: 1 addition & 0 deletions pkg/tests/testdata/TestToolAs/other.gpt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
other file
8 changes: 8 additions & 0 deletions pkg/tests/testdata/TestToolAs/test.gpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
tools: infile as local, ./other.gpt as remote

A tool

---
name: infile

infile tool
32 changes: 26 additions & 6 deletions pkg/types/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func (p Program) ChatName() string {
}

type ToolReference struct {
Named string
Reference string
Arg string
ToolID string
Expand Down Expand Up @@ -184,9 +185,14 @@ func SplitArg(hasArg string) (prefix, arg string) {
var (
fields = strings.Fields(hasArg)
idx = slices.Index(fields, "with")
asIdx = slices.Index(fields, "as")
)

if idx == -1 {
if asIdx != -1 {
return strings.Join(fields[:asIdx], " "),
strings.Join(fields[asIdx:], " ")
}
return strings.TrimSpace(hasArg), ""
}

Expand All @@ -201,7 +207,12 @@ func (t Tool) GetToolRefsFromNames(names []string) (result []ToolReference, _ er
return nil, NewErrToolNotFound(toolName)
}
_, arg := SplitArg(toolName)
named, ok := strings.CutPrefix(arg, "as ")
if !ok {
named = ""
}
result = append(result, ToolReference{
Named: named,
Arg: arg,
Reference: toolName,
ToolID: toolID,
Expand Down Expand Up @@ -287,8 +298,13 @@ func (t Tool) String() string {
func (t Tool) GetCompletionTools(prg Program) (result []CompletionTool, err error) {
toolNames := map[string]struct{}{}

for _, subToolName := range t.Parameters.Tools {
result, err = appendTool(result, prg, t, subToolName, toolNames)
subToolRefs, err := t.GetToolRefsFromNames(t.Parameters.Tools)
if err != nil {
return nil, err
}

for _, subToolRef := range subToolRefs {
result, err = appendTool(result, prg, t, subToolRef.Reference, toolNames, subToolRef.Named)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -327,7 +343,7 @@ func appendExports(completionTools []CompletionTool, prg Program, parentTool Too
}

for _, export := range subTool.Export {
completionTools, err = appendTool(completionTools, prg, subTool, export, toolNames)
completionTools, err = appendTool(completionTools, prg, subTool, export, toolNames, "")
if err != nil {
return nil, err
}
Expand All @@ -336,7 +352,7 @@ func appendExports(completionTools []CompletionTool, prg Program, parentTool Too
return completionTools, nil
}

func appendTool(completionTools []CompletionTool, prg Program, parentTool Tool, subToolName string, toolNames map[string]struct{}) ([]CompletionTool, error) {
func appendTool(completionTools []CompletionTool, prg Program, parentTool Tool, subToolName string, toolNames map[string]struct{}, asName string) ([]CompletionTool, error) {
subTool, err := getTool(prg, parentTool, subToolName)
if err != nil {
return nil, err
Expand All @@ -356,18 +372,22 @@ func appendTool(completionTools []CompletionTool, prg Program, parentTool Tool,
if subTool.Instructions == "" {
log.Debugf("Skipping zero instruction tool %s (%s)", subToolName, subTool.ID)
} else {
name := subToolName
if asName != "" {
name = asName
}
completionTools = append(completionTools, CompletionTool{
Function: CompletionFunctionDefinition{
ToolID: subTool.ID,
Name: PickToolName(subToolName, toolNames),
Name: PickToolName(name, toolNames),
Description: subTool.Parameters.Description,
Parameters: args,
},
})
}

for _, export := range subTool.Export {
completionTools, err = appendTool(completionTools, prg, subTool, export, toolNames)
completionTools, err = appendTool(completionTools, prg, subTool, export, toolNames, "")
if err != nil {
return nil, err
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/types/toolname_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ func TestParse(t *testing.T) {
tool, subTool := SplitToolRef("a from b with x")
autogold.Expect([]string{"b", "a"}).Equal(t, []string{tool, subTool})

tool, subTool = SplitToolRef("a from b with x as other")
autogold.Expect([]string{"b", "a"}).Equal(t, []string{tool, subTool})

tool, subTool = SplitToolRef("a with x")
autogold.Expect([]string{"a", ""}).Equal(t, []string{tool, subTool})

tool, subTool = SplitToolRef("a with x as other")
autogold.Expect([]string{"a", ""}).Equal(t, []string{tool, subTool})
}