GPT 避坑指南之{TimeoutError: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。}
首先来说怎么解决的
openAi 的python包中的api_requestor.py增加代理设置,我的文件路径如下 C:\Python39\Lib\site-packages\openai\api_requestor.py 增加的代码片段如下
然后简述下整个路程
近几日AutoGPT在coder圈子里突然爆火,本人也想要做一番尝试。
然github.com/Significant… 进入Git,拉代码,装包,运行。满心期待
pia~~~ 下面你的报错就呼在了脸上!!!
Using memory of type: LocalCache
Using Browser: chrome
Traceback (most recent call last):
File "C:\Python39\lib\site-packages\urllib3\connection.py", line 174, in _new_conn
conn = connection.create_connection(
File "C:\Python39\lib\site-packages\urllib3\util\connection.py", line 95, in create_connection
raise err
File "C:\Python39\lib\site-packages\urllib3\util\connection.py", line 85, in create_connection
sock.connect(sa)
TimeoutError: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Python39\lib\site-packages\urllib3\connectionpool.py", line 703, in urlopen
httplib_response = self._make_request(
File "C:\Python39\lib\site-packages\urllib3\connectionpool.py", line 386, in _make_request
self._validate_conn(conn)
File "C:\Python39\lib\site-packages\urllib3\connectionpool.py", line 1042, in _validate_conn
conn.connect()
File "C:\Python39\lib\site-packages\urllib3\connection.py", line 363, in connect
self.sock = conn = self._new_conn()
File "C:\Python39\lib\site-packages\urllib3\connection.py", line 186, in _new_conn
raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPSConnection object at 0x00000242828CA250>: Failed to establish a new connection: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Python39\lib\site-packages\requests\adapters.py", line 489, in send
resp = conn.urlopen(
File "C:\Python39\lib\site-packages\urllib3\connectionpool.py", line 815, in urlopen
return self.urlopen(
File "C:\Python39\lib\site-packages\urllib3\connectionpool.py", line 815, in urlopen
return self.urlopen(
File "C:\Python39\lib\site-packages\urllib3\connectionpool.py", line 787, in urlopen
retries = retries.increment(
File "C:\Python39\lib\site-packages\urllib3\util\retry.py", line 592, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
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 0x00000242828CA250>: Failed to establish a new connection: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失 败。'))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Python39\lib\site-packages\openai\api_requestor.py", line 516, in request_raw
result = _thread_context.session.request(
File "C:\Python39\lib\site-packages\requests\sessions.py", line 587, in request
resp = self.send(prep, **send_kwargs)
File "C:\Python39\lib\site-packages\requests\sessions.py", line 701, in send
r = adapter.send(request, **kwargs)
File "C:\Python39\lib\site-packages\requests\adapters.py", line 565, in send
raise ConnectionError(e, request=request)
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 0x00000242828CA250>: Failed to establish a new connection: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。'))
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Python39\lib\runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Python39\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "E:\github\Auto-GPT\autogpt\__main__.py", line 53, in <module>
main()
File "E:\github\Auto-GPT\autogpt\__main__.py", line 49, in main
agent.start_interaction_loop()
File "E:\github\Auto-GPT\autogpt\agent\agent.py", line 65, in start_interaction_loop
assistant_reply = chat_with_ai(
File "E:\github\Auto-GPT\autogpt\chat.py", line 159, in chat_with_ai
assistant_reply = create_chat_completion(
File "E:\github\Auto-GPT\autogpt\llm_utils.py", line 90, in create_chat_completion
response = openai.ChatCompletion.create(
File "C:\Python39\lib\site-packages\openai\api_resources\chat_completion.py", line 25, in create
return super().create(*args, **kwargs)
File "C:\Python39\lib\site-packages\openai\api_resources\abstract\engine_api_resource.py", line 153, in create
response, _, api_key = requestor.request(
File "C:\Python39\lib\site-packages\openai\api_requestor.py", line 216, in request
result = self.request_raw(
File "C:\Python39\lib\site-packages\openai\api_requestor.py", line 528, in request_raw
raise error.APIConnectionError(
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 0x00000242828CA250>: Failed to establish a new connection: [WinError 10060] 由于连接方在一段时间后没有正确答复 或连接的主机没有反应,连接尝试失败。'))
好,第一反应是不是代理没开呢?检查后确定开了,额。。。切换全局模式把!
em。。。。依旧。
仔细看看这个报错。
假设不是autoGpt 的问题,那么我们来写一个调用openai的demo来试一下
import openai
openai.api_key = ' '
def chat_gpt(prompt):
prompt = prompt
model_engine = "这里是你的openAi -key"
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("////")
OK 运行,pia!
Traceback (most recent call last):
File "C:\Python39\lib\site-packages\openai\api_requestor.py", line 516, in request_raw
result = _thread_context.session.request(
File "C:\Python39\lib\site-packages\requests\sessions.py", line 587, in request
resp = self.send(prep, **send_kwargs)
File "C:\Python39\lib\site-packages\requests\sessions.py", line 701, in send
r = adapter.send(request, **kwargs)
File "C:\Python39\lib\site-packages\requests\adapters.py", line 456, in send
conn = self.get_connection(request.url, proxies)
File "C:\Python39\lib\site-packages\requests\adapters.py", line 348, in get_connection
raise InvalidProxyURL(
requests.exceptions.InvalidProxyURL: Please check proxy URL. It is malformed and could be missing the host.
好 这段看来就是问题所以在 了, 手动加上开篇的代理。问题解决。