-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Description
Describe the bug
I have successfully installed the openai-python library. When I copy the following code from README.md, I get an error when trying to run the program.
Code content:
import openai
with open('.\config\config.json', 'r', encoding='utf-8') as f:
config = json.load(f)
openai.api_key = config['api-key']
chat_completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hello world"}])
print(chat_completion)
Note: Due to relevant confidentiality requirements, I save my api-key in my local json file, and after testing, my code can normally read the api-key stored in the json file
Error content:
ERROR: APIConnectionError
Error communicating with OpenAI: HTTPSConnectionPool(host='api.openai.com', port=443): Max retries exceeded with url: /v1/chat/completions (Caused by ProxyError('Unable to connect to proxy', SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1129)'))))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='api.openai.com', port=443): Max retries exceeded with url: /v1/chat/completions (Caused by ProxyError('Unable to connect to proxy', SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1129)'))))
During handling of the above exception, another exception occurred:
requests.exceptions.ProxyError: HTTPSConnectionPool(host='api.openai.com', port=443): Max retries exceeded with url: /v1/chat/completions (Caused by ProxyError('Unable to connect to proxy', SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1129)'))))
The above exception was the direct cause of the following exception:
File "C:\Users\xupei\Desktop\ChatBot(GPT3.5turbo)\main.py", line 32, in <module>
chat_completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hello world"}])
openai.error.APIConnectionError: Error communicating with OpenAI: HTTPSConnectionPool(host='api.openai.com', port=443): Max retries exceeded with url: /v1/chat/completions (Caused by ProxyError('Unable to connect to proxy', SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1129)'))))
After repeated attempts, I found that it was because I was using a network proxy. So I tried shutting down the network agent and rerun the code. But after a long waiting period, the error was still reported.
Error content:
Error communicating with OpenAI: HTTPSConnectionPool(host='api.openai.com', port=443): Max retries exceeded with url: /v1/chat/completions (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x000002ABD10FB130>: Failed to establish a new connection: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。'))
TimeoutError: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。
The above exception was the direct cause of the following exception:
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPSConnection object at 0x000002ABD10FB130>: Failed to establish a new connection: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。
The above exception was the direct cause of the following exception:
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='api.openai.com', port=443): Max retries exceeded with url: /v1/chat/completions (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x000002ABD10FB130>: Failed to establish a new connection: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。'))
During handling of the above exception, another exception occurred:
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='api.openai.com', port=443): Max retries exceeded with url: /v1/chat/completions (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x000002ABD10FB130>: Failed to establish a new connection: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。'))
The above exception was the direct cause of the following exception:
File "C:\Users\xupei\Desktop\ChatBot(GPT3.5turbo)\main.py", line 32, in <module>
chat_completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hello world"}])
openai.error.APIConnectionError: Error communicating with OpenAI: HTTPSConnectionPool(host='api.openai.com', port=443): Max retries exceeded with url: /v1/chat/completions (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x000002ABD10FB130>: Failed to establish a new connection: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。'))
The connection to the host timed out or failed to connect. This is due to the fact that my country and region cannot connect to
openai's server without network proxy.
I really hope this problem can be solved and I am looking forward to your reply.
To Reproduce
1.edit code
code content:
import openai
with open('.\config\config.json', 'r', encoding='utf-8') as f:
config = json.load(f)
openai.api_key = config['api-key']
chat_completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hello world"}])
print(chat_completion)
Note: Due to relevant confidentiality requirements, I save my api-key in my local json file, and after testing, my code can normally read the api-key stored in the json file
2.run
3.see error
Code snippets
No response
OS
Windows11
Python version
Python 3.9.10
Library version
openai-python v0.27.6