Skip to content

enhance: add a maximum consecutive tools calls restriction #924

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
Jan 3, 2025
Merged
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
35 changes: 35 additions & 0 deletions pkg/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"context"
"encoding/json"
"fmt"
"os"
"slices"
"strconv"
"strings"
"sync"

Expand All @@ -12,6 +15,16 @@ import (
"github.com/gptscript-ai/gptscript/pkg/version"
)

var maxConsecutiveToolCalls = 10

func init() {
if val := os.Getenv("GPTSCRIPT_MAX_CONSECUTIVE_TOOL_CALLS"); val != "" {
if i, err := strconv.Atoi(val); err == nil && i > 0 {
maxConsecutiveToolCalls = i
}
}
}

type Model interface {
Call(ctx context.Context, messageRequest types.CompletionRequest, env []string, status chan<- types.CompletionStatus) (*types.CompletionMessage, error)
ProxyInfo([]string) (string, string, error)
Expand Down Expand Up @@ -385,6 +398,28 @@ func (e *Engine) complete(ctx context.Context, state *State) (*Return, error) {
}
}()

// Limit the number of consecutive tool calls and responses.
// We don't want the LLM to call tools unrestricted or get stuck in an error loop.
var messagesSinceLastUserMessage int
for _, msg := range slices.Backward(state.Completion.Messages) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the new iterator functions, nice! 🎉

if msg.Role == types.CompletionMessageRoleTypeUser {
break
}
messagesSinceLastUserMessage++
}
// Divide by 2 because tool calls come in pairs: call and response.
if messagesSinceLastUserMessage/2 > maxConsecutiveToolCalls {
msg := fmt.Sprintf("We cannot continue because the number of consecutive tool calls is limited to %d.", maxConsecutiveToolCalls)
ret.State.Completion.Messages = append(state.Completion.Messages, types.CompletionMessage{
Role: types.CompletionMessageRoleTypeAssistant,
Content: []types.ContentPart{{Text: msg}},
})
// Setting this ensures that chat continues as expected when we hit this problem.
state.Pending = map[string]types.CompletionToolCall{}
ret.Result = &msg
return &ret, nil
}

resp, err := e.Model.Call(ctx, state.Completion, e.Env, progress)
if err != nil {
return nil, fmt.Errorf("failed calling model for completion: %w", err)
Expand Down
Loading