Skip to content

Commit 61592f1

Browse files
Merge pull request #824 from ibuildthecloud/update
chore: dynamically update tools and other tool params on subsequent chats
2 parents 16ca484 + ed94de0 commit 61592f1

File tree

8 files changed

+410
-20
lines changed

8 files changed

+410
-20
lines changed

pkg/engine/engine.go

+29-20
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,29 @@ func (c *Context) WrappedContext(e *Engine) context.Context {
258258
return context.WithValue(c.Ctx, engineContext{}, &cp)
259259
}
260260

261+
func populateMessageParams(ctx Context, completion *types.CompletionRequest, tool types.Tool) error {
262+
completion.Model = tool.Parameters.ModelName
263+
completion.MaxTokens = tool.Parameters.MaxTokens
264+
completion.JSONResponse = tool.Parameters.JSONResponse
265+
completion.Cache = tool.Parameters.Cache
266+
completion.Chat = tool.Parameters.Chat
267+
completion.Temperature = tool.Parameters.Temperature
268+
completion.InternalSystemPrompt = tool.Parameters.InternalPrompt
269+
270+
if tool.Chat && completion.InternalSystemPrompt == nil {
271+
completion.InternalSystemPrompt = new(bool)
272+
}
273+
274+
var err error
275+
completion.Tools, err = tool.GetCompletionTools(*ctx.Program, ctx.AgentGroup...)
276+
if err != nil {
277+
return err
278+
}
279+
280+
completion.Messages = addUpdateSystem(ctx, tool, completion.Messages)
281+
return nil
282+
}
283+
261284
func (e *Engine) Start(ctx Context, input string) (ret *Return, _ error) {
262285
tool := ctx.Tool
263286

@@ -290,28 +313,11 @@ func (e *Engine) Start(ctx Context, input string) (ret *Return, _ error) {
290313
return nil, fmt.Errorf("credential tools cannot make calls to the LLM")
291314
}
292315

293-
completion := types.CompletionRequest{
294-
Model: tool.Parameters.ModelName,
295-
MaxTokens: tool.Parameters.MaxTokens,
296-
JSONResponse: tool.Parameters.JSONResponse,
297-
Cache: tool.Parameters.Cache,
298-
Chat: tool.Parameters.Chat,
299-
Temperature: tool.Parameters.Temperature,
300-
InternalSystemPrompt: tool.Parameters.InternalPrompt,
301-
}
302-
303-
if tool.Chat && completion.InternalSystemPrompt == nil {
304-
completion.InternalSystemPrompt = new(bool)
305-
}
306-
307-
var err error
308-
completion.Tools, err = tool.GetCompletionTools(*ctx.Program, ctx.AgentGroup...)
309-
if err != nil {
316+
var completion types.CompletionRequest
317+
if err := populateMessageParams(ctx, &completion, tool); err != nil {
310318
return nil, err
311319
}
312320

313-
completion.Messages = addUpdateSystem(ctx, tool, completion.Messages)
314-
315321
if tool.Chat && input == "{}" {
316322
input = ""
317323
}
@@ -497,6 +503,9 @@ func (e *Engine) Continue(ctx Context, state *State, results ...CallResult) (*Re
497503
return nil, fmt.Errorf("invalid continue call, no completion needed")
498504
}
499505

500-
state.Completion.Messages = addUpdateSystem(ctx, ctx.Tool, state.Completion.Messages)
506+
if err := populateMessageParams(ctx, &state.Completion, ctx.Tool); err != nil {
507+
return nil, err
508+
}
509+
501510
return e.complete(ctx.Ctx, state)
502511
}

pkg/tests/runner_test.go

+31
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"testing"
1212

1313
"github.com/gptscript-ai/gptscript/pkg/engine"
14+
"github.com/gptscript-ai/gptscript/pkg/loader"
1415
"github.com/gptscript-ai/gptscript/pkg/tests/tester"
1516
"github.com/gptscript-ai/gptscript/pkg/types"
1617
"github.com/hexops/autogold/v2"
@@ -1041,3 +1042,33 @@ func TestRuntimesLocalDev(t *testing.T) {
10411042
_ = os.RemoveAll("testdata/TestRuntimesLocalDev/node_modules")
10421043
_ = os.RemoveAll("testdata/TestRuntimesLocalDev/package-lock.json")
10431044
}
1045+
1046+
func TestToolsChange(t *testing.T) {
1047+
r := tester.NewRunner(t)
1048+
prg, err := loader.ProgramFromSource(context.Background(), `
1049+
chat: true
1050+
tools: sys.ls, sys.read, sys.write
1051+
`, "")
1052+
require.NoError(t, err)
1053+
1054+
resp, err := r.Chat(context.Background(), nil, prg, nil, "input 1")
1055+
require.NoError(t, err)
1056+
r.AssertResponded(t)
1057+
assert.False(t, resp.Done)
1058+
autogold.Expect("TEST RESULT CALL: 1").Equal(t, resp.Content)
1059+
autogold.ExpectFile(t, toJSONString(t, resp), autogold.Name(t.Name()+"/step1"))
1060+
1061+
prg, err = loader.ProgramFromSource(context.Background(), `
1062+
chat: true
1063+
temperature: 0.6
1064+
tools: sys.ls, sys.write
1065+
`, "")
1066+
require.NoError(t, err)
1067+
1068+
resp, err = r.Chat(context.Background(), resp.State, prg, nil, "input 2")
1069+
require.NoError(t, err)
1070+
r.AssertResponded(t)
1071+
assert.False(t, resp.Done)
1072+
autogold.Expect("TEST RESULT CALL: 2").Equal(t, resp.Content)
1073+
autogold.ExpectFile(t, toJSONString(t, resp), autogold.Name(t.Name()+"/step2"))
1074+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
`{
2+
"role": "assistant",
3+
"content": [
4+
{
5+
"text": "TEST RESULT CALL: 1"
6+
}
7+
],
8+
"usage": {}
9+
}`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
`{
2+
"model": "gpt-4o",
3+
"internalSystemPrompt": false,
4+
"tools": [
5+
{
6+
"function": {
7+
"toolID": "sys.ls",
8+
"name": "ls",
9+
"description": "Lists the contents of a directory",
10+
"parameters": {
11+
"properties": {
12+
"dir": {
13+
"description": "The directory to list",
14+
"type": "string"
15+
}
16+
},
17+
"type": "object"
18+
}
19+
}
20+
},
21+
{
22+
"function": {
23+
"toolID": "sys.read",
24+
"name": "read",
25+
"description": "Reads the contents of a file",
26+
"parameters": {
27+
"properties": {
28+
"filename": {
29+
"description": "The name of the file to read",
30+
"type": "string"
31+
}
32+
},
33+
"type": "object"
34+
}
35+
}
36+
},
37+
{
38+
"function": {
39+
"toolID": "sys.write",
40+
"name": "write",
41+
"description": "Write the contents to a file",
42+
"parameters": {
43+
"properties": {
44+
"content": {
45+
"description": "The content to write",
46+
"type": "string"
47+
},
48+
"filename": {
49+
"description": "The name of the file to write to",
50+
"type": "string"
51+
}
52+
},
53+
"type": "object"
54+
}
55+
}
56+
}
57+
],
58+
"messages": [
59+
{
60+
"role": "user",
61+
"content": [
62+
{
63+
"text": "input 1"
64+
}
65+
],
66+
"usage": {}
67+
}
68+
],
69+
"chat": true
70+
}`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
`{
2+
"role": "assistant",
3+
"content": [
4+
{
5+
"text": "TEST RESULT CALL: 2"
6+
}
7+
],
8+
"usage": {}
9+
}`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
`{
2+
"model": "gpt-4o",
3+
"internalSystemPrompt": false,
4+
"tools": [
5+
{
6+
"function": {
7+
"toolID": "sys.ls",
8+
"name": "ls",
9+
"description": "Lists the contents of a directory",
10+
"parameters": {
11+
"properties": {
12+
"dir": {
13+
"description": "The directory to list",
14+
"type": "string"
15+
}
16+
},
17+
"type": "object"
18+
}
19+
}
20+
},
21+
{
22+
"function": {
23+
"toolID": "sys.write",
24+
"name": "write",
25+
"description": "Write the contents to a file",
26+
"parameters": {
27+
"properties": {
28+
"content": {
29+
"description": "The content to write",
30+
"type": "string"
31+
},
32+
"filename": {
33+
"description": "The name of the file to write to",
34+
"type": "string"
35+
}
36+
},
37+
"type": "object"
38+
}
39+
}
40+
}
41+
],
42+
"messages": [
43+
{
44+
"role": "user",
45+
"content": [
46+
{
47+
"text": "input 1"
48+
}
49+
],
50+
"usage": {}
51+
},
52+
{
53+
"role": "assistant",
54+
"content": [
55+
{
56+
"text": "TEST RESULT CALL: 1"
57+
}
58+
],
59+
"usage": {}
60+
},
61+
{
62+
"role": "user",
63+
"content": [
64+
{
65+
"text": "input 2"
66+
}
67+
],
68+
"usage": {}
69+
}
70+
],
71+
"chat": true,
72+
"temperature": 0.6
73+
}`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
`{
2+
"done": false,
3+
"content": "TEST RESULT CALL: 1",
4+
"toolID": "inline:",
5+
"state": {
6+
"continuation": {
7+
"state": {
8+
"input": "input 1",
9+
"completion": {
10+
"model": "gpt-4o",
11+
"internalSystemPrompt": false,
12+
"tools": [
13+
{
14+
"function": {
15+
"toolID": "sys.ls",
16+
"name": "ls",
17+
"description": "Lists the contents of a directory",
18+
"parameters": {
19+
"properties": {
20+
"dir": {
21+
"description": "The directory to list",
22+
"type": "string"
23+
}
24+
},
25+
"type": "object"
26+
}
27+
}
28+
},
29+
{
30+
"function": {
31+
"toolID": "sys.read",
32+
"name": "read",
33+
"description": "Reads the contents of a file",
34+
"parameters": {
35+
"properties": {
36+
"filename": {
37+
"description": "The name of the file to read",
38+
"type": "string"
39+
}
40+
},
41+
"type": "object"
42+
}
43+
}
44+
},
45+
{
46+
"function": {
47+
"toolID": "sys.write",
48+
"name": "write",
49+
"description": "Write the contents to a file",
50+
"parameters": {
51+
"properties": {
52+
"content": {
53+
"description": "The content to write",
54+
"type": "string"
55+
},
56+
"filename": {
57+
"description": "The name of the file to write to",
58+
"type": "string"
59+
}
60+
},
61+
"type": "object"
62+
}
63+
}
64+
}
65+
],
66+
"messages": [
67+
{
68+
"role": "user",
69+
"content": [
70+
{
71+
"text": "input 1"
72+
}
73+
],
74+
"usage": {}
75+
},
76+
{
77+
"role": "assistant",
78+
"content": [
79+
{
80+
"text": "TEST RESULT CALL: 1"
81+
}
82+
],
83+
"usage": {}
84+
}
85+
],
86+
"chat": true
87+
}
88+
},
89+
"result": "TEST RESULT CALL: 1"
90+
},
91+
"continuationToolID": "inline:"
92+
}
93+
}`

0 commit comments

Comments
 (0)