_old_api.py 1.9 KB

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