Skip to content

Commit b921cfc

Browse files
committed
formats by Black
1 parent c2a0eb4 commit b921cfc

14 files changed

+123
-79
lines changed

openai/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@
3232
organization = os.environ.get("OPENAI_ORGANIZATION")
3333
api_base = os.environ.get("OPENAI_API_BASE", "https://api.openai.com/v1")
3434
api_type = os.environ.get("OPENAI_API_TYPE", "open_ai")
35-
api_version = "2022-03-01-preview" if api_type in (
36-
"azure", "azure_ad", "azuread") else None
35+
api_version = (
36+
"2022-03-01-preview" if api_type in ("azure", "azure_ad", "azuread") else None
37+
)
3738
verify_ssl_certs = True # No effect. Certificates are always verified.
3839
proxy = None
3940
app_info = None

openai/api_resources/abstract/api_resource.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,24 +62,23 @@ def instance_url(self, operation=None):
6262
self.azure_api_prefix,
6363
base,
6464
extn,
65-
api_version
65+
api_version,
6666
)
6767

6868
return "/%s/%s/%s/%s?api-version=%s" % (
6969
self.azure_api_prefix,
7070
self.azure_deployments_prefix,
7171
extn,
7272
operation,
73-
api_version
73+
api_version,
7474
)
7575

7676
elif self.typed_api_type == ApiType.OPEN_AI:
7777
base = self.class_url()
7878
return "%s/%s" % (base, extn)
7979

8080
else:
81-
raise error.InvalidAPIType(
82-
"Unsupported API type %s" % self.api_type)
81+
raise error.InvalidAPIType("Unsupported API type %s" % self.api_type)
8382

8483
# The `method_` and `url_` arguments are suffixed with an underscore to
8584
# avoid conflicting with actual request parameters in `params`.
@@ -101,7 +100,7 @@ def _static_request(
101100
api_version=api_version,
102101
organization=organization,
103102
api_base=api_base,
104-
api_type=api_type
103+
api_type=api_type,
105104
)
106105
response, _, api_key = requestor.request(
107106
method_, url_, params, request_id=request_id
@@ -111,8 +110,13 @@ def _static_request(
111110
)
112111

113112
@classmethod
114-
def _get_api_type_and_version(cls, api_type: Optional[str] = None, api_version: Optional[str] = None):
115-
typed_api_type = ApiType.from_str(
116-
api_type) if api_type else ApiType.from_str(openai.api_type)
113+
def _get_api_type_and_version(
114+
cls, api_type: Optional[str] = None, api_version: Optional[str] = None
115+
):
116+
typed_api_type = (
117+
ApiType.from_str(api_type)
118+
if api_type
119+
else ApiType.from_str(openai.api_type)
120+
)
117121
typed_api_version = api_version or openai.api_version
118122
return (typed_api_type, typed_api_version)

openai/api_resources/abstract/createable_api_resource.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@ def create(
2525
organization=organization,
2626
)
2727
typed_api_type, api_version = cls._get_api_type_and_version(
28-
api_type, api_version)
28+
api_type, api_version
29+
)
2930

3031
if typed_api_type in (ApiType.AZURE, ApiType.AZURE_AD):
3132
base = cls.class_url()
32-
url = "/%s%s?api-version=%s" % (cls.azure_api_prefix,
33-
base, api_version)
33+
url = "/%s%s?api-version=%s" % (cls.azure_api_prefix, base, api_version)
3434
elif typed_api_type == ApiType.OPEN_AI:
3535
url = cls.class_url()
3636
else:
37-
raise error.InvalidAPIType('Unsupported API type %s' % api_type)
37+
raise error.InvalidAPIType("Unsupported API type %s" % api_type)
3838

3939
response, _, api_key = requestor.request(
4040
"post", url, params, request_id=request_id

openai/api_resources/abstract/deletable_api_resource.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,26 @@ class DeletableAPIResource(APIResource):
99
@classmethod
1010
def delete(cls, sid, api_type=None, api_version=None, **params):
1111
if isinstance(cls, APIResource):
12-
raise ValueError(
13-
".delete may only be called as a class method now.")
12+
raise ValueError(".delete may only be called as a class method now.")
1413

1514
base = cls.class_url()
1615
extn = quote_plus(sid)
1716

1817
typed_api_type, api_version = cls._get_api_type_and_version(
19-
api_type, api_version)
18+
api_type, api_version
19+
)
2020
if typed_api_type in (ApiType.AZURE, ApiType.AZURE_AD):
2121
url = "/%s%s/%s?api-version=%s" % (
22-
cls.azure_api_prefix, base, extn, api_version)
22+
cls.azure_api_prefix,
23+
base,
24+
extn,
25+
api_version,
26+
)
2327
elif typed_api_type == ApiType.OPEN_AI:
2428
url = "%s/%s" % (base, extn)
2529
else:
26-
raise error.InvalidAPIType('Unsupported API type %s' % api_type)
30+
raise error.InvalidAPIType("Unsupported API type %s" % api_type)
2731

28-
return cls._static_request("delete", url, api_type=api_type, api_version=api_version, **params)
32+
return cls._static_request(
33+
"delete", url, api_type=api_type, api_version=api_version, **params
34+
)

openai/api_resources/abstract/engine_api_resource.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ def class_url(
2929
# with forward slashes (/), so replace the former with the latter.
3030
base = cls.OBJECT_NAME.replace(".", "/") # type: ignore
3131
typed_api_type, api_version = cls._get_api_type_and_version(
32-
api_type, api_version)
32+
api_type, api_version
33+
)
3334

3435
if typed_api_type in (ApiType.AZURE, ApiType.AZURE_AD):
3536
if not api_version:
@@ -46,7 +47,7 @@ def class_url(
4647
cls.azure_deployments_prefix,
4748
extn,
4849
base,
49-
api_version
50+
api_version,
5051
)
5152

5253
elif typed_api_type == ApiType.OPEN_AI:
@@ -81,12 +82,16 @@ def create(
8182
if typed_api_type in (util.ApiType.AZURE, util.ApiType.AZURE_AD):
8283
if deployment_id is None and engine is None:
8384
raise error.InvalidRequestError(
84-
"Must provide an 'engine' or 'deployment_id' parameter to create a %s" % cls, "engine"
85+
"Must provide an 'engine' or 'deployment_id' parameter to create a %s"
86+
% cls,
87+
"engine",
8588
)
8689
else:
8790
if model is None and engine is None:
8891
raise error.InvalidRequestError(
89-
"Must provide an 'engine' or 'model' parameter to create a %s" % cls, "engine"
92+
"Must provide an 'engine' or 'model' parameter to create a %s"
93+
% cls,
94+
"engine",
9095
)
9196

9297
if timeout is None:
@@ -155,7 +160,7 @@ def instance_url(self):
155160
)
156161

157162
extn = quote_plus(id)
158-
params_connector = '?'
163+
params_connector = "?"
159164

160165
if self.typed_api_type in (ApiType.AZURE, ApiType.AZURE_AD):
161166
api_version = self.api_version or openai.api_version
@@ -170,17 +175,16 @@ def instance_url(self):
170175
self.engine,
171176
base,
172177
extn,
173-
api_version
178+
api_version,
174179
)
175-
params_connector = '&'
180+
params_connector = "&"
176181

177182
elif self.typed_api_type == ApiType.OPEN_AI:
178183
base = self.class_url(self.engine, self.api_type, self.api_version)
179184
url = "%s/%s" % (base, extn)
180185

181186
else:
182-
raise error.InvalidAPIType(
183-
"Unsupported API type %s" % self.api_type)
187+
raise error.InvalidAPIType("Unsupported API type %s" % self.api_type)
184188

185189
timeout = self.get("timeout")
186190
if timeout is not None:

openai/api_resources/abstract/listable_api_resource.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@ def list(
2828
)
2929

3030
typed_api_type, api_version = cls._get_api_type_and_version(
31-
api_type, api_version)
31+
api_type, api_version
32+
)
3233

3334
if typed_api_type in (ApiType.AZURE, ApiType.AZURE_AD):
3435
base = cls.class_url()
35-
url = "/%s%s?api-version=%s" % (cls.azure_api_prefix,
36-
base, api_version)
36+
url = "/%s%s?api-version=%s" % (cls.azure_api_prefix, base, api_version)
3737
elif typed_api_type == ApiType.OPEN_AI:
3838
url = cls.class_url()
3939
else:
40-
raise error.InvalidAPIType('Unsupported API type %s' % api_type)
40+
raise error.InvalidAPIType("Unsupported API type %s" % api_type)
4141

4242
response, _, api_key = requestor.request(
4343
"get", url, params, request_id=request_id

openai/api_resources/deployment.py

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
from openai import util
2-
from openai.api_resources.abstract import DeletableAPIResource, ListableAPIResource, CreateableAPIResource
2+
from openai.api_resources.abstract import (
3+
DeletableAPIResource,
4+
ListableAPIResource,
5+
CreateableAPIResource,
6+
)
37
from openai.error import InvalidRequestError, APIError
48

59

@@ -12,10 +16,12 @@ def create(cls, *args, **kwargs):
1216
Creates a new deployment for the provided prompt and parameters.
1317
"""
1418
typed_api_type, _ = cls._get_api_type_and_version(
15-
kwargs.get("api_type", None), None)
19+
kwargs.get("api_type", None), None
20+
)
1621
if typed_api_type not in (util.ApiType.AZURE, util.ApiType.AZURE_AD):
1722
raise APIError(
18-
"Deployment operations are only available for the Azure API type.")
23+
"Deployment operations are only available for the Azure API type."
24+
)
1925

2026
if kwargs.get("model", None) is None:
2127
raise InvalidRequestError(
@@ -30,8 +36,10 @@ def create(cls, *args, **kwargs):
3036
param="scale_settings",
3137
)
3238

33-
if "scale_type" not in scale_settings or \
34-
(scale_settings["scale_type"].lower() == 'manual' and "capacity" not in scale_settings):
39+
if "scale_type" not in scale_settings or (
40+
scale_settings["scale_type"].lower() == "manual"
41+
and "capacity" not in scale_settings
42+
):
3543
raise InvalidRequestError(
3644
"The 'scale_settings' parameter contains invalid or incomplete values.",
3745
param="scale_settings",
@@ -42,29 +50,35 @@ def create(cls, *args, **kwargs):
4250
@classmethod
4351
def list(cls, *args, **kwargs):
4452
typed_api_type, _ = cls._get_api_type_and_version(
45-
kwargs.get("api_type", None), None)
53+
kwargs.get("api_type", None), None
54+
)
4655
if typed_api_type not in (util.ApiType.AZURE, util.ApiType.AZURE_AD):
4756
raise APIError(
48-
"Deployment operations are only available for the Azure API type.")
57+
"Deployment operations are only available for the Azure API type."
58+
)
4959

5060
return super().list(*args, **kwargs)
5161

5262
@classmethod
5363
def delete(cls, *args, **kwargs):
5464
typed_api_type, _ = cls._get_api_type_and_version(
55-
kwargs.get("api_type", None), None)
65+
kwargs.get("api_type", None), None
66+
)
5667
if typed_api_type not in (util.ApiType.AZURE, util.ApiType.AZURE_AD):
5768
raise APIError(
58-
"Deployment operations are only available for the Azure API type.")
69+
"Deployment operations are only available for the Azure API type."
70+
)
5971

6072
return super().delete(*args, **kwargs)
6173

6274
@classmethod
6375
def retrieve(cls, *args, **kwargs):
6476
typed_api_type, _ = cls._get_api_type_and_version(
65-
kwargs.get("api_type", None), None)
77+
kwargs.get("api_type", None), None
78+
)
6679
if typed_api_type not in (util.ApiType.AZURE, util.ApiType.AZURE_AD):
6780
raise APIError(
68-
"Deployment operations are only available for the Azure API type.")
81+
"Deployment operations are only available for the Azure API type."
82+
)
6983

7084
return super().retrieve(*args, **kwargs)

openai/api_resources/edit.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ def create(cls, *args, **kwargs):
2020
typed_api_type = cls._get_api_type_and_version(api_type=api_type)[0]
2121
if typed_api_type in (util.ApiType.AZURE, util.ApiType.AZURE_AD):
2222
raise error.InvalidAPIType(
23-
"This operation is not supported by the Azure OpenAI API yet.")
23+
"This operation is not supported by the Azure OpenAI API yet."
24+
)
2425

2526
while True:
2627
try:

openai/api_resources/file.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ def create(
2525
user_provided_filename=None,
2626
):
2727
if purpose != "search" and model is not None:
28-
raise ValueError(
29-
"'model' is only meaningful if 'purpose' is 'search'")
28+
raise ValueError("'model' is only meaningful if 'purpose' is 'search'")
3029
requestor = api_requestor.APIRequestor(
3130
api_key,
3231
api_base=api_base or openai.api_base,
@@ -35,16 +34,16 @@ def create(
3534
organization=organization,
3635
)
3736
typed_api_type, api_version = cls._get_api_type_and_version(
38-
api_type, api_version)
37+
api_type, api_version
38+
)
3939

4040
if typed_api_type in (ApiType.AZURE, ApiType.AZURE_AD):
4141
base = cls.class_url()
42-
url = "/%s%s?api-version=%s" % (cls.azure_api_prefix,
43-
base, api_version)
42+
url = "/%s%s?api-version=%s" % (cls.azure_api_prefix, base, api_version)
4443
elif typed_api_type == ApiType.OPEN_AI:
4544
url = cls.class_url()
4645
else:
47-
raise error.InvalidAPIType('Unsupported API type %s' % api_type)
46+
raise error.InvalidAPIType("Unsupported API type %s" % api_type)
4847

4948
# Set the filename on 'purpose' and 'model' to None so they are
5049
# interpreted as form data.
@@ -53,9 +52,10 @@ def create(
5352
files.append(("model", (None, model)))
5453
if user_provided_filename is not None:
5554
files.append(
56-
("file", (user_provided_filename, file, 'application/octet-stream')))
55+
("file", (user_provided_filename, file, "application/octet-stream"))
56+
)
5757
else:
58-
files.append(("file", ("file", file, 'application/octet-stream')))
58+
files.append(("file", ("file", file, "application/octet-stream")))
5959
response, _, api_key = requestor.request("post", url, files=files)
6060
return util.convert_to_openai_object(
6161
response, api_key, api_version, organization
@@ -69,7 +69,7 @@ def download(
6969
api_base=None,
7070
api_type=None,
7171
api_version=None,
72-
organization=None
72+
organization=None,
7373
):
7474
requestor = api_requestor.APIRequestor(
7575
api_key,
@@ -79,16 +79,21 @@ def download(
7979
organization=organization,
8080
)
8181
typed_api_type, api_version = cls._get_api_type_and_version(
82-
api_type, api_version)
82+
api_type, api_version
83+
)
8384

8485
if typed_api_type in (ApiType.AZURE, ApiType.AZURE_AD):
8586
base = cls.class_url()
8687
url = "/%s%s/%s/content?api-version=%s" % (
87-
cls.azure_api_prefix, base, id, api_version)
88+
cls.azure_api_prefix,
89+
base,
90+
id,
91+
api_version,
92+
)
8893
elif typed_api_type == ApiType.OPEN_AI:
8994
url = f"{cls.class_url()}/{id}/content"
9095
else:
91-
raise error.InvalidAPIType('Unsupported API type %s' % api_type)
96+
raise error.InvalidAPIType("Unsupported API type %s" % api_type)
9297

9398
result = requestor.request_raw("get", url)
9499
if not 200 <= result.status_code < 300:

0 commit comments

Comments
 (0)