openai / openai-python

The official Python library for the OpenAI API
https://pypi.org/project/openai/
Apache License 2.0
21.81k stars 2.99k forks source link

Questions about using openai libraries and network proxies together #463

Closed Cruzxu2008 closed 9 months ago

Cruzxu2008 commented 1 year ago

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

OPEN-9 commented 11 months ago

1、

import os os.environ["http_proxy"] = "http://127.0.0.1:7890" os.environ["https_proxy"] = "http://127.0.0.1:7890" image

AND

pip3 uninstall urllib3 pip3 install urllib3==1.25.11 image

2、

Also check whether vpn is blocked by open ai

RobertCraigie commented 9 months ago

I believe the cause of this issue is that you're in a country that is not supported by OpenAI. Unfortunately there isn't much we can do about this.

Here's a list of the countries currently supported: https://platform.openai.com/docs/supported-countries.

Please feel free to ask any more questions in the community forum! https://community.openai.com/