1. 报错
raise error.APIConnectionError(
openai.error.APIConnectionError: Error communicating with OpenAI: HTTPSConnectionPool(host=‘api.openai.com’, port=443): Max retries exceeded with url: /v1/audio/transcriptions (Caused by SSLError(“Can’t connect to HTTPS URL because the SSL module is not available.”))
2. 解决方法
- 文件路径:./envs/虚拟环境名称/Lib/site-packages/openai/api_requestor.py(openai库路径)
- 找到
if not hasattr(_thread_context, "session"):
方法,加入代理:
proxy = {
'http': 'http://localhost:7890',
'https': 'http://localhost:7890'
}
- 在
result = _thread_context.session.request(
中加入代理:
proxies=proxy,
- 主文件中配置环境
import os
os.environ["http_proxy"] = "http://localhost:7890"
os.environ["https_proxy"] = "http://localhost:7890"
3. 测试案例
import os
os.environ["http_proxy"] = "http://localhost:7890"
os.environ["https_proxy"] = "http://localhost:7890"
import openai
from langchain.llms import OpenAI
llm = OpenAI(temperature=0.9)
text = "Who won the world series in 2020?"
print(llm(text))