Skip to content

bug: fix concatinating multiple tool calls when index is not set #374

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 17, 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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/gptscript-ai/chat-completion-client v0.0.0-20240515050533-bdef9f2226a9
github.com/hexops/autogold/v2 v2.2.1
github.com/hexops/valast v1.4.4
github.com/jaytaylor/html2text v0.0.0-20230321000545-74c2419ad056
github.com/mholt/archiver/v4 v4.0.0-alpha.8
github.com/olahol/melody v1.1.4
Expand Down Expand Up @@ -48,7 +49,6 @@ require (
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hexops/autogold v1.3.1 // indirect
github.com/hexops/gotextdiff v1.0.3 // indirect
github.com/hexops/valast v1.4.4 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/invopop/yaml v0.2.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
Expand Down
5 changes: 3 additions & 2 deletions pkg/openai/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,8 @@ func appendMessage(msg types.CompletionMessage, response openai.ChatCompletionSt
delta := response.Choices[0].Delta
msg.Role = types.CompletionMessageRoleType(override(string(msg.Role), delta.Role))

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

if !streamResponse {
request.StreamOptions = nil
resp, err := c.c.CreateChatCompletion(ctx, request)
if err != nil {
return nil, err
Expand Down
50 changes: 50 additions & 0 deletions pkg/openai/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package openai

import (
"testing"

openai "github.com/gptscript-ai/chat-completion-client"
"github.com/gptscript-ai/gptscript/pkg/types"
"github.com/hexops/autogold/v2"
"github.com/hexops/valast"
)

func Test_appendMessage(t *testing.T) {
autogold.Expect(types.CompletionMessage{Content: []types.ContentPart{
{ToolCall: &types.CompletionToolCall{
Index: valast.Ptr(0),
Function: types.CompletionFunctionCall{
Name: "foo",
Arguments: "bar",
},
}},
{ToolCall: &types.CompletionToolCall{
Index: valast.Ptr(1),
Function: types.CompletionFunctionCall{
Name: "foo",
Arguments: "bar",
},
}},
}}).Equal(t, appendMessage(types.CompletionMessage{}, openai.ChatCompletionStreamResponse{
Choices: []openai.ChatCompletionStreamChoice{
{
Delta: openai.ChatCompletionStreamChoiceDelta{
ToolCalls: []openai.ToolCall{
{
Function: openai.FunctionCall{
Name: "foo",
Arguments: "bar",
},
},
{
Function: openai.FunctionCall{
Name: "foo",
Arguments: "bar",
},
},
},
},
},
},
}))
}