def request(self, method, url, headers, post_data=None, stream=False): if stream: raise NotImplementedError("Stream not yet implemented for {}".format(self)) if six.PY3 and isinstance(post_data, six.string_types): post_data = post_data.encode("utf-8") req = urllib.request.Request(url, post_data, headers) if method not in ("get", "post"): req.get_method = lambda: method.upper() try: # use the custom proxy tied opener, if any. # otherwise, fall to the default urllib opener. response = ( self._opener.open(req) if self._opener else urllib.request.urlopen(req) ) rbody = response.read() rcode = response.code headers = dict(response.info()) except urllib.error.HTTPError as e: rcode = e.code rbody = e.read() headers = dict(e.info()) except (urllib.error.URLError, ValueError) as e: self._handle_request_error(e) lh = dict((k.lower(), v) for k, v in six.iteritems(dict(headers))) return rbody, rcode, lh, stream
def request(self, method, url, headers, post_data=None, stream=False): if stream: raise NotImplementedError("Stream not yet implemented for {}".format(self)) b = util.io.BytesIO() rheaders = util.io.BytesIO() # Pycurl's design is a little weird: although we set per-request # options on this object, it's also capable of maintaining established # connections. Here we call reset() between uses to make sure it's in a # pristine state, but notably reset() doesn't reset connections, so we # still get to take advantage of those by virtue of re-using the same # object. self._curl.reset() proxy = self._get_proxy(url) if proxy: if proxy.hostname: self._curl.setopt(pycurl.PROXY, proxy.hostname) if proxy.port: self._curl.setopt(pycurl.PROXYPORT, proxy.port) if proxy.username or proxy.password: self._curl.setopt( pycurl.PROXYUSERPWD, "%s:%s" % (proxy.username, proxy.password) ) if method == "get": self._curl.setopt(pycurl.HTTPGET, 1) elif method == "post": self._curl.setopt(pycurl.POST, 1) self._curl.setopt(pycurl.POSTFIELDS, post_data) else: self._curl.setopt(pycurl.CUSTOMREQUEST, method.upper()) # pycurl doesn't like unicode URLs self._curl.setopt(pycurl.URL, util.utf8(url)) self._curl.setopt(pycurl.WRITEFUNCTION, b.write) self._curl.setopt(pycurl.HEADERFUNCTION, rheaders.write) self._curl.setopt(pycurl.NOSIGNAL, 1) self._curl.setopt(pycurl.CONNECTTIMEOUT, 30) self._curl.setopt(pycurl.TIMEOUT, 80) self._curl.setopt( pycurl.HTTPHEADER, ["%s: %s" % (k, v) for k, v in six.iteritems(dict(headers))], ) if self._verify_ssl_certs: self._curl.setopt(pycurl.CAINFO, openai.ca_bundle_path) else: self._curl.setopt(pycurl.SSL_VERIFYHOST, False) try: self._curl.perform() except pycurl.error as e: self._handle_request_error(e) rbody = b.getvalue().decode("utf-8") rcode = self._curl.getinfo(pycurl.RESPONSE_CODE) headers = self.parse_headers(rheaders.getvalue().decode("utf-8")) return rbody, rcode, headers, stream
def __deepcopy__(self, memo): copied = self.__copy__() memo[id(self)] = copied for k, v in six.iteritems(self): # Call parent's __setitem__ to avoid checks that we've added in the # overridden version that can throw exceptions. super(OpenAIObject, copied).__setitem__(k, deepcopy(v, memo)) return copied
def to_dict_recursive(self): d = dict(self) for k, v in six.iteritems(d): if isinstance(v, OpenAIObject): d[k] = v.to_dict_recursive() elif isinstance(v, list): d[k] = [ e.to_dict_recursive() if isinstance(e, OpenAIObject) else e for e in v ] return d
def __init__(self, verify_ssl_certs=True, proxy=None): super(PycurlClient, self).__init__( verify_ssl_certs=verify_ssl_certs, proxy=proxy ) # Initialize this within the object so that we can reuse connections. self._curl = pycurl.Curl() # need to urlparse the proxy, since PyCurl # consumes the proxy url in small pieces if self._proxy: # now that we have the parser, get the proxy url pieces proxy = self._proxy for scheme, value in six.iteritems(proxy): proxy[scheme] = urlparse(value)
def __copy__(self): copied = OpenAIObject( self.get("id"), self.api_key, api_version=self.api_version, organization=self.organization, ) copied._retrieve_params = self._retrieve_params for k, v in six.iteritems(self): # Call parent's __setitem__ to avoid checks that we've added in the # overridden version that can throw exceptions. super(OpenAIObject, copied).__setitem__(k, v) return copied
def convert_to_dict(obj): """Converts a OpenAIObject back to a regular dict. Nested OpenAIObjects are also converted back to regular dicts. :param obj: The OpenAIObject to convert. :returns: The OpenAIObject as a dict. """ if isinstance(obj, list): return [convert_to_dict(i) for i in obj] # This works by virtue of the fact that OpenAIObjects _are_ dicts. The dict # comprehension returns a regular dict and recursively applies the # conversion to each value. elif isinstance(obj, dict): return {k: convert_to_dict(v) for k, v in six.iteritems(obj)} else: return obj
def serialize(self, previous): params = {} unsaved_keys = self._unsaved_values or set() previous = previous or self._previous or {} for k, v in six.iteritems(self): if k == "id" or (isinstance(k, str) and k.startswith("_")): continue elif isinstance(v, openai.api_resources.abstract.APIResource): continue elif hasattr(v, "serialize"): child = v.serialize(previous.get(k, None)) if child != {}: params[k] = child elif k in unsaved_keys: params[k] = _compute_diff(v, previous.get(k, None)) elif k == "additional_owners" and v is not None: params[k] = _serialize_list(v, previous.get(k, None)) return params
def _api_encode(data): for key, value in six.iteritems(data): key = util.utf8(key) if value is None: continue elif hasattr(value, "openai_id"): yield (key, value.openai_id) elif isinstance(value, list) or isinstance(value, tuple): for i, sv in enumerate(value): if isinstance(sv, dict): subdict = _encode_nested_dict("%s[%d]" % (key, i), sv) for k, v in _api_encode(subdict): yield (k, v) else: yield ("%s[%d]" % (key, i), util.utf8(sv)) elif isinstance(value, dict): subdict = _encode_nested_dict(key, value) for subkey, subvalue in _api_encode(subdict): yield (subkey, subvalue) elif isinstance(value, datetime.datetime): yield (key, _encode_datetime(value)) else: yield (key, util.utf8(value))
def refresh_from( self, values, api_key=None, partial=False, api_version=None, organization=None, last_response=None, ): self.api_key = api_key or getattr(values, "api_key", None) self.api_version = api_version or getattr(values, "api_version", None) self.organization = organization or getattr(values, "organization", None) self._last_response = last_response or getattr(values, "_last_response", None) # Wipe old state before setting new. This is useful for e.g. # updating a customer, where there is no persistent card # parameter. Mark those values which don't persist as transient if partial: self._unsaved_values = self._unsaved_values - set(values) else: removed = set(self.keys()) - set(values) self._transient_values = self._transient_values | removed self._unsaved_values = set() self.clear() self._transient_values = self._transient_values - set(values) for k, v in six.iteritems(values): super(OpenAIObject, self).__setitem__( k, util.convert_to_openai_object(v, api_key, api_version, organization)) self._previous = values
def _encode_nested_dict(key, data, fmt="%s[%s]"): d = OrderedDict() for subkey, subvalue in six.iteritems(data): d[fmt % (key, subkey)] = subvalue return d
def request_raw( self, method, url, params=None, supplied_headers=None, stream=False ): """ Mechanism for issuing an API call """ if self.api_key: my_api_key = self.api_key else: from openai import api_key my_api_key = api_key if my_api_key is None: raise error.AuthenticationError( "No API key provided. (HINT: set your API key using in code using " '"openai.api_key = <API-KEY>", or you can set the environment variable OPENAI_API_KEY=<API-KEY>). You can generate API keys ' "in the OpenAI web interface. See https://onboard.openai.com " "for details, or email [email protected] if you have any " "questions." ) abs_url = "%s%s" % (self.api_base, url) headers = {} compress = None progress_meter = False if method == "get" or method == "delete": if params: encoded_params = url_encode_params(params) abs_url = _build_api_url(abs_url, encoded_params) else: encoded_params = None post_data = None elif method in {"post", "put"}: if ( supplied_headers is not None and supplied_headers.get("Content-Type") == "multipart/form-data" ): generator = MultipartDataGenerator() generator.add_params(params or {}) post_data = generator.get_post_data() content_type = "multipart/form-data; boundary=%s" % ( generator.boundary, ) # We will overrite Content-Type supplied_headers.pop("Content-Type") progress_meter = True # compress = "gzip" compress = None else: post_data = json.dumps(params).encode() content_type = "application/json" headers["Content-Type"] = content_type encoded_params = post_data if progress_meter: post_data = BufferReader(post_data, desc="Upload progress") if compress == "gzip": if not hasattr(post_data, "read"): post_data = BytesIO(post_data) headers["Content-Encoding"] = "gzip" from openai.gzip_stream import GZIPCompressedStream post_data = GZIPCompressedStream(post_data, compression_level=9) else: raise error.APIConnectionError( "Unrecognized HTTP method %r. This may indicate a bug in the " "OpenAI bindings. Please contact [email protected] for " "assistance." % (method,) ) headers = self.request_headers(my_api_key, method, headers) if supplied_headers is not None: for key, value in six.iteritems(supplied_headers): headers[key] = value util.log_info("Request to OpenAI API", method=method, path=abs_url) util.log_debug( "Post details", post_data=encoded_params, api_version=self.api_version ) rbody, rcode, rheaders, stream = self._client.request_with_retries( method, abs_url, headers, post_data, stream=stream ) util.log_info( "OpenAI API response", path=abs_url, response_code=rcode, processing_ms=rheaders.get("OpenAI-Processing-Ms"), ) util.log_debug("API response body", body=rbody, headers=rheaders) if "Request-Id" in rheaders: request_id = rheaders["Request-Id"] util.log_debug( "Dashboard link for request", link=util.dashboard_link(request_id) ) return rbody, rcode, rheaders, stream, my_api_key
def parse_headers(self, data): if "\r\n" not in data: return {} raw_headers = data.split("\r\n", 1)[1] headers = email.message_from_string(raw_headers) return dict((k.lower(), v) for k, v in six.iteritems(dict(headers)))