vsakkas / sydney.py

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

Help! http.cookies.CookieError: Illegal key #136

Closed catkingcat closed 6 months ago

catkingcat commented 6 months ago

Exception has occurred: CookieError Illegal key '<' File "C:\PyTest\bing.py", line 22, in main async with SydneyClient() as sydney: File "C:\PyTest\bing.py", line 55, in asyncio.run(main()) http.cookies.CookieError: Illegal key '<'

When I run the Demo, I used os.environ["BING_COOKIES"] = to set cookies. I tried to get the cookies from https://www.bing.com/search?q=Bing+AI&showconv=0&FORM=hpcodxbing or https://copilot.microsoft.com/. It has a long header string that contains "GUID", "MUIDB", "ANON" and so on. Each one I have tried to set it but all failed.

Then I also used one answer suggest way like this: def prepare_cookies(cookies:RequestsCookieJar): """Prepare cookies for sydney""" base_str = json.dumps(dict(cookies.items())) return base_str.replace('{','').replace('}','').replace(',',';').replace(': ','=').replace('"','')

def get_new_cookies() -> RequestsCookieJar: """Returns new cookies to use in Bing Chat""" res = requests.get('https://www.bing.com/search?q=Bing+AI&showconv=1&FORM=hpcodx')

res = requests.get('https://www.bing.com/turing/conversation/create?bundleVersion=1.1342.3-cplt.7')

return res.cookies

async def main() -> None: async with SydneyClient(bing_cookies=prepare_cookies(get_new_cookies())) as sydney:

vsakkas commented 6 months ago

Hi @catkingcat

How did you get/copy the cookies? I see you're using a function called prepare_cookies. Depending on the format of the cookies, it is possible that this is not needed.

Have you tried using the Cookie-editor extension? There are instructions on the README on how to use it.

catkingcat commented 6 months ago

@vsakkas Yep, I tried to use Cookie-editor too. By clicking "Export As Header String", I got same thing like F12. It's a very long string and contains a lot of key-value infos.

Like this (I replaced specific information with an asterisk)

SRCHUSR=DOB=;_Rwho=u=;GC=;_TTSS_OUT=hist=;SRCHHPGUSR=SRCHLANG=;ANON=A=;MicrosoftApplicationsTelemetryDeviceId=;_SS=SID=;ipv6=hit=;dsc=order=;_U=;SRCHD=AF=;PPLState=1ANIMIA=FRE=;NAP=V=;_RwBf=ilt=;_UR=QS=;_EDGE_S=SID=;MUIDB=1USRLOC=HS=;_BINGNEWS=SW=;_clck=;_HPVN=CS=;_tarLang=default=;_TTSS_IN=hist=;BFPRResults=FirstPageUrls=;EDGSRCHHPGUSR=CIBV=;KievRPSSecAuth=;MMCASM=ID=;MUID=1SNRHOP=I=;SRCHS=PC=;SRCHUID=V=;WLID=;WLS=C=***

I think it only needs parts of cookies content. It should not be so long. Can you give me a sample of "BING_COOKIES"? Thanks a lot.

vsakkas commented 6 months ago

@catkingcat It seems that this cookie comes when you're not logged in to the Copilot page. Do you also face similar problems when checking a cookie from a logged in session? As an alternative, can you use the library without using cookies?

vsakkas commented 6 months ago

Also, @catkingcat Can you export the cookies you're using in JSON format as well, remove the values and send the keys here?

catkingcat commented 6 months ago

I have logged in Copilot and send a text. Then I got this cookie from create?bundleVersion=1.1381.12, like you suggested. I've that I can't use the lib without using cookies.

cookies export in JSON format after editing, I also del some infos useless: [ {"name": "SRCHUSR", "value": ""}, {"name": "_Rwho", "value": ""}, {"name": "GC", "value": ""}, {"name": "_TTSS_OUT", "value": ""}, {"name": "SRCHHPGUSR", "value": ""}, {"name": "ANON", "value": ""}, {"name": "MicrosoftApplicationsTelemetryDeviceId", "value": ""}, {"name": "_SS", "value": ""}, {"name": "ipv6", "value": ""}, {"name": "dsc", "value": ""}, {"name": "_U", "value": ""}, {"name": "SRCHD", "value": ""}, {"name": "PPLState", "value": ""}, {"name": "ANIMIA", "value": ""}, {"name": "NAP", "value": ""}, {"name": "_RwBf", "value": ""}, {"name": "_UR", "value": ""}, {"name": "_EDGE_S", "value": ""}, {"name": "CSRFCookie", "value": ""}, {"name": "MUIDB", "value": ""}, {"name": "USRLOC", "value": ""}, {"name": "_BINGNEWS", "value": ""}, {"name": "_clck", "value": ""}, {"name": "_HPVN", "value": ""}, {"name": "_tarLang", "value": ""}, {"name": "_TTSS_IN", "value": ""}, {"name": "BFPRResults", "value": ""}, {"name": "EDGSRCHHPGUSR", "value": ""}, {"name": "KievRPSSecAuth", "value": ""}, {"name": "MMCASM", "value": ""}, {"name": "MUID", "value": ""}, {"name": "SNRHOP", "value": ""}, {"name": "SRCHS", "value": ""}, {"name": "SRCHUID", "value": ""}, {"name": "WLID", "value": ""}, {"name": "WLS", "value": ""} ]

