Detailed explanation of problems encountered in calling the API interface of ChatGpt

        When calling the API interface of ChatGpt, due to the need to surf the Internet scientifically, some problems will be encountered. According to the purpose of calling the API interface, it can be divided into two categories:

1. Call the API to build a dialogue robot

        报错:openai.error.APIConnectionError: Error communicating with OpenAI: HTTPSConnectionPool(host='api.openai.com', port=443): Max retries exceeded with url: /v1/engines/text-davinci-002/completions (Caused by SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1129)')))

        Use the interface packaged by others:

parameter name type length must Remark
apiKey String 64 yes OpenAI's ApiKey
sessionId String 64 yes Session ID, associated context, it is recommended to use UUID as sessionId
content String 1000 yes sent content

        The code example is as follows, just change the value at 'apikey' to your own apikey.

2. Call the API interface and use the GPT3 or GPT4 algorithm to process related NLP tasks

        Take the concept extraction task as an example:

        问题:openai.error.APIConnectionError: Error communicating with OpenAI: HTTPSConnectionPool(host='api.openai.com', port=443): Max retries exceeded with url: /v1/engines/text-davinci-002/completions (Caused by SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1129)')))

        Solution: Use a proxy to solve this problem

        The code example is as follows:

your_proxy_addressHere you need to replace and         with your own actual proxy address and port your_proxy_port. This way, your request will be sent through the specified proxy server. 

        In this case, since the OpenAI library has no api_clientattributes, an error attributeError: module 'openai' has no attribute 'api_client' will be reported. In this case, you need to create a openai.api_resources.completion.Completionnew class inherited from and rewrite _createthe method. In this new _createmethod, proxy settings can be specified. code show as below:

 

         Same as above, just replace the proxy address and port in proxies with your own. If the error is still reported at this time: openai.error.APIConnectionError: Error communicating with OpenAI: HTTPSConnectionPool(host='api.openai.com', port=443): Max retries exceeded with url: /v1/engines/text-davinci-002 /completions (Caused by SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1129)')))

         Then the problem is probably the version of urllib3, and the problem will be solved by returning the version of urllib3 to version 1.25.11.

 The result of the operation is as follows:

 

Reference link:

OpenAI API proxy (openai-proxy.com)

Solve the problem of openai.error.APIConnectionError when Dai Li is connected to OpenAI API - Zhihu.com

Guess you like

Origin blog.csdn.net/qq_43704127/article/details/129740456