forked from openai/openai-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompletion.py
More file actions
50 lines (39 loc) · 1.57 KB
/
completion.py
File metadata and controls
50 lines (39 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import time
from openai import util
from openai.api_resources.abstract import DeletableAPIResource, ListableAPIResource
from openai.api_resources.abstract.engine_api_resource import EngineAPIResource
from openai.error import TryAgain
class Completion(EngineAPIResource):
OBJECT_NAME = "completions"
@classmethod
def create(cls, *args, **kwargs):
"""
Creates a new completion for the provided prompt and parameters.
See https://platform.openai.com/docs/api-reference/completions/create for a list
of valid parameters.
"""
start = time.time()
timeout = kwargs.pop("timeout", None)
while True:
try:
return super().create(*args, **kwargs)
except TryAgain as e:
if timeout is not None and time.time() > start + timeout:
raise
util.log_info("Waiting for model to warm up", error=e)
@classmethod
async def acreate(cls, *args, **kwargs):
"""
Creates a new completion for the provided prompt and parameters.
See https://platform.openai.com/docs/api-reference/completions/create for a list
of valid parameters.
"""
start = time.time()
timeout = kwargs.pop("timeout", None)
while True:
try:
return await super().acreate(*args, **kwargs)
except TryAgain as e:
if timeout is not None and time.time() > start + timeout:
raise
util.log_info("Waiting for model to warm up", error=e)