openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
openai/api_resources/abstract/engine_api_resource.py
130lines · modecode
| 1 | import time |
| 2 | from typing import Optional |
| 3 | from urllib.parse import quote_plus |
| 4 | |
| 5 | from openai import api_requestor, error, util |
| 6 | from openai.api_resources.abstract.api_resource import APIResource |
| 7 | |
| 8 | MAX_TIMEOUT = 20 |
| 9 | |
| 10 | |
| 11 | class EngineAPIResource(APIResource): |
| 12 | engine_required = True |
| 13 | plain_old_data = False |
| 14 | |
| 15 | def __init__(self, engine: Optional[str] = None, **kwargs): |
| 16 | super().__init__(engine=engine, **kwargs) |
| 17 | |
| 18 | @classmethod |
| 19 | def class_url(cls, engine: Optional[str] = None): |
| 20 | # Namespaces are separated in object names with periods (.) and in URLs |
| 21 | # with forward slashes (/), so replace the former with the latter. |
| 22 | base = cls.OBJECT_NAME.replace(".", "/") # type: ignore |
| 23 | if engine is None: |
| 24 | return "/%s/%ss" % (cls.api_prefix, base) |
| 25 | |
| 26 | extn = quote_plus(engine) |
| 27 | return "/%s/engines/%s/%ss" % (cls.api_prefix, extn, base) |
| 28 | |
| 29 | @classmethod |
| 30 | def create( |
| 31 | cls, |
| 32 | api_key=None, |
| 33 | api_base=None, |
| 34 | idempotency_key=None, |
| 35 | request_id=None, |
| 36 | api_version=None, |
| 37 | organization=None, |
| 38 | **params, |
| 39 | ): |
| 40 | engine = params.pop("engine", None) |
| 41 | timeout = params.pop("timeout", None) |
| 42 | stream = params.get("stream", False) |
| 43 | if engine is None and cls.engine_required: |
| 44 | raise error.InvalidRequestError( |
| 45 | "Must provide an 'engine' parameter to create a %s" % cls, "engine" |
| 46 | ) |
| 47 | |
| 48 | if timeout is None: |
| 49 | # No special timeout handling |
| 50 | pass |
| 51 | elif timeout > 0: |
| 52 | # API only supports timeouts up to MAX_TIMEOUT |
| 53 | params["timeout"] = min(timeout, MAX_TIMEOUT) |
| 54 | timeout = (timeout - params["timeout"]) or None |
| 55 | elif timeout == 0: |
| 56 | params["timeout"] = MAX_TIMEOUT |
| 57 | |
| 58 | requestor = api_requestor.APIRequestor( |
| 59 | api_key, |
| 60 | api_base=api_base, |
| 61 | api_version=api_version, |
| 62 | organization=organization, |
| 63 | ) |
| 64 | url = cls.class_url(engine) |
| 65 | headers = util.populate_headers(idempotency_key, request_id) |
| 66 | response, _, api_key = requestor.request( |
| 67 | "post", url, params, headers, stream=stream |
| 68 | ) |
| 69 | |
| 70 | if stream: |
| 71 | return ( |
| 72 | util.convert_to_openai_object( |
| 73 | line, |
| 74 | api_key, |
| 75 | api_version, |
| 76 | organization, |
| 77 | engine=engine, |
| 78 | plain_old_data=cls.plain_old_data, |
| 79 | ) |
| 80 | for line in response |
| 81 | ) |
| 82 | else: |
| 83 | obj = util.convert_to_openai_object( |
| 84 | response, |
| 85 | api_key, |
| 86 | api_version, |
| 87 | organization, |
| 88 | engine=engine, |
| 89 | plain_old_data=cls.plain_old_data, |
| 90 | ) |
| 91 | |
| 92 | if timeout is not None: |
| 93 | obj.wait(timeout=timeout or None) |
| 94 | |
| 95 | return obj |
| 96 | |
| 97 | def instance_url(self): |
| 98 | id = self.get("id") |
| 99 | |
| 100 | if not isinstance(id, str): |
| 101 | raise error.InvalidRequestError( |
| 102 | "Could not determine which URL to request: %s instance " |
| 103 | "has invalid ID: %r, %s. ID should be of type `str` (or" |
| 104 | " `unicode`)" % (type(self).__name__, id, type(id)), |
| 105 | "id", |
| 106 | ) |
| 107 | |
| 108 | base = self.class_url(self.engine) |
| 109 | extn = quote_plus(id) |
| 110 | url = "%s/%s" % (base, extn) |
| 111 | |
| 112 | timeout = self.get("timeout") |
| 113 | if timeout is not None: |
| 114 | timeout = quote_plus(str(timeout)) |
| 115 | url += "?timeout={}".format(timeout) |
| 116 | return url |
| 117 | |
| 118 | def wait(self, timeout=None): |
| 119 | start = time.time() |
| 120 | while self.status != "complete": |
| 121 | self.timeout = ( |
| 122 | min(timeout + start - time.time(), MAX_TIMEOUT) |
| 123 | if timeout is not None |
| 124 | else MAX_TIMEOUT |
| 125 | ) |
| 126 | if self.timeout < 0: |
| 127 | del self.timeout |
| 128 | break |
| 129 | self.refresh() |
| 130 | return self |
| 131 | |