場景復(fù)現(xiàn)
代碼如下(源自網(wǎng)絡(luò)):
import openai
openai.api_key = 'sk-xxxx'
def chat_gpt(prompt):
prompt = prompt
model_engine = "text-davinci-003"
completion = openai.Completion.create(
engine=model_engine,
prompt=prompt,
max_tokens=1024,
n=1,
stop=None,
temperature=0.5,
timeout=1000,
)
response = completion.choices[0].text
print(response)
chat_gpt("現(xiàn)在幾點了")
執(zhí)行報錯:
APIConnectionError: Error communicating with OpenAI: HTTPSConnectionPool(host='api.openai.com', port=443): Max retries exceeded with url: /v1/chat/completions (Caused by SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1125)')))
解決辦法
1、urllib3降低版本到1.25.11
pip install urllib3==1.25.11
參考大佬文章:傳送門
簡單來說就是1.26.0版本的urllib3添加了HTTPS支持,但代理服務(wù)器不支持HTTPS,所以報錯(pip走代理報錯也差不多類似原因,具體請參考上文,有詳細解讀)
這個方法對部分人有用,但很不幸我是另一部分(哭)!
2、修改openai源碼(親測成功)
查看堆棧找到報錯庫的路徑:
?打開api_requestor.py,找到:
if not hasattr(_thread_context, "session")
分別在它上面和下面添加以下兩行:
proxy = {
'http': 'http://localhost:7890',
'https': 'http://localhost:7890'
}
proxies=proxy
?保存搞定,重新運行代碼:
3、代碼里設(shè)置環(huán)境變量(親測成功)
添加以下代碼:文章來源:http://www.zghlxwxcb.cn/news/detail-458557.html
import os
os.environ["http_proxy"] = "http://localhost:7890"
os.environ["https_proxy"] = "http://localhost:7890"
以上解決辦法均來自網(wǎng)絡(luò),我只是個搬運工兼匯總~文章來源地址http://www.zghlxwxcb.cn/news/detail-458557.html
到了這里,關(guān)于OpenAI調(diào)用API報錯 time out:HTTPSConnectionPool(host=‘a(chǎn)pi.openai.com‘, port=443)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!