Skip to content

Commit

Permalink
enhance: add a maximum consecutive tools calls restriction
Browse files Browse the repository at this point in the history
This is a safety net for instances when the LLM goes awry. There is an
undocumented environment variable to change the maximum number of
consecutive tool calls from the default of 10.

Signed-off-by: Donnie Adams <[email protected]>
  • Loading branch information
thedadams committed Jan 2, 2025
1 parent e7b2e64 commit e2979f4
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 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,26 @@ 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) {
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 and responses is limited to %d.", maxConsecutiveToolCalls)
ret.State.Completion.Messages = append(state.Completion.Messages, types.CompletionMessage{
Role: types.CompletionMessageRoleTypeAssistant,
Content: []types.ContentPart{{Text: msg}},
})
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

0 comments on commit e2979f4

Please sign in to comment.