Esempio n. 1
0
 def _handle_request_error(self, e):
     msg = (
         "Unexpected error communicating with OpenAI. "
         "If this problem persists, let us know at [email protected]."
     )
     msg = textwrap.fill(msg) + "\n\n(Network error: " + str(e) + ")"
     raise error.APIConnectionError(msg)
Esempio n. 2
0
    def _handle_request_error(self, e):
        if e.args[0] in [
            pycurl.E_COULDNT_CONNECT,
            pycurl.E_COULDNT_RESOLVE_HOST,
            pycurl.E_OPERATION_TIMEOUTED,
        ]:
            msg = (
                "Could not connect to OpenAI.  Please check your "
                "internet connection and try again.  If this problem "
                "persists, you should check OpenAI's service status at "
                "https://twitter.com/openaistatus, or let us know at "
                "[email protected]."
            )
            should_retry = True
        elif e.args[0] in [pycurl.E_SSL_CACERT, pycurl.E_SSL_PEER_CERTIFICATE]:
            msg = (
                "Could not verify OpenAI's SSL certificate.  Please make "
                "sure that your network is not intercepting certificates.  "
                "If this problem persists, let us know at "
                "[email protected]."
            )
            should_retry = False
        else:
            msg = (
                "Unexpected error communicating with OpenAI. If this "
                "problem persists, let us know at [email protected]."
            )
            should_retry = False

        msg = textwrap.fill(msg) + "\n\n(Network error: " + e.args[1] + ")"
        raise error.APIConnectionError(msg, should_retry=should_retry)
Esempio n. 3
0
    def _handle_request_error(self, e):
        # Catch SSL error first as it belongs to ConnectionError,
        # but we don't want to retry, unless it is caused by dropped
        # SSL connection
        if isinstance(e, requests.exceptions.SSLError):
            if 'ECONNRESET' not in repr(e):
                msg = (
                    "Could not verify OpenAI's SSL certificate.  Please make "
                    "sure that your network is not intercepting certificates.  "
                    "If this problem persists, let us know at "
                    "[email protected]."
                )
                should_retry = False
            else:
                msg = "Detected ECONNRESET, indicates a dropped SSL connection."
                should_retry = True
            err = "%s: %s" % (type(e).__name__, str(e))
        # Retry only timeout and connect errors; similar to urllib3 Retry
        elif isinstance(
            e, (requests.exceptions.Timeout, requests.exceptions.ConnectionError)
        ):
            msg = (
                "Unexpected error communicating with OpenAI.  "
                "If this problem persists, let us know at "
                "[email protected]."
            )
            err = "%s: %s" % (type(e).__name__, str(e))
            should_retry = True
        # Catch remaining request exceptions
        elif isinstance(e, requests.exceptions.RequestException):
            msg = (
                "Unexpected error communicating with OpenAI.  "
                "If this problem persists, let us know at "
                "[email protected]."
            )
            err = "%s: %s" % (type(e).__name__, str(e))
            should_retry = False
        else:
            msg = (
                "Unexpected error communicating with OpenAI. "
                "It looks like there's probably a configuration "
                "issue locally.  If this problem persists, let us "
                "know at [email protected]."
            )
            err = "A %s was raised" % (type(e).__name__,)
            if str(e):
                err += " with error message %s" % (str(e),)
            else:
                err += " with no error message"
            should_retry = False

        if isinstance(e, requests.RequestException):
            request = e.request  # type: requests.Request
            if request is not None:
                err += " (url=" + self._sanitized_url(request.url) + ")"

        msg = textwrap.fill(msg) + "\n\n(Network error: %s)" % (err,)
        raise error.APIConnectionError(msg, should_retry=should_retry)
Esempio n. 4
0
    def _handle_request_error(self, e, url):
        if isinstance(e, urlfetch.InvalidURLError):
            msg = (
                "The OpenAI library attempted to fetch an "
                "invalid URL (%r). This is likely due to a bug "
                "in the OpenAI Python bindings. Please let us know "
                "at [email protected]." % (url,)
            )
        elif isinstance(e, urlfetch.DownloadError):
            msg = "There was a problem retrieving data from OpenAI."
        elif isinstance(e, urlfetch.ResponseTooLargeError):
            msg = (
                "There was a problem receiving all of your data from "
                "OpenAI.  This is likely due to a bug in OpenAI. "
                "Please let us know at [email protected]."
            )
        else:
            msg = (
                "Unexpected error communicating with OpenAI. If this "
                "problem persists, let us know at [email protected]."
            )

        msg = textwrap.fill(msg) + "\n\n(Network error: " + str(e) + ")"
        raise error.APIConnectionError(msg)
Esempio n. 5
0
    def request_raw(
        self,
        method,
        url,
        params=None,
        supplied_headers=None,
        files=None,
        stream=False,
        request_id: Optional[str] = None,
    ) -> requests.Response:
        abs_url = "%s%s" % (self.api_base, url)
        headers = {}

        data = None
        if method == "get" or method == "delete":
            if params:
                encoded_params = urlencode([(k, v) for k, v in params.items()
                                            if v is not None])
                abs_url = _build_api_url(abs_url, encoded_params)
        elif method in {"post", "put"}:
            if params and files:
                raise ValueError(
                    "At most one of params and files may be specified.")
            if params:
                data = json.dumps(params).encode()
                headers["Content-Type"] = "application/json"
        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(method, headers, request_id)
        if supplied_headers is not None:
            headers.update(supplied_headers)

        util.log_info("Request to OpenAI API", method=method, path=abs_url)
        util.log_debug("Post details", data=data, api_version=self.api_version)

        if not hasattr(_thread_context, "session"):
            _thread_context.session = _make_session()
        try:
            result = _thread_context.session.request(
                method,
                abs_url,
                headers=headers,
                data=data,
                files=files,
                stream=stream,
                timeout=TIMEOUT_SECS,
            )
        except requests.exceptions.RequestException as e:
            raise error.APIConnectionError(
                "Error communicating with OpenAI") from e
        util.log_info(
            "OpenAI API response",
            path=abs_url,
            response_code=result.status_code,
            processing_ms=result.headers.get("OpenAI-Processing-Ms"),
        )
        # Don't read the whole stream for debug logging unless necessary.
        if openai.log == "debug":
            util.log_debug("API response body",
                           body=result.content,
                           headers=result.headers)
        return result
Esempio n. 6
0
    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