Skip to content

Commit 0127cca

Browse files
bug: fix concatinating multiple tool calls when index is not set
1 parent fa496ac commit 0127cca

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ require (
1616
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
1717
github.com/gptscript-ai/chat-completion-client v0.0.0-20240515050533-bdef9f2226a9
1818
github.com/hexops/autogold/v2 v2.2.1
19+
github.com/hexops/valast v1.4.4
1920
github.com/jaytaylor/html2text v0.0.0-20230321000545-74c2419ad056
2021
github.com/mholt/archiver/v4 v4.0.0-alpha.8
2122
github.com/olahol/melody v1.1.4
@@ -48,7 +49,6 @@ require (
4849
github.com/hashicorp/go-multierror v1.1.1 // indirect
4950
github.com/hexops/autogold v1.3.1 // indirect
5051
github.com/hexops/gotextdiff v1.0.3 // indirect
51-
github.com/hexops/valast v1.4.4 // indirect
5252
github.com/inconshreveable/mousetrap v1.1.0 // indirect
5353
github.com/invopop/yaml v0.2.0 // indirect
5454
github.com/josharian/intern v1.0.0 // indirect

pkg/openai/client.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,8 +414,8 @@ func appendMessage(msg types.CompletionMessage, response openai.ChatCompletionSt
414414
delta := response.Choices[0].Delta
415415
msg.Role = types.CompletionMessageRoleType(override(string(msg.Role), delta.Role))
416416

417-
for _, tool := range delta.ToolCalls {
418-
idx := 0
417+
for i, tool := range delta.ToolCalls {
418+
idx := i
419419
if tool.Index != nil {
420420
idx = *tool.Index
421421
}
@@ -484,6 +484,7 @@ func (c *Client) call(ctx context.Context, request openai.ChatCompletionRequest,
484484
slog.Debug("calling openai", "message", request.Messages)
485485

486486
if !streamResponse {
487+
request.StreamOptions = nil
487488
resp, err := c.c.CreateChatCompletion(ctx, request)
488489
if err != nil {
489490
return nil, err

pkg/openai/client_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package openai
2+
3+
import (
4+
"testing"
5+
6+
openai "github.com/gptscript-ai/chat-completion-client"
7+
"github.com/gptscript-ai/gptscript/pkg/types"
8+
"github.com/hexops/autogold/v2"
9+
"github.com/hexops/valast"
10+
)
11+
12+
func Test_appendMessage(t *testing.T) {
13+
autogold.Expect(types.CompletionMessage{Content: []types.ContentPart{
14+
{ToolCall: &types.CompletionToolCall{
15+
Index: valast.Ptr(0),
16+
Function: types.CompletionFunctionCall{
17+
Name: "foo",
18+
Arguments: "bar",
19+
},
20+
}},
21+
{ToolCall: &types.CompletionToolCall{
22+
Index: valast.Ptr(1),
23+
Function: types.CompletionFunctionCall{
24+
Name: "foo",
25+
Arguments: "bar",
26+
},
27+
}},
28+
}}).Equal(t, appendMessage(types.CompletionMessage{}, openai.ChatCompletionStreamResponse{
29+
Choices: []openai.ChatCompletionStreamChoice{
30+
{
31+
Delta: openai.ChatCompletionStreamChoiceDelta{
32+
ToolCalls: []openai.ToolCall{
33+
{
34+
Function: openai.FunctionCall{
35+
Name: "foo",
36+
Arguments: "bar",
37+
},
38+
},
39+
{
40+
Function: openai.FunctionCall{
41+
Name: "foo",
42+
Arguments: "bar",
43+
},
44+
},
45+
},
46+
},
47+
},
48+
},
49+
}))
50+
}

0 commit comments

Comments
 (0)