最近小伙伴们肯定发现了openai有了新版本1.3.3,许多接口已经和老版本大有不同。这里附上新接口的快速上手的文档链接:https://github.com/openai/openai-python/discussions/742
新版本放弃了使用requests库而是使用了httpx库进行接口请求。聊一下httpx:支持同步和异步http请求接口,而requests库只能同步请求,可以理解为强化版requests,能更好地支持异步调用。
那之前遇到的APIConnectionError 调用异常,就不在适用了。
笔者查看了httpx使用proxy的方法:
httpx文档:https://www.python-httpx.org/advanced/#http-proxying
proxies = {
"http://": "http://localhost:8030",
"https://": "http://localhost:8031",
}
with httpx.Client(proxies=proxies) as client:
...
那就把openai源码中包含httpx.Client替换成自己的代理地址就好啦httpx.Client(proxies={"http://localhost:8030","https://": "http://localhost:8031",})