1、安裝虛擬環(huán)境
conda create --name OPENAI python ==3.7
注意,一定要python的版本 一定要 3.9及以下??!
否則一直會(huì)出錯(cuò)
出錯(cuò)
ImportError: cannot import name ‘COMMON_SAFE_ASCII_CHARACTERS’ from ‘charset_normalizer.constant’ (D:\anaconda3\envs\AIGC\lib\site-packages\charset_normalizer\constant.py)
用
pip install chardet
pip install charset-normalizer==2.1.0
都不管用
要確保python的版本號(hào)
最好python>=3.8 否則openai的有些庫(kù)不能用
conda install python==3.8
2、安裝openai
又是出錯(cuò)
ERROR: Could not find a version that satisfies the requirement openai (from versions: none) ERROR: No matching distribution found for openai
這時(shí)候用國(guó)內(nèi)源就好了
pip install openai -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
3、連接不上
一直出現(xiàn)問(wèn)題
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('Cannot connect to proxy.', OSError(0, 'Error')))
后來(lái)在知乎回答—— 找到了答案
原因是
urllib3的版本問(wèn)題,環(huán)境內(nèi)的urllib3版本是1.26.14版本,回退到1.25.11版本問(wèn)題就解決了。
檢查了一下urllib3的更新日志,應(yīng)該是1.26.0的修改導(dǎo)致的:
最后
conda install urllib3==1.25.11
4、一直顯示無(wú)chatCompletion
即顯示
AttributeError: module 'openai' has no attribute 'ChatCompletion'
openai的版本號(hào)不夠新,參照2進(jìn)行更新
pip install -U openai -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
能跑通啦,棒棒!?。?!
5、no model named win32api
pip install win32api
ERROR: Could not find a version that satisfies the requirement win32api (from versions: none)
ERROR: No matching distribution found for win32api
實(shí)際的安裝方法
conda install win32api
6、連接錯(cuò)誤
Error communicating with OpenAI: HTTPSConnectionPool(host=‘a(chǎn)pi.openai.com’, port=443): Max retries exceeded with url: /v1/chat/completions (Caused by ProxyError(‘Cannot connect to proxy.’, NewConnectionError(‘<urllib3.connection.HTTPSConnection object at 0x0000027039BA40A0>: Failed to establish a new connection: [WinError 10061] 由于目標(biāo)計(jì)算機(jī)積極拒絕,無(wú)法連接?!?))
解決方法
解決辦法:
①打開文件路徑:D:\Anaconda3\envs\py\Lib\site-packages\openai\api_reuqestor.py(openai庫(kù)路徑)
②找到if not hasattr(_thread_context,“session”): 方法,并在此方法上加入代理。(此方法大概在512行)
每一個(gè)人的代理不一定一樣
# proxy = {
# 'http': 'http://<代理ip>:<代理端口>',
# 'https': 'https://<代理ip>:<代理端口>'
# }
proxy={
'http':'127.0.0.1:10809',
'https':'127.0.0.1:10809'
}
③在result = _thread_context.session.request(中加入代理文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-465678.html
result = _thread_context.session.request(
method,
abs_url,
headers=headers,
data=data,
files=files,
stream=stream,
timeout=request_timeout if request_timeout else TIMEOUT_SECS,
proxies=proxy # 新增此行
)
再次運(yùn)行API測(cè)試代碼發(fā)現(xiàn)已經(jīng)成功返回?cái)?shù)據(jù)文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-465678.html
到了這里,關(guān)于python 安裝openai的踩坑史的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!