diff --git a/go.mod b/go.mod index 5fa1a5c8..fb81ca10 100644 --- a/go.mod +++ b/go.mod @@ -15,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= 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 }