openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
src/openai/lib/_old_api.py
72lines · modecode
| 1 | from __future__ import annotations |
| 2 | |
| 3 | from typing import TYPE_CHECKING, Any |
| 4 | from typing_extensions import override |
| 5 | |
| 6 | from .._utils import LazyProxy |
| 7 | from .._exceptions import OpenAIError |
| 8 | |
| 9 | INSTRUCTIONS = """ |
| 10 | |
| 11 | You tried to access openai.{symbol}, but this is no longer supported in openai>=1.0.0 - see the README at https://github.com/openai/openai-python for the API. |
| 12 | |
| 13 | You can run `openai migrate` to automatically upgrade your codebase to use the 1.0.0 interface. |
| 14 | |
| 15 | Alternatively, you can pin your installation to the old version, e.g. `pip install openai==0.28` |
| 16 | |
| 17 | A detailed migration guide is available here: https://github.com/openai/openai-python/discussions/742 |
| 18 | """ |
| 19 | |
| 20 | |
| 21 | class APIRemovedInV1(OpenAIError): |
| 22 | def __init__(self, *, symbol: str) -> None: |
| 23 | super().__init__(INSTRUCTIONS.format(symbol=symbol)) |
| 24 | |
| 25 | |
| 26 | class APIRemovedInV1Proxy(LazyProxy[Any]): |
| 27 | def __init__(self, *, symbol: str) -> None: |
| 28 | super().__init__() |
| 29 | self._symbol = symbol |
| 30 | |
| 31 | @override |
| 32 | def __load__(self) -> Any: |
| 33 | # return the proxy until it is eventually called so that |
| 34 | # we don't break people that are just checking the attributes |
| 35 | # of a module |
| 36 | return self |
| 37 | |
| 38 | def __call__(self, *_args: Any, **_kwargs: Any) -> Any: |
| 39 | raise APIRemovedInV1(symbol=self._symbol) |
| 40 | |
| 41 | |
| 42 | SYMBOLS = [ |
| 43 | "Edit", |
| 44 | "File", |
| 45 | "Audio", |
| 46 | "Image", |
| 47 | "Model", |
| 48 | "Engine", |
| 49 | "Customer", |
| 50 | "FineTune", |
| 51 | "Embedding", |
| 52 | "Completion", |
| 53 | "Deployment", |
| 54 | "Moderation", |
| 55 | "ErrorObject", |
| 56 | "FineTuningJob", |
| 57 | "ChatCompletion", |
| 58 | ] |
| 59 | |
| 60 | # we explicitly tell type checkers that nothing is exported |
| 61 | # from this file so that when we re-export the old symbols |
| 62 | # in `openai/__init__.py` they aren't added to the auto-complete |
| 63 | # suggestions given by editors |
| 64 | if TYPE_CHECKING: |
| 65 | __all__: list[str] = [] |
| 66 | else: |
| 67 | __all__ = SYMBOLS |
| 68 | |
| 69 | |
| 70 | __locals = locals() |
| 71 | for symbol in SYMBOLS: |
| 72 | __locals[symbol] = APIRemovedInV1Proxy(symbol=symbol) |
| 73 | |