Skip to content

Commit 53b9671

Browse files
committed
PR feedback
Signed-off-by: Grant Linville <[email protected]>
1 parent 30f24a4 commit 53b9671

File tree

2 files changed

+3
-6
lines changed

2 files changed

+3
-6
lines changed

pkg/openai/client.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"errors"
66
"io"
77
"log/slog"
8-
"math"
98
"os"
109
"slices"
1110
"sort"
@@ -322,7 +321,7 @@ func (c *Client) Call(ctx context.Context, messageRequest types.CompletionReques
322321
if messageRequest.Chat {
323322
// Check the last message. If it is from a tool call, and if it takes up more than 80% of the budget on its own, reject it.
324323
lastMessage := msgs[len(msgs)-1]
325-
if lastMessage.Role == string(types.CompletionMessageRoleTypeTool) && countMessage(lastMessage) > int(math.Round(float64(getBudget(messageRequest.MaxTokens))*0.8)) {
324+
if lastMessage.Role == string(types.CompletionMessageRoleTypeTool) && countMessage(lastMessage) > int(float64(getBudget(messageRequest.MaxTokens))*0.8) {
326325
// We need to update it in the msgs slice for right now and in the messageRequest for future calls.
327326
msgs[len(msgs)-1].Content = TooLongMessage
328327
messageRequest.Messages[len(messageRequest.Messages)-1].Content = types.Text(TooLongMessage)
@@ -397,7 +396,7 @@ func (c *Client) Call(ctx context.Context, messageRequest types.CompletionReques
397396

398397
// If we got back a context length exceeded error, keep retrying and shrinking the message history until we pass.
399398
var apiError *openai.APIError
400-
if err != nil && errors.As(err, &apiError) && apiError.Code == "context_length_exceeded" && messageRequest.Chat {
399+
if errors.As(err, &apiError) && apiError.Code == "context_length_exceeded" && messageRequest.Chat {
401400
// Decrease maxTokens by 10% to make garbage collection more aggressive.
402401
// The retry loop will further decrease maxTokens if needed.
403402
maxTokens := decreaseTenPercent(messageRequest.MaxTokens)

pkg/openai/count.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package openai
22

33
import (
4-
"math"
5-
64
openai "github.com/gptscript-ai/chat-completion-client"
75
)
86

97
const DefaultMaxTokens = 128_000
108

119
func decreaseTenPercent(maxTokens int) int {
1210
maxTokens = getBudget(maxTokens)
13-
return int(math.Round(float64(maxTokens) * 0.9))
11+
return int(float64(maxTokens) * 0.9)
1412
}
1513

1614
func getBudget(maxTokens int) int {

0 commit comments

Comments
 (0)