openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
openai/api_resources/abstract/api_resource.py
117lines · modecode
| 1 | from urllib.parse import quote_plus |
| 2 | |
| 3 | import openai |
| 4 | from openai import api_requestor, error, util |
| 5 | from openai.openai_object import OpenAIObject |
| 6 | from openai.util import ApiType |
| 7 | |
| 8 | |
| 9 | class APIResource(OpenAIObject): |
| 10 | api_prefix = "" |
| 11 | azure_api_prefix = "openai" |
| 12 | azure_deployments_prefix = "deployments" |
| 13 | |
| 14 | @classmethod |
| 15 | def retrieve(cls, id, api_key=None, request_id=None, **params): |
| 16 | instance = cls(id, api_key, **params) |
| 17 | instance.refresh(request_id=request_id) |
| 18 | return instance |
| 19 | |
| 20 | def refresh(self, request_id=None): |
| 21 | self.refresh_from( |
| 22 | self.request("get", self.instance_url(), request_id=request_id) |
| 23 | ) |
| 24 | return self |
| 25 | |
| 26 | @classmethod |
| 27 | def class_url(cls): |
| 28 | if cls == APIResource: |
| 29 | raise NotImplementedError( |
| 30 | "APIResource is an abstract class. You should perform actions on its subclasses." |
| 31 | ) |
| 32 | # Namespaces are separated in object names with periods (.) and in URLs |
| 33 | # with forward slashes (/), so replace the former with the latter. |
| 34 | base = cls.OBJECT_NAME.replace(".", "/") # type: ignore |
| 35 | if cls.api_prefix: |
| 36 | return "/%s/%ss" % (cls.api_prefix, base) |
| 37 | return "/%ss" % (base) |
| 38 | |
| 39 | def instance_url(self, operation=None): |
| 40 | id = self.get("id") |
| 41 | |
| 42 | if not isinstance(id, str): |
| 43 | raise error.InvalidRequestError( |
| 44 | "Could not determine which URL to request: %s instance " |
| 45 | "has invalid ID: %r, %s. ID should be of type `str` (or" |
| 46 | " `unicode`)" % (type(self).__name__, id, type(id)), |
| 47 | "id", |
| 48 | ) |
| 49 | api_version = self.api_version or openai.api_version |
| 50 | extn = quote_plus(id) |
| 51 | |
| 52 | if self.typed_api_type == ApiType.AZURE: |
| 53 | if not api_version: |
| 54 | raise error.InvalidRequestError( |
| 55 | "An API version is required for the Azure API type." |
| 56 | ) |
| 57 | |
| 58 | if not operation: |
| 59 | base = self.class_url() |
| 60 | return "/%s%s/%s?api-version=%s" % ( |
| 61 | self.azure_api_prefix, |
| 62 | base, |
| 63 | extn, |
| 64 | api_version |
| 65 | ) |
| 66 | |
| 67 | return "/%s/%s/%s/%s?api-version=%s" % ( |
| 68 | self.azure_api_prefix, |
| 69 | self.azure_deployments_prefix, |
| 70 | extn, |
| 71 | operation, |
| 72 | api_version |
| 73 | ) |
| 74 | |
| 75 | |
| 76 | elif self.typed_api_type == ApiType.OPEN_AI: |
| 77 | base = self.class_url() |
| 78 | return "%s/%s" % (base, extn) |
| 79 | |
| 80 | else: |
| 81 | raise error.InvalidAPIType("Unsupported API type %s" % self.api_type) |
| 82 | |
| 83 | # The `method_` and `url_` arguments are suffixed with an underscore to |
| 84 | # avoid conflicting with actual request parameters in `params`. |
| 85 | @classmethod |
| 86 | def _static_request( |
| 87 | cls, |
| 88 | method_, |
| 89 | url_, |
| 90 | api_key=None, |
| 91 | api_base=None, |
| 92 | api_type=None, |
| 93 | request_id=None, |
| 94 | api_version=None, |
| 95 | organization=None, |
| 96 | **params, |
| 97 | ): |
| 98 | requestor = api_requestor.APIRequestor( |
| 99 | api_key, |
| 100 | api_version=api_version, |
| 101 | organization=organization, |
| 102 | api_base=api_base, |
| 103 | api_type=api_type |
| 104 | ) |
| 105 | response, _, api_key = requestor.request( |
| 106 | method_, url_, params, request_id=request_id |
| 107 | ) |
| 108 | return util.convert_to_openai_object( |
| 109 | response, api_key, api_version, organization |
| 110 | ) |
| 111 | |
| 112 | @classmethod |
| 113 | def _get_api_type_and_version(cls, api_type: str, api_version: str): |
| 114 | typed_api_type = ApiType.from_str(api_type) if api_type else ApiType.from_str(openai.api_type) |
| 115 | typed_api_version = api_version or openai.api_version |
| 116 | return (typed_api_type, typed_api_version) |