Skip to content

Commit bd269ab

Browse files
authored
Speed up imports (#386)
1 parent cf03fe1 commit bd269ab

File tree

9 files changed

+58
-42
lines changed

9 files changed

+58
-42
lines changed

openai/__init__.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,26 @@
33
# Originally forked from the MIT-licensed Stripe Python bindings.
44

55
import os
6+
import sys
7+
from typing import TYPE_CHECKING, Optional
8+
69
from contextvars import ContextVar
7-
from typing import Optional, TYPE_CHECKING
10+
11+
if "pkg_resources" not in sys.modules:
12+
# workaround for the following:
13+
# https://github.com/benoitc/gunicorn/pull/2539
14+
sys.modules["pkg_resources"] = object() # type: ignore[assignment]
15+
import aiohttp
16+
17+
del sys.modules["pkg_resources"]
818

919
from openai.api_resources import (
1020
Audio,
1121
ChatCompletion,
1222
Completion,
1323
Customer,
14-
Edit,
1524
Deployment,
25+
Edit,
1626
Embedding,
1727
Engine,
1828
ErrorObject,

openai/api_resources/embedding.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import base64
22
import time
33

4-
54
from openai import util
65
from openai.api_resources.abstract.engine_api_resource import EngineAPIResource
7-
from openai.datalib import numpy as np, assert_has_numpy
6+
from openai.datalib.numpy_helper import assert_has_numpy
7+
from openai.datalib.numpy_helper import numpy as np
88
from openai.error import TryAgain
99

1010

openai/datalib.py renamed to openai/datalib/common.py

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,6 @@
1313
See also `setup.py`.
1414
1515
"""
16-
try:
17-
import numpy
18-
except ImportError:
19-
numpy = None
20-
21-
try:
22-
import pandas
23-
except ImportError:
24-
pandas = None
25-
26-
HAS_NUMPY = bool(numpy)
27-
HAS_PANDAS = bool(pandas)
28-
2916
INSTRUCTIONS = """
3017
3118
OpenAI error:
@@ -39,18 +26,7 @@
3926
"""
4027

4128
NUMPY_INSTRUCTIONS = INSTRUCTIONS.format(library="numpy")
42-
PANDAS_INSTRUCTIONS = INSTRUCTIONS.format(library="pandas")
4329

4430

4531
class MissingDependencyError(Exception):
4632
pass
47-
48-
49-
def assert_has_numpy():
50-
if not HAS_NUMPY:
51-
raise MissingDependencyError(NUMPY_INSTRUCTIONS)
52-
53-
54-
def assert_has_pandas():
55-
if not HAS_PANDAS:
56-
raise MissingDependencyError(PANDAS_INSTRUCTIONS)

openai/datalib/numpy_helper.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from openai.datalib.common import INSTRUCTIONS, MissingDependencyError
2+
3+
try:
4+
import numpy
5+
except ImportError:
6+
numpy = None
7+
8+
HAS_NUMPY = bool(numpy)
9+
10+
NUMPY_INSTRUCTIONS = INSTRUCTIONS.format(library="numpy")
11+
12+
13+
def assert_has_numpy():
14+
if not HAS_NUMPY:
15+
raise MissingDependencyError(NUMPY_INSTRUCTIONS)

openai/datalib/pandas_helper.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from openai.datalib.common import INSTRUCTIONS, MissingDependencyError
2+
3+
try:
4+
import pandas
5+
except ImportError:
6+
pandas = None
7+
8+
HAS_PANDAS = bool(pandas)
9+
10+
PANDAS_INSTRUCTIONS = INSTRUCTIONS.format(library="pandas")
11+
12+
13+
def assert_has_pandas():
14+
if not HAS_PANDAS:
15+
raise MissingDependencyError(PANDAS_INSTRUCTIONS)

openai/embeddings_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
from tenacity import retry, stop_after_attempt, wait_random_exponential
1111

1212
import openai
13-
from openai.datalib import numpy as np
14-
from openai.datalib import pandas as pd
13+
from openai.datalib.numpy_helper import numpy as np
14+
from openai.datalib.pandas_helper import pandas as pd
1515

1616

1717
@retry(wait=wait_random_exponential(min=1, max=20), stop=stop_after_attempt(6))

openai/tests/test_long_examples_validator.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,8 @@
44

55
import pytest
66

7-
from openai.datalib import (
8-
HAS_NUMPY,
9-
HAS_PANDAS,
10-
NUMPY_INSTRUCTIONS,
11-
PANDAS_INSTRUCTIONS,
12-
)
7+
from openai.datalib.numpy_helper import HAS_NUMPY, NUMPY_INSTRUCTIONS
8+
from openai.datalib.pandas_helper import HAS_PANDAS, PANDAS_INSTRUCTIONS
139

1410

1511
@pytest.mark.skipif(not HAS_PANDAS, reason=PANDAS_INSTRUCTIONS)
@@ -54,5 +50,5 @@ def test_long_examples_validator() -> None:
5450
assert prepared_data_cmd_output.stderr == ""
5551
# validate get_long_indexes() applied during optional_fn() call in long_examples_validator()
5652
assert "indices of the long examples has changed" in prepared_data_cmd_output.stdout
57-
53+
5854
return prepared_data_cmd_output.stdout

openai/validators.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
import sys
33
from typing import Any, Callable, NamedTuple, Optional
44

5-
from openai.datalib import pandas as pd, assert_has_pandas
5+
from openai.datalib.pandas_helper import assert_has_pandas
6+
from openai.datalib.pandas_helper import pandas as pd
67

78

89
class Remediation(NamedTuple):
@@ -158,6 +159,7 @@ def long_examples_validator(df):
158159

159160
ft_type = infer_task_type(df)
160161
if ft_type != "open-ended generation":
162+
161163
def get_long_indexes(d):
162164
long_examples = d.apply(
163165
lambda x: len(x.prompt) + len(x.completion) > 10000, axis=1
@@ -171,10 +173,12 @@ def get_long_indexes(d):
171173
optional_msg = f"Remove {len(long_indexes)} long examples"
172174

173175
def optional_fn(x):
174-
176+
175177
long_indexes_to_drop = get_long_indexes(x)
176178
if long_indexes != long_indexes_to_drop:
177-
sys.stdout.write(f"The indices of the long examples has changed as a result of a previously applied recommendation.\nThe {len(long_indexes_to_drop)} long examples to be dropped are now at the following indices: {long_indexes_to_drop}\n")
179+
sys.stdout.write(
180+
f"The indices of the long examples has changed as a result of a previously applied recommendation.\nThe {len(long_indexes_to_drop)} long examples to be dropped are now at the following indices: {long_indexes_to_drop}\n"
181+
)
178182
return x.drop(long_indexes_to_drop)
179183

180184
return Remediation(

openai/wandb_logger.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
from pathlib import Path
1515

1616
from openai import File, FineTune
17-
from openai.datalib import numpy as np
18-
from openai.datalib import pandas as pd
17+
from openai.datalib.numpy_helper import numpy as np
18+
from openai.datalib.pandas_helper import pandas as pd
1919

2020

2121
class WandbLogger:

0 commit comments

Comments
 (0)