irudnyts / openai

An R package-wrapper around OpenAI API
https://irudnyts.github.io/openai/
Other
164 stars 28 forks source link

Include option for automatic retries #44

Open christophscheuch opened 1 year ago

christophscheuch commented 1 year ago

As it turns out the OpenAI API frequently returns very unhelpful error messages like this one:

The server had an error while processing your request. Sorry about that!

There are also several threads on this issue, for instance in the OpenAI Develper forum.

I think some parameter like max_retries could be really useful to handle such transient errors that arise while applying functions like create_chat_completion().

The httr2 package provides the very useful req_retry() function that allows to define the maximum number of retries, which errors are transient, and how long to wait between retries. Is there any specific reason that you sticked httr for the openai package?

olivierISCIII commented 1 year ago

I think these connection problems can be fixed with the insistently function from the purrr package without having to modify the openai package.

christophscheuch commented 1 year ago

Good suggestion, thanks!

Btw httr::RETRY() is a special case of insistently() for HTTP verbs :)

irudnyts commented 1 year ago

@christophscheuch

I think some parameter like max_retries could be really useful to handle such transient errors that arise while applying functions like create_chat_completion().

I am trying to be as close as possible to the original Python package and not introduce new parameters :) But maybe I will change my mind at some point.

The httr2 package provides the very useful req_retry() function that allows to define the maximum number of retries, which errors are transient, and how long to wait between retries. Is there any specific reason that you sticked httr for the openai package?

No good reason at all. It just seemed a natural candidate, and tbh, I did not even know that httr2 exist.

@olivierISCIII Thanks for the suggestion!

olyerickson commented 1 year ago

I've very interested in this as well, as we (and everyone else) are seeing the openai.error.RateLimitError much more frequently over the past week or so...

irudnyts commented 9 months ago

@christophscheuch @olyerickson @olivierISCIII Hadley rolled out stable version 1.0.0 of {httr2}, and from now on, I think it finally makes sense to use it.

Here is the new argument for max_retries, as in the official OpenAI's Python package:

remotes::install_github("irudnyts/openai", ref = "r6")

library(openai)
client <- OpenAI(max_retries = 3) # XXX: here we go! 
completion <- client$chat$completions$create(
    model = "gpt-3.5-turbo",
    messages = list(list("role" = "user", "content" = "What's up?"))
)