vsakkas / sydney.py

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

Exception: Failed to create conversation, received status: 404, Unclosed client session #32

Closed qiankun1024 closed 1 year ago

qiankun1024 commented 1 year ago

when i use Sydney.py to easily create a CLI client for Bing Chat: example.py

import asyncio

from sydney import SydneyClient

async def main() -> None:
    async with SydneyClient() as sydney:
        while True:
            prompt = input("You: ")

            if prompt == "!reset":
                await sydney.reset_conversation()
                continue
            elif prompt == "!exit":
                break

            print("Sydney: ", end="", flush=True)
            async for response in sydney.ask_stream(prompt):
                print(response, end="", flush=True)
            print("\n")

if __name__ == "__main__":
    asyncio.run(main())

But it reported this error:

Traceback (most recent call last):
  File "~/Project/sydney.py/example.py", line 24, in <module>
    asyncio.run(main())
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
    return future.result()
  File "~/Project/sydney.py/example.py", line 7, in main
    async with SydneyClient() as sydney:
  File "~/Project/sydney.py/sydney/sydney.py", line 57, in __aenter__
    await self.start_conversation()
  File "~/Project/sydney.py/sydney/sydney.py", line 259, in start_conversation
    raise Exception(
Exception: Failed to create conversation, received status: 404
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x10a233bb0>
Unclosed connector
connections: ['[(<aiohttp.client_proto.ResponseHandler object at 0x10aef11c0>, 0.491523)]']
connector: <aiohttp.connector.TCPConnector object at 0x10ae5ea00>

Is this a problem with Python 3.9?

vsakkas commented 1 year ago

Hi @qiankun1024 ! Thanks for reaching out about this issue.

I will try to reproduce your issue and will follow up with some information, hopefully later today.

vsakkas commented 1 year ago

Hi @qiankun1024

Unfortunately, I was not able to recreate the issue you are having on my local setup. However, given that originally Sydney.py did not support Python 3.9 I released an updated version of it which should now support Python 3.9!

Let me know if the updated version changes anything on your side. Also, make sure that the BING_U_COOKIE environment variable has the correct value when you run your code.

qiankun1024 commented 1 year ago

Hi @vsakkas

Thank you very much for your patience in answering. I tried the latest release version, but it still didn't work.

  File "~/Project/sydney.py/sydney/sydney.py", line 263, in start_conversation
    raise Exception(
Exception: Failed to create conversation, received status: 404
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x10996a130>
Unclosed connector
connections: ['[(<aiohttp.client_proto.ResponseHandler object at 0x10a378280>, 3.2212585)]']
connector: <aiohttp.connector.TCPConnector object at 0x10a2f70d0>

I suspect it might be due to my network issue. There is a firewall in my region, so I had to use a VPN proxy to access NewBing. image

I also configured a proxy in my programming environment, which is in the settings of vscode. image

I also set the system level HTTP Proxy and https Proxy, but these attempts have not worked.

(venv) ➜  sydney.py git:(master) ✗ echo $https_proxy
http://127.0.0.1:7890
(venv) ➜  sydney.py git:(master) ✗ echo $http_proxy 
http://127.0.0.1:7890
(venv) ➜  sydney.py git:(master) ✗ curl -I https://www.bing.com/turing/conversation/create
HTTP/1.1 200 Connection established

HTTP/2 200 
content-length: 0
x-cache: CONFIG_NOCACHE
accept-ch: Sec-CH-UA-Arch, Sec-CH-UA-Bitness, Sec-CH-UA-Full-Version, Sec-CH-UA-Mobile, Sec-CH-UA-Model, Sec-CH-UA-Platform, Sec-CH-UA-Platform-Version
x-msedge-ref: Ref A: 621C82883D644780A92E083D9A912CFB Ref B: SG1EDGE0318 Ref C: 2023-04-20T02:27:33Z
date: Thu, 20 Apr 2023 02:27:33 GMT

Anyway, thank you again. I will try to find the problem again later.

qiankun1024 commented 1 year ago

By the way, the way I access NewBing and obtain cookies here should be correct

image

And then the environment variable setting for BING_U_COOKIE is also OK:

(venv) ➜  sydney.py git:(master) ✗ env | grep BING_U_COOKIE
BING_U_COOKIE=12u9mEoDivfFIX4WlxCaLLh9YV0eWbKPq1uYIp-UC597roTUN6YmB8ZUATpkFO2nLv5dwL5-mYh0NFqI_h9RY5D3HoMdiR6bkaIwXA8ZVvyByY9h6pPlSUw5Of5X03eYf7M5vV2_GcgxxAzbrsnrS-3DxICHr6GXy6ts1twBtmMZrRansBjpAg4NDI1a1P-_1uJOBK4wHFUs...omit
(venv) ➜  sydney.py git:(master) ✗ echo $BING_U_COOKIE
12u9mEoDivfFIX4WlxCaLLh9YV0eWbKPq1uYIp-UC597roTUN6YmB8ZUATpkFO2nLv5dwL5-mYh0NFqI_h9RY5D3HoMdiR6bkaIwXA8ZVvyByY9h6pPlSUw5Of5X03eYf7M5vV2_GcgxxAzbrsnrS-3DxICHr6GXy6ts1twBtmMZrRansBjpAg4NDI1a1P-_1uJOBK4wHFUs...omit
vsakkas commented 1 year ago

Thanks for all this information, this is really helpful. Judging from the above, it seems the issue is that you're relying on a VPN proxy in order to access Bing Chat, something that is not currently supported by Sydney.py.

I have started working on a new branch with some limited support for specifying a proxy URL, which can be found here: https://github.com/vsakkas/sydney.py/tree/feat/proxy-support

Unfortunately, the websockets package that I am using does not seem to support proxies, so I will look into replacing it with something else. Right now, I think that if you use the linked branch and provide the proxy_url to the SydneyClient, the start_conversation method should work at least.

I will follow up later with more updates regarding proxy support!

qiankun1024 commented 1 year ago

It doesn't seem to be working

(venv) ➜  sydney.py git:(feat/proxy-support) ✗ echo $https_proxy
http://127.0.0.1:7890
(venv) ➜  sydney.py git:(feat/proxy-support) ✗ echo $http_proxy
http://127.0.0.1:7890
(venv) ➜  sydney.py git:(feat/proxy-support) ✗
(venv) ➜  sydney.py git:(feat/proxy-support) ✗ curl -I https://www.bing.com/turing/conversation/create
HTTP/1.1 200 Connection established

HTTP/2 200
content-length: 0
x-cache: CONFIG_NOCACHE
accept-ch: Sec-CH-UA-Arch, Sec-CH-UA-Bitness, Sec-CH-UA-Full-Version, Sec-CH-UA-Mobile, Sec-CH-UA-Model, Sec-CH-UA-Platform, Sec-CH-UA-Platform-Version
x-msedge-ref: Ref A: DF1BFEF4A7494C89B6270784203D9DFA Ref B: SG1EDGE0309 Ref C: 2023-04-20T10:12:45Z
date: Thu, 20 Apr 2023 10:12:44 GMT

(venv) ➜  sydney.py git:(feat/proxy-support) ✗ echo $BING_U_COOKIE
14eEpGSiqV7NFTxgpARz6nPSc79WHnFDW_swzTL7vZlCkG0TOW5-ATF1MU2DvzELgTVoSyE2voXBkBa2gPe9dHBIaTyfUK5-Z__W4CDq9EeOtRl7KAMzvEN4D5LM078mbTTkBXkbCkCsCdkIiD5whxyu8nQUJvi26laGFP_b5jdzBm4wG4n62_FHHuB-xCJYNzRDGpudog6x0Izb1z7D-tREiRNR8upT-znN0teVaLHM

(venv) ➜  sydney.py git:(feat/proxy-support) ✗ python example.py
Traceback (most recent call last):
  File "~/Project/sydney.py/example.py", line 24, in <module>
    asyncio.run(main())
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
    return future.result()
  File "~/Project/sydney.py/example.py", line 7, in main
    async with SydneyClient() as sydney:
  File "~/Project/sydney.py/sydney/sydney.py", line 64, in __aenter__
    await self.start_conversation()
  File "~/Project/sydney.py/sydney/sydney.py", line 272, in start_conversation
    raise Exception(
Exception: Failed to create conversation, received status: 404
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x10e51d1f0>
Unclosed connector
connections: ['[(<aiohttp.client_proto.ResponseHandler object at 0x10ef5a220>, 2.972278958)]']
connector: <aiohttp.connector.TCPConnector object at 0x10ef390d0>
qiankun1024 commented 1 year ago

By the way, here is a suggestion for you. I hope that the project can include all dependent packages in requirements.txt, Below are the requirements required for my Python 3.9 based virtual environment.

aiohttp==3.8.4
aiosignal==1.3.1
async-timeout==4.0.2
attrs==23.1.0
charset-normalizer==3.1.0
frozenlist==1.3.3
idna==3.4
multidict==6.0.4
websockets==11.0.2
yarl==1.8.2
vsakkas commented 1 year ago

Thank you for the extra information, I was expecting the branch to at least fix the 404 issue for creating a conversation, but looks like that something is missing.

Since you are using Bing Chat from a different region, I am wondering if the 404 issue could be related to the URL that Sydney.py uses to create the conversation.

Right now, it uses https://www.bing.com/turing/conversation/create.

On Edge, if you go to Bing Chat, open the Dev Tools on the Network tab, send a message and then look for an entry named create, what is the Request URL that you see? Is it https://www.bing.com/turing/conversation/create or https://edgeservices.bing.com/edgesvc/turing/conversation/create or something else?

vsakkas commented 1 year ago

Also, thank you for the recommendation! I will consider it, although I am not sure if that is needed. I use poetry to manage this project, which relies on pyproject.toml file which lists the dependencies that I have used:

[tool.poetry.dependencies]
python = "^3.9"
aiohttp = "^3.8.4"
websockets = "^10.4"

However, running a pip freeze gave me this:

aiohttp==3.8.4
aiosignal==1.3.1
async-timeout==4.0.2
attrs==23.1.0
black==23.3.0
charset-normalizer==3.1.0
click==8.1.3
frozenlist==1.3.3
idna==3.4
multidict==6.0.4
mypy==1.2.0
mypy-extensions==1.0.0
packaging==23.1
pathspec==0.11.1
platformdirs==3.2.0
tomli==2.0.1
typing_extensions==4.5.0
websockets==10.4
yarl==1.8.2

Besides some extra dev related dependencies, the only difference is in the websockets package but right now it is not related to the issue as your 404 error is caused in the part that uses aiohttp.

vsakkas commented 1 year ago

I have pushed another change to the existing branch.

I used mitmproxy in order to test this and made some changes to the aiohttp code when creating a conversation. I started the mitmproxy and instead of using the proxy_url parameter of the SydneyClient, the branch now allows to use the HTTP_PROXY and HTTPS_PROXY environment variables which in my case were both set to http://127.0.0.1:8080.

Unfortunately, I had to disable the SSL verification in order to get this to work, but after doing that I was able to get the conversation to start!

qiankun1024 commented 1 year ago

Thank you for the extra information, I was expecting the branch to at least fix the 404 issue for creating a conversation, but looks like that something is missing.

Since you are using Bing Chat from a different region, I am wondering if the 404 issue could be related to the URL that Sydney.py uses to create the conversation.

Right now, it uses https://www.bing.com/turing/conversation/create.

On Edge, if you go to Bing Chat, open the Dev Tools on the Network tab, send a message and then look for an entry named create, what is the Request URL that you see? Is it https://www.bing.com/turing/conversation/create or https://edgeservices.bing.com/edgesvc/turing/conversation/create or something else?

The request url just looks like this:

Request URL: https://www.bing.com/turing/conversation/create
Request Method: GET
Status Code: 200 
Remote Address: 127.0.0.1:7890
Referrer Policy: origin-when-cross-origin
qiankun1024 commented 1 year ago

I have never heard of Poetry before and have always used Pip. It seems like it's a good tool, and I will try to learn its usage.

qiankun1024 commented 1 year ago

That's great. I successfully used the latest branch.

(venv) ➜  sydney.py git:(feat/proxy-support) ✗ python example.py
You: When was Bing Chat released?
Sydney:
Microsoft announced the new Bing Chat on February 7th, 2023[^1^] and it was available to limited numbers of users since the launch date[^2^]. The Bing Chat preview was opened up to a wider audience in March 2023[^3^].

You: Sydney: I'm sorry but I haven't received a message from you. If you have any questions or need help with anything, feel free to ask me.🙂

You: love you~
Sydney: Traceback (most recent call last):
  File "~/Project/sydney.py/example.py", line 24, in <module>
    asyncio.run(main())
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
    return future.result()
  File "~/Project/sydney.py/example.py", line 18, in main
    async for response in sydney.ask_stream(prompt):
  File "~/Project/sydney.py/sydney/sydney.py", line 349, in ask_stream
    async for response in self._ask(prompt, citations, raw, stream=True):
  File "~/Project/sydney.py/sydney/sydney.py", line 197, in _ask
    yield response["item"]["messages"][1]["text"]
IndexError: list index out of range

At that time, I replied with “love you”,She may be too shy to report the mistake ^-^

vsakkas commented 1 year ago

I suspect the conversation might have stopped which is something that Bing Chat does on the browser as well, sometimes. Does this occur often with the updated branch? I can try to improve the error handling in this part of the code, although it does not seem to be related to the proxy support, so that will be in another branch once I get to it.

qiankun1024 commented 1 year ago

According to my testing, if you type enter again while waiting for Sydney's answer, it will prompt for the first time: You: Sydney: I'm sorry but I haven't received any message from you. If you have any questions or need help with anything, feel free to ask me anytime.🙂

The second time you ask again, he will report this error:

 File "~/Project/sydney.py/sydney/sydney.py", line 349, in ask_stream
    async for response in self._ask(prompt, citations, raw, stream=True):
  File "~/Project/sydney.py/sydney/sydney.py", line 197, in _ask
    yield response["item"]["messages"][1]["text"]
IndexError: list index out of range

So if you wait for it to reply every time before entering, there won't be any problems.

vsakkas commented 1 year ago

Yes, I was able to reproduce this issue sometimes on my local setup as well. I am not sure if it is related to the proxy support though, as it seems to be the behavior of input() in Python. You can see here a short discussion (and solution) about it: https://stackoverflow.com/questions/65976696/how-to-ignore-input-while-in-a-loop-python

So what seems to happens is that after this, Bing Chat closes the conversation from its side with the last message being: I'm sorry but I haven't received any message from you. If you have any questions or need help with anything, feel free to ask me anytime.🙂 but the Sydney Client tries to send another message to it anyways.

This script should fix the issue for you, I think:

import asyncio

from sydney import SydneyClient

def flush_input():
    try:
        import msvcrt

        while msvcrt.kbhit():
            msvcrt.getch()
    except ImportError:
        import sys, termios  # for linux/unix

        termios.tcflush(sys.stdin, termios.TCIOFLUSH)

async def main() -> None:
    async with SydneyClient() as sydney:
        while True:
            flush_input()
            prompt = input("You: ")
            await asyncio.sleep(0)

            if prompt == "!reset":
                await sydney.reset_conversation()
                continue
            if prompt == "!exit":
                break

            print("Sydney: ", end="", flush=True)
            async for response in sydney.ask_stream(prompt):
                print(response, end="", flush=True)

if __name__ == "__main__":
    asyncio.run(main())
qiankun1024 commented 1 year ago

Thank you very much for letting me learn a lot about Python. Thank you again!

vsakkas commented 1 year ago

Glad to help! I will try to cleanup and improve the code in the branch asap and get it merged and released so that it can be used by everyone. I will keep this issue open until that's done.

vsakkas commented 1 year ago

Hi @qiankun1024

It's been a while, but I got back to this task and made some changes to the existing feat/proxy-support branch. If you are still interested in this and have time to test this, I would be very grateful as I am ready to merge it and release 0.12.0 so that everyone can have this feature out of the box :)

qiankun1024 commented 1 year ago

@vsakkas I apologize for the delayed response as I have been busy with a Vue project lately. I just noticed your merge and it seems to be working perfectly, just as we hoped for in this project.

Thank you for your contribution and for your patience.

Best regards