Skip to content

Commit

Permalink
Merge pull request #620 from ibuildthecloud/main
Browse files Browse the repository at this point in the history
chore: add support for tools with only a single agent line and nothing else
  • Loading branch information
ibuildthecloud authored Jul 10, 2024
2 parents 6cdd89d + 476f4ae commit 1f9e86d
Show file tree
Hide file tree
Showing 11 changed files with 346 additions and 2 deletions.
5 changes: 5 additions & 0 deletions pkg/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ func NewContext(ctx context.Context, prg *types.Program, input string) (Context,
}

callCtx.AgentGroup = agentGroup

if callCtx.Tool.IsAgentsOnly() && len(callCtx.AgentGroup) > 0 {
callCtx.Tool = callCtx.Program.ToolSet[callCtx.AgentGroup[0].ToolID]
}

return callCtx, nil
}

Expand Down
6 changes: 4 additions & 2 deletions pkg/input/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package input
import (
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"strings"

"github.com/gptscript-ai/gptscript/internal"
"github.com/gptscript-ai/gptscript/pkg/loader"
"github.com/gptscript-ai/gptscript/pkg/types"
)
Expand All @@ -33,7 +35,7 @@ func FromFile(file string) (string, error) {
}
return string(data), nil
} else if file != "" {
if s, err := os.Stat(file); err == nil && s.IsDir() {
if s, err := fs.Stat(internal.FS, file); err == nil && s.IsDir() {
for _, ext := range types.DefaultFiles {
if _, err := os.Stat(filepath.Join(file, ext)); err == nil {
file = filepath.Join(file, ext)
Expand All @@ -42,7 +44,7 @@ func FromFile(file string) (string, error) {
}
}
log.Debugf("reading file %s", file)
data, err := os.ReadFile(file)
data, err := fs.ReadFile(internal.FS, file)
if err != nil {
return "", fmt.Errorf("reading %s: %w", file, err)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ func (c *context) finish(tools *[]Node) {
len(c.tool.GlobalTools) > 0 ||
len(c.tool.ExportInputFilters) > 0 ||
len(c.tool.ExportOutputFilters) > 0 ||
len(c.tool.Agents) > 0 ||
c.tool.Chat {
*tools = append(*tools, Node{
ToolNode: &ToolNode{
Expand Down
20 changes: 20 additions & 0 deletions pkg/tests/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,26 @@ func TestExport(t *testing.T) {
assert.Equal(t, "TEST RESULT CALL: 3", x)
}

func TestAgentOnly(t *testing.T) {
r := tester.NewRunner(t)

prg, err := r.Load("")
require.NoError(t, err)

r.RespondWith(tester.Result{
Func: types.CompletionFunctionCall{
Name: "agent2",
Arguments: "Agent 2 input",
},
})

resp, err := r.Chat(context.Background(), nil, prg, nil, "Input 1")
require.NoError(t, err)
r.AssertResponded(t)
assert.False(t, resp.Done)
autogold.Expect("TEST RESULT CALL: 2").Equal(t, resp.Content)
autogold.ExpectFile(t, toJSONString(t, resp), autogold.Name(t.Name()+"/step1"))
}
func TestAgents(t *testing.T) {
r := tester.NewRunner(t)

Expand Down
16 changes: 16 additions & 0 deletions pkg/tests/testdata/TestAgentOnly/call1-resp.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
`{
"role": "assistant",
"content": [
{
"toolCall": {
"index": 0,
"id": "call_1",
"function": {
"name": "agent2",
"arguments": "Agent 2 input"
}
}
}
],
"usage": {}
}`
42 changes: 42 additions & 0 deletions pkg/tests/testdata/TestAgentOnly/call1.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
`{
"model": "gpt-4o",
"internalSystemPrompt": false,
"tools": [
{
"function": {
"toolID": "testdata/TestAgentOnly/test.gpt:agent2",
"name": "agent2",
"parameters": {
"properties": {
"defaultPromptParameter": {
"description": "Prompt to send to the assistant. This may be an instruction or question.",
"type": "string"
}
},
"type": "object"
}
}
}
],
"messages": [
{
"role": "system",
"content": [
{
"text": "I am agent1"
}
],
"usage": {}
},
{
"role": "user",
"content": [
{
"text": "Input 1"
}
],
"usage": {}
}
],
"chat": true
}`
9 changes: 9 additions & 0 deletions pkg/tests/testdata/TestAgentOnly/call2-resp.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
`{
"role": "assistant",
"content": [
{
"text": "TEST RESULT CALL: 2"
}
],
"usage": {}
}`
57 changes: 57 additions & 0 deletions pkg/tests/testdata/TestAgentOnly/call2.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
`{
"model": "gpt-4o",
"internalSystemPrompt": false,
"tools": [
{
"function": {
"toolID": "testdata/TestAgentOnly/test.gpt:agent1",
"name": "agent1",
"parameters": {
"properties": {
"defaultPromptParameter": {
"description": "Prompt to send to the assistant. This may be an instruction or question.",
"type": "string"
}
},
"type": "object"
}
}
},
{
"function": {
"toolID": "testdata/TestAgentOnly/test.gpt:agent3",
"name": "agent3",
"parameters": {
"properties": {
"defaultPromptParameter": {
"description": "Prompt to send to the assistant. This may be an instruction or question.",
"type": "string"
}
},
"type": "object"
}
}
}
],
"messages": [
{
"role": "system",
"content": [
{
"text": "I am agent2"
}
],
"usage": {}
},
{
"role": "user",
"content": [
{
"text": "Agent 2 input"
}
],
"usage": {}
}
],
"chat": true
}`
168 changes: 168 additions & 0 deletions pkg/tests/testdata/TestAgentOnly/step1.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
`{
"done": false,
"content": "TEST RESULT CALL: 2",
"toolID": "testdata/TestAgentOnly/test.gpt:agent2",
"state": {
"continuation": {
"state": {
"input": "Input 1",
"completion": {
"model": "gpt-4o",
"internalSystemPrompt": false,
"tools": [
{
"function": {
"toolID": "testdata/TestAgentOnly/test.gpt:agent2",
"name": "agent2",
"parameters": {
"properties": {
"defaultPromptParameter": {
"description": "Prompt to send to the assistant. This may be an instruction or question.",
"type": "string"
}
},
"type": "object"
}
}
}
],
"messages": [
{
"role": "system",
"content": [
{
"text": "I am agent1"
}
],
"usage": {}
},
{
"role": "user",
"content": [
{
"text": "Input 1"
}
],
"usage": {}
},
{
"role": "assistant",
"content": [
{
"toolCall": {
"index": 0,
"id": "call_1",
"function": {
"name": "agent2",
"arguments": "Agent 2 input"
}
}
}
],
"usage": {}
}
],
"chat": true
},
"pending": {
"call_1": {
"index": 0,
"id": "call_1",
"function": {
"name": "agent2",
"arguments": "Agent 2 input"
}
}
}
},
"calls": {
"call_1": {
"toolID": "testdata/TestAgentOnly/test.gpt:agent2",
"input": "Agent 2 input"
}
}
},
"subCalls": [
{
"toolId": "testdata/TestAgentOnly/test.gpt:agent2",
"callId": "call_1",
"state": {
"continuation": {
"state": {
"input": "Agent 2 input",
"completion": {
"model": "gpt-4o",
"internalSystemPrompt": false,
"tools": [
{
"function": {
"toolID": "testdata/TestAgentOnly/test.gpt:agent1",
"name": "agent1",
"parameters": {
"properties": {
"defaultPromptParameter": {
"description": "Prompt to send to the assistant. This may be an instruction or question.",
"type": "string"
}
},
"type": "object"
}
}
},
{
"function": {
"toolID": "testdata/TestAgentOnly/test.gpt:agent3",
"name": "agent3",
"parameters": {
"properties": {
"defaultPromptParameter": {
"description": "Prompt to send to the assistant. This may be an instruction or question.",
"type": "string"
}
},
"type": "object"
}
}
}
],
"messages": [
{
"role": "system",
"content": [
{
"text": "I am agent2"
}
],
"usage": {}
},
{
"role": "user",
"content": [
{
"text": "Agent 2 input"
}
],
"usage": {}
},
{
"role": "assistant",
"content": [
{
"text": "TEST RESULT CALL: 2"
}
],
"usage": {}
}
],
"chat": true
}
},
"result": "TEST RESULT CALL: 2"
},
"continuationToolID": "testdata/TestAgentOnly/test.gpt:agent2"
}
}
],
"subCallID": "call_1"
}
}`
20 changes: 20 additions & 0 deletions pkg/tests/testdata/TestAgentOnly/test.gpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
agents: agent1, agent2

----
name: agent1
chat: true

I am agent1

----
name: agent2
chat: true
agents: agent3

I am agent2

---
name: agent3
chat: true

I am agent3
Loading

0 comments on commit 1f9e86d

Please sign in to comment.