From 2eb1311d74cfc7dcd352cbc655fc4df4956e930d Mon Sep 17 00:00:00 2001 From: Thorsten Klein Date: Thu, 12 Dec 2024 20:56:52 +0100 Subject: [PATCH 1/2] feat: chat-completion-client retries by default - to disable via env --- go.mod | 4 ++++ pkg/openai/client.go | 13 +++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index 5fa1a5c8..716d610c 100644 --- a/go.mod +++ b/go.mod @@ -2,6 +2,10 @@ module github.com/gptscript-ai/gptscript go 1.23.1 +replace ( + github.com/gptscript-ai/chat-completion-client => ../chat-completion-client + ) + require ( github.com/AlecAivazis/survey/v2 v2.3.7 github.com/BurntSushi/locker v0.0.0-20171006230638-a6e239ea1c69 diff --git a/pkg/openai/client.go b/pkg/openai/client.go index 1894bdda..dea234a9 100644 --- a/pkg/openai/client.go +++ b/pkg/openai/client.go @@ -555,10 +555,19 @@ func (c *Client) call(ctx context.Context, request openai.ChatCompletionRequest, var ( headers map[string]string modelProviderEnv []string + retryOpts = []openai.RetryOptions{ + { + Retries: 5, + RetryAboveCode: 499, // 5xx errors + RetryCodes: []int{429}, // 429 Too Many Requests (ratelimit) + }, + } ) for _, e := range env { if strings.HasPrefix(e, "GPTSCRIPT_MODEL_PROVIDER_") { modelProviderEnv = append(modelProviderEnv, e) + } else if strings.HasPrefix(e, "GPTSCRIPT_DISABLE_RETRIES") { + retryOpts = nil } } @@ -572,7 +581,7 @@ func (c *Client) call(ctx context.Context, request openai.ChatCompletionRequest, if !streamResponse { request.StreamOptions = nil - resp, err := c.c.CreateChatCompletion(ctx, request, headers) + resp, err := c.c.CreateChatCompletion(ctx, request, headers, retryOpts...) if err != nil { return types.CompletionMessage{}, err } @@ -597,7 +606,7 @@ func (c *Client) call(ctx context.Context, request openai.ChatCompletionRequest, }), nil } - stream, err := c.c.CreateChatCompletionStream(ctx, request, headers) + stream, err := c.c.CreateChatCompletionStream(ctx, request, headers, retryOpts...) if err != nil { return types.CompletionMessage{}, err } From 09c776eb167d947e8c7642eefa775c18032ebd08 Mon Sep 17 00:00:00 2001 From: Thorsten Klein Date: Mon, 16 Dec 2024 21:39:26 +0100 Subject: [PATCH 2/2] fix: dep --- go.mod | 6 +----- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/go.mod b/go.mod index 716d610c..fb81ca10 100644 --- a/go.mod +++ b/go.mod @@ -2,10 +2,6 @@ module github.com/gptscript-ai/gptscript go 1.23.1 -replace ( - github.com/gptscript-ai/chat-completion-client => ../chat-completion-client - ) - require ( github.com/AlecAivazis/survey/v2 v2.3.7 github.com/BurntSushi/locker v0.0.0-20171006230638-a6e239ea1c69 @@ -19,7 +15,7 @@ require ( github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 github.com/google/uuid v1.6.0 github.com/gptscript-ai/broadcaster v0.0.0-20240625175512-c43682019b86 - github.com/gptscript-ai/chat-completion-client v0.0.0-20241104122544-5fe75f07c131 + github.com/gptscript-ai/chat-completion-client v0.0.0-20241216203633-5c0178fb89ed github.com/gptscript-ai/cmd v0.0.0-20240802230653-326b7baf6fcb github.com/gptscript-ai/go-gptscript v0.9.5-rc5.0.20240927213153-2af51434b93e github.com/gptscript-ai/tui v0.0.0-20240923192013-172e51ccf1d6 diff --git a/go.sum b/go.sum index 3661a6c6..85184dc1 100644 --- a/go.sum +++ b/go.sum @@ -200,8 +200,8 @@ github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY= github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c= github.com/gptscript-ai/broadcaster v0.0.0-20240625175512-c43682019b86 h1:m9yLtIEd0z1ia8qFjq3u0Ozb6QKwidyL856JLJp6nbA= github.com/gptscript-ai/broadcaster v0.0.0-20240625175512-c43682019b86/go.mod h1:lK3K5EZx4dyT24UG3yCt0wmspkYqrj4D/8kxdN3relk= -github.com/gptscript-ai/chat-completion-client v0.0.0-20241104122544-5fe75f07c131 h1:y2FcmT4X8U606gUS0teX5+JWX9K/NclsLEhHiyrd+EU= -github.com/gptscript-ai/chat-completion-client v0.0.0-20241104122544-5fe75f07c131/go.mod h1:7P/o6/IWa1KqsntVf68hSnLKuu3+xuqm6lYhch1w4jo= +github.com/gptscript-ai/chat-completion-client v0.0.0-20241216203633-5c0178fb89ed h1:qMHm0IYpKgmw4KHX76RMB/duSICxo7IZuimPCKb0qG4= +github.com/gptscript-ai/chat-completion-client v0.0.0-20241216203633-5c0178fb89ed/go.mod h1:7P/o6/IWa1KqsntVf68hSnLKuu3+xuqm6lYhch1w4jo= github.com/gptscript-ai/cmd v0.0.0-20240802230653-326b7baf6fcb h1:ky2J2CzBOskC7Jgm2VJAQi2x3p7FVGa+2/PcywkFJuc= github.com/gptscript-ai/cmd v0.0.0-20240802230653-326b7baf6fcb/go.mod h1:DJAo1xTht1LDkNYFNydVjTHd576TC7MlpsVRl3oloVw= github.com/gptscript-ai/go-gptscript v0.9.5-rc5.0.20240927213153-2af51434b93e h1:WpNae0NBx+Ri8RB3SxF8DhadDKU7h+jfWPQterDpbJA=