IKDH commented 6 months ago

Try to use cookie editor and export a cookie from a logged in session in Header String. Put it like this without any editing. Does it solve the problem ?

catkingcat commented 6 months ago

@IKDH Thanks for your idea, but I'm afraid that doesn't work. Exception has occurred: http.cookies.CookieError: Illegal key '<SRCHUSR'. It seems like the cookie I set isn't parsed correctly.

I set cookie in this way os.environ["BING_COOKIES"] = "<SRCHUSR=DOB=20221227&T=...>' How do you set the cookie? Can you show me a sample?

vsakkas commented 6 months ago

@catkingcat Your cookie seems to start with a < which is likely the issue.

The way I get cookies is:

IKDH commented 6 months ago

It has to start with : os.environ["BING_COOKIES"] = "SRCH

catkingcat commented 6 months ago

@vsakkas @IKDH Yes,you‘re right. It works. Thank you! But I met a new error like someone reported last week. It seems the copliot reject I request.

Exception has occurred: ConnectionTimeoutException
Failed to connect to Copilot, connection timed out
websockets.exceptions.InvalidStatusCode: server rejected WebSocket connection: HTTP 200

During handling of the above exception, another exception occurred:

asyncio.exceptions.CancelledError: 

During handling of the above exception, another exception occurred:

asyncio.exceptions.TimeoutError: 

During handling of the above exception, another exception occurred:

  File "C:\PyTest\bing.py", line 37, in main
    async for response in sydney.ask_stream(prompt):
  File "C:\PyTest\bing.py", line 55, in <module>
    asyncio.run(main())
sydney.exceptions.ConnectionTimeoutException: Failed to connect to Copilot, connection timed out
IKDH commented 6 months ago

You are on your computer or on a server ?

catkingcat commented 6 months ago

A VPS with Win server 2012 in Singapore.

IKDH commented 6 months ago

Try on your computer. I haven't managed to run it on a server.

catkingcat commented 6 months ago

I think my IP or region is forbidden by Copilot. It can't run in my computer either. I would try in other way tomorrow. Thanks for your help!

catkingcat commented 6 months ago

@vsakkas I change my account of MS, but still can't connect Copilot. Then I reinstall old version of sydney-py in 0.19.0 and 0.18.0. It's 100% rejected by server. Is there other way I can try to run this demo?

Traceback (most recent call last):
  File "C:\PyTest\bing.py", line 64, in <module>
    asyncio.run(main())
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\asynci
o\runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\asynci
o\base_events.py", line 642, in run_until_complete
    return future.result()
  File "C:\PyTest\bing.py", line 43, in main
    response = await sydney.ask("Hello, Bing!")
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\site-p
ackages\sydney\sydney.py", line 480, in ask
    async for response, suggested_responses in self._ask(
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\site-p
ackages\sydney\sydney.py", line 326, in _ask
    raise ConnectionTimeoutException(
sydney.exceptions.ConnectionTimeoutException: Failed to connect to Copilot, conn
ection timed out
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x000000A4
55544A60>
Traceback (most recent call last):
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\asynci
o\proactor_events.py", line 116, in __del__
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\asynci
o\proactor_events.py", line 108, in close
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\asynci
o\base_events.py", line 746, in call_soon
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\asynci
o\base_events.py", line 510, in _check_closed
RuntimeError: Event loop is closed
IKDH commented 6 months ago

Try to use a proxy. Are you able to use Copilot in your country?

catkingcat commented 6 months ago

@IKDH Yes, Copilot is available in my browser. So I try to run this demo in different time, but all failed. I findsome issues talked about connection timed out. It seems that old version can work.

redstoneleo commented 5 months ago

old version

which version?