vsakkas / sydney.py

Python Client for Copilot (formerly named Bing Chat), also known as Sydney.
MIT License
239 stars 31 forks source link

Failed to connect to Copilot, connection timed out #132

Open Mazawrath opened 6 months ago

Mazawrath commented 6 months ago

Apologizes for the rather non-helpful error, I saw someone else had issues with this same error but I just can't seem to figure out what it is.

Traceback (most recent call last):
  File "/root/workspace/ChatGPT_API/src/bingGPT.py", line 52, in ask
    request = await sydney.ask(ask_request.prompt, attachment=ask_request.attachment, raw=True)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/workspace/ChatGPT_API/.venv/lib/python3.11/site-packages/sydney/sydney.py", line 515, in ask
    async for response, suggested_responses in self._ask(
  File "/root/workspace/ChatGPT_API/.venv/lib/python3.11/site-packages/sydney/sydney.py", line 348, in _ask
    raise ConnectionTimeoutException(
sydney.exceptions.ConnectionTimeoutException: Failed to connect to Copilot, connection timed out

If I remove the BING_COOKIES, I get this error instead

Traceback (most recent call last):
  File "/root/workspace/ChatGPT_API/.venv/lib/python3.11/site-packages/uvicorn/protocols/http/httptools_impl.py", line 426, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/workspace/ChatGPT_API/.venv/lib/python3.11/site-packages/uvicorn/middleware/proxy_headers.py", line 84, in __call__
    return await self.app(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/workspace/ChatGPT_API/.venv/lib/python3.11/site-packages/fastapi/applications.py", line 1106, in __call__
    await super().__call__(scope, receive, send)
  File "/root/workspace/ChatGPT_API/.venv/lib/python3.11/site-packages/starlette/applications.py", line 122, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/root/workspace/ChatGPT_API/.venv/lib/python3.11/site-packages/starlette/middleware/errors.py", line 184, in __call__
    raise exc
  File "/root/workspace/ChatGPT_API/.venv/lib/python3.11/site-packages/starlette/middleware/errors.py", line 162, in __call__
    await self.app(scope, receive, _send)
  File "/root/workspace/ChatGPT_API/.venv/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 79, in __call__
    raise exc
  File "/root/workspace/ChatGPT_API/.venv/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 68, in __call__
    await self.app(scope, receive, sender)
  File "/root/workspace/ChatGPT_API/.venv/lib/python3.11/site-packages/fastapi/middleware/asyncexitstack.py", line 20, in __call__
    raise e
  File "/root/workspace/ChatGPT_API/.venv/lib/python3.11/site-packages/fastapi/middleware/asyncexitstack.py", line 17, in __call__
    await self.app(scope, receive, send)
  File "/root/workspace/ChatGPT_API/.venv/lib/python3.11/site-packages/starlette/routing.py", line 718, in __call__
    await route.handle(scope, receive, send)
  File "/root/workspace/ChatGPT_API/.venv/lib/python3.11/site-packages/starlette/routing.py", line 276, in handle
    await self.app(scope, receive, send)
  File "/root/workspace/ChatGPT_API/.venv/lib/python3.11/site-packages/starlette/routing.py", line 66, in app
    response = await func(request)
               ^^^^^^^^^^^^^^^^^^^
  File "/root/workspace/ChatGPT_API/.venv/lib/python3.11/site-packages/fastapi/routing.py", line 274, in app
    raw_response = await run_endpoint_function(
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/workspace/ChatGPT_API/.venv/lib/python3.11/site-packages/fastapi/routing.py", line 191, in run_endpoint_function
    return await dependant.call(**values)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/workspace/ChatGPT_API/src/bingGPT.py", line 37, in ask
    await sydney.reset_conversation(style="creative")
  File "/root/workspace/ChatGPT_API/.venv/lib/python3.11/site-packages/sydney/sydney.py", line 748, in reset_conversation
    await self.start_conversation()
  File "/root/workspace/ChatGPT_API/.venv/lib/python3.11/site-packages/sydney/sydney.py", line 462, in start_conversation
    response_dict = await response.json()
                    ^^^^^^^^^^^^^^^^^^^^^
  File "/root/workspace/ChatGPT_API/.venv/lib/python3.11/site-packages/aiohttp/client_reqrep.py", line 1165, in json
    raise ContentTypeError(
aiohttp.client_exceptions.ContentTypeError: 0, message='Attempt to decode JSON with unexpected mimetype: ', url=URL('https://www.bing.com/turing/conversation/create?bundleVersion=1.1342.3-cplt.7')
vsakkas commented 6 months ago

Thanks @Mazawrath for reporting this

I will have a look, I think this does show up sometimes and typically waiting and retrying should do the trick. I will see if I can do something about this

Mazawrath commented 6 months ago

Yeah unfortunately for me it's 100% consistent and happens every time I am running a much older version on this project my other VM and this issue doesn't happen on that one, so not sure what the difference is.

Mazawrath commented 6 months ago

I was able to do further testing. I tried wscat -c wss://sydney.bing.com/sydney/ChatHub to see if the WebSocket responds and got error: Unexpected server response: 200 This is happening on my DigitalOcean VM, when I was originally using Azure.

Is there anything that can be done to fix this?

vsakkas commented 6 months ago

Yeah unfortunately for me it's 100% consistent and happens every time I am running a much older version on this project my other VM and this issue doesn't happen on that one, so not sure what the difference is.

Hi @Mazawrath Which version are you using that does not have this issue? If that's the case, there's probably a regression

vsakkas commented 6 months ago

Latest version 0.20.1 could possibly fix this due to improvements in the API compatibility.

IKDH commented 6 months ago

@vsakkas I still get the same error on AWS server. It's working fine on computer but not on my AWS server. I will try to use a VPN to see if it's not related to a blacklist of datacenter IPs.

IKDH commented 6 months ago

Yeah unfortunately for me it's 100% consistent and happens every time I am running a much older version on this project my other VM and this issue doesn't happen on that one, so not sure what the difference is.

@Mazawrath Could you tell me which version of this lib is working on your VM ? Is it with or without cookies ? Because all my tests failed on datacenter.

Mazawrath commented 6 months ago

I can also confirm going from DigitalOcean to Oracle Cloud didn't fix it either. Seems like Bing is blacklisting IPs from both of those services.

The only service I've actually managed to use that works is Azure.

IKDH commented 6 months ago

I can also confirm going from DigitalOcean to Oracle Cloud didn't fix it either. Seems like Bing is blacklisting IPs from both of those services.

The only service I've actually managed to use that works is Azure.

I am not sure if it's related because even with residential proxy, I can't use the script on datacenter. Which version of the lib is working on Azure ?

haiht24 commented 5 months ago

It is working on Namecheap VPS @vsakkas @Mazawrath @IKDH I've noticed that my Digital Ocean droplet running Ubuntu 22 can't connect to Bing Chat, while my Namecheap VPS running the same OS can. It seems Bing might blacklist certain IP ranges.

To check if your server can connect to Bing Chat, use this command: curl https://edgeservices.bing.com/edgesvc/turing/conversation/create?bundleVersion=1.1381.12

If you get a JSON response like: {"conversationId":"_something_","clientId":"_something_","result":{"value":"Success","message":null}}

then your server is good to go for running sydney.py. This is a quick way to verify connectivity before proceeding with your setup. If the response is empty, it's possible that your server may not allow connecting to Bing Chat

Hope this helps!

Mazawrath commented 5 months ago

So, I went down quite the rabbithole. First of all, thank you for @haiht24 for confirming what I assumed was true, that yes, Bing is blocking Oracle Cloud. Now, I was able to get around this with a proxy. However, sydney.py uses websockets, which does not support proxies. I was able to get a proxy setup using websocket-client, and was able to string together a successful solution, however it doesn't seem to be async and might have issues if it gets implemented here. Worth looking into @vsakkas of maybe finding a way to support proxies for the actual websocket part, I didn't find the issue to be with aiohttp, so that use_proxy argument did not fix my issue.

vsakkas commented 5 months ago

Thank you @haiht24 @Mazawrath for the investigation!

I've been somewhat busy lately, so development has slowed down in the last couple of weeks, but this is definitely something that I'd like to tackle wherever I have the time for it.

I will look int some websocket alternatives or maybe even a way to add proxy support without replacing the current library. Will follow up once I have something ready!

nisnevich commented 1 month ago

I've been using sydney since a month, but then it started to require captcha consistently. Now I'm going through either one of these 4 errors every time I try to enable cookie or proxy or use sydney without them:

sydney.exceptions.CreateConversationException: Failed to create conversation, received status: 500

sydney.exceptions.ConnectionTimeoutException: Failed to connect to Copilot, connection timed out

sydney.exceptions.NoResponseException: No response was returned

aiohttp.client_exceptions.ContentTypeError: 0, message='Attempt to decode JSON with unexpected mimetype: ', url=URL('https://copilot.microsoft.com/turing/conversation/create?bundleVersion=1.1729.0')       

What I found has helped so far was only switching to a different machine and network.

I carefully followed recommendations to enable cookies but couldn't set them to work, any time I tried I got one of the above errors. I didn't get any "Verified" message, although I was logged in (tried "guest" as well) and talked through multiple conversations till they reached the quota. Adding optional cookie didn't make any difference: os.environ["BING_U_COOKIE"] = 'OPTIONAL'