Skip to content

Commit

Permalink
fix: only count assistant messages when limiting consecutive tool calls
Browse files Browse the repository at this point in the history
Before this change, the number of consecutive tool calls was counted by
looking at all messages since the last time a message with role "user"
was sent. This was bad logic because the LLM could ask for many parallel
tool calls.

This change addresses this problem by only considering messages with
role "assistant" with tool calls since the last user message.

Signed-off-by: Donnie Adams <[email protected]>
  • Loading branch information
thedadams committed Jan 5, 2025
1 parent a89c442 commit 78f07b0
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions pkg/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,17 +400,24 @@ func (e *Engine) complete(ctx context.Context, state *State) (*Return, error) {
}
}()

// Limit the number of consecutive tool calls and responses.
// Limit the number of consecutive tool calls.
// 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
} else if msg.Role == types.CompletionMessageRoleTypeAssistant {
for _, content := range msg.Content {
// If this message is requesting that a tool call be made, then count it towards the limit.
if content.ToolCall != nil {
messagesSinceLastUserMessage++
break
}
}
}
messagesSinceLastUserMessage++
}
// Divide by 2 because tool calls come in pairs: call and response.
if messagesSinceLastUserMessage/2 > maxConsecutiveToolCalls {

if messagesSinceLastUserMessage > 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,
Expand Down

0 comments on commit 78f07b0

Please sign in to comment.