Skip to content

Commit

Permalink
add timeout to openai create (microsoft#973)
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkall authored Apr 3, 2023
1 parent 5616ce0 commit 904b5ea
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions flaml/integrations/oai/completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ class Completion:
retry_time = 10
# fail a request after hitting RateLimitError for this many seconds
retry_timeout = 60
# time out for request to openai server
request_timeout = 30

openai_completion_class = not ERROR and openai.Completion
_total_cost = 0
Expand Down Expand Up @@ -137,7 +139,9 @@ def _get_response(cls, config: dict, eval_only=False, use_cache=True):
start_time = time.time()
while True:
try:
response = openai_completion.create(**config)
response = openai_completion.create(
request_timeout=cls.request_timeout, **config
)
cls._cache.set(key, response)
return response
except (
Expand Down Expand Up @@ -635,7 +639,9 @@ def create(cls, context, use_cache=True, **config):
if use_cache:
with diskcache.Cache(cls.cache_path) as cls._cache:
return cls._get_response(params)
return cls.openai_completion_class.create(**params)
return cls.openai_completion_class.create(
request_timeout=cls.request_timeout, **params
)

@classmethod
def _construct_params(cls, data_instance, config, prompt=None, messages=None):
Expand Down

0 comments on commit 904b5ea

Please sign in to comment.