Skip to content

Commit eb03680

Browse files
authored
feat: chat-completion-client retries by default - to disable via env (#917)
* feat: chat-completion-client retries by default - to disable via env
1 parent e758b54 commit eb03680

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ require (
1515
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
1616
github.com/google/uuid v1.6.0
1717
github.com/gptscript-ai/broadcaster v0.0.0-20240625175512-c43682019b86
18-
github.com/gptscript-ai/chat-completion-client v0.0.0-20241104122544-5fe75f07c131
18+
github.com/gptscript-ai/chat-completion-client v0.0.0-20241216203633-5c0178fb89ed
1919
github.com/gptscript-ai/cmd v0.0.0-20240802230653-326b7baf6fcb
2020
github.com/gptscript-ai/go-gptscript v0.9.5-rc5.0.20240927213153-2af51434b93e
2121
github.com/gptscript-ai/tui v0.0.0-20240923192013-172e51ccf1d6

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,8 @@ github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY=
200200
github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c=
201201
github.com/gptscript-ai/broadcaster v0.0.0-20240625175512-c43682019b86 h1:m9yLtIEd0z1ia8qFjq3u0Ozb6QKwidyL856JLJp6nbA=
202202
github.com/gptscript-ai/broadcaster v0.0.0-20240625175512-c43682019b86/go.mod h1:lK3K5EZx4dyT24UG3yCt0wmspkYqrj4D/8kxdN3relk=
203-
github.com/gptscript-ai/chat-completion-client v0.0.0-20241104122544-5fe75f07c131 h1:y2FcmT4X8U606gUS0teX5+JWX9K/NclsLEhHiyrd+EU=
204-
github.com/gptscript-ai/chat-completion-client v0.0.0-20241104122544-5fe75f07c131/go.mod h1:7P/o6/IWa1KqsntVf68hSnLKuu3+xuqm6lYhch1w4jo=
203+
github.com/gptscript-ai/chat-completion-client v0.0.0-20241216203633-5c0178fb89ed h1:qMHm0IYpKgmw4KHX76RMB/duSICxo7IZuimPCKb0qG4=
204+
github.com/gptscript-ai/chat-completion-client v0.0.0-20241216203633-5c0178fb89ed/go.mod h1:7P/o6/IWa1KqsntVf68hSnLKuu3+xuqm6lYhch1w4jo=
205205
github.com/gptscript-ai/cmd v0.0.0-20240802230653-326b7baf6fcb h1:ky2J2CzBOskC7Jgm2VJAQi2x3p7FVGa+2/PcywkFJuc=
206206
github.com/gptscript-ai/cmd v0.0.0-20240802230653-326b7baf6fcb/go.mod h1:DJAo1xTht1LDkNYFNydVjTHd576TC7MlpsVRl3oloVw=
207207
github.com/gptscript-ai/go-gptscript v0.9.5-rc5.0.20240927213153-2af51434b93e h1:WpNae0NBx+Ri8RB3SxF8DhadDKU7h+jfWPQterDpbJA=

pkg/openai/client.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -555,10 +555,19 @@ func (c *Client) call(ctx context.Context, request openai.ChatCompletionRequest,
555555
var (
556556
headers map[string]string
557557
modelProviderEnv []string
558+
retryOpts = []openai.RetryOptions{
559+
{
560+
Retries: 5,
561+
RetryAboveCode: 499, // 5xx errors
562+
RetryCodes: []int{429}, // 429 Too Many Requests (ratelimit)
563+
},
564+
}
558565
)
559566
for _, e := range env {
560567
if strings.HasPrefix(e, "GPTSCRIPT_MODEL_PROVIDER_") {
561568
modelProviderEnv = append(modelProviderEnv, e)
569+
} else if strings.HasPrefix(e, "GPTSCRIPT_DISABLE_RETRIES") {
570+
retryOpts = nil
562571
}
563572
}
564573

@@ -572,7 +581,7 @@ func (c *Client) call(ctx context.Context, request openai.ChatCompletionRequest,
572581

573582
if !streamResponse {
574583
request.StreamOptions = nil
575-
resp, err := c.c.CreateChatCompletion(ctx, request, headers)
584+
resp, err := c.c.CreateChatCompletion(ctx, request, headers, retryOpts...)
576585
if err != nil {
577586
return types.CompletionMessage{}, err
578587
}
@@ -597,7 +606,7 @@ func (c *Client) call(ctx context.Context, request openai.ChatCompletionRequest,
597606
}), nil
598607
}
599608

600-
stream, err := c.c.CreateChatCompletionStream(ctx, request, headers)
609+
stream, err := c.c.CreateChatCompletionStream(ctx, request, headers, retryOpts...)
601610
if err != nil {
602611
return types.CompletionMessage{}, err
603612
}

0 commit comments

Comments
 (0)