vsakkas/sydney.py

Is there an error in my code, or is there a bug in the library?

Closed this issue · 35 comments

this is my code that I created to have a personal bot on telegram. the bot responds perfectly to questions that do not require searching on the web, as soon as you ask a question for which you will have to look for the answer on the internet I get this error... why? how can I solve it Sydney: Searching the web for: Cosa c'è sulla luna?` it hangs like this without giving me the answer in the telegram bot and it doesn't give me any errors in the terminal

this is my code

import os import asyncio import telebot from sydney import SydneyClient

Token del bot Telegram, ottenuto da BotFather su Telegram telegram_token = 'bot_token'

Impostazione del cookie _U per l'utilizzo con Sydney.py bing_cookies = 'cookies _u' os.environ["BING_U_COOKIE"] = bing_cookies

` Inizializzazione del bot Telegram
bot = telebot.TeleBot(telegram_token)

Funzione per gestire il comando /start @bot.message_handler(commands=['start']) def send_welcome(message): bot.reply_to(message, "Ciao! Scrivi qualcosa per iniziare la conversazione con Sydney.")

Funzione per gestire i messaggi testuali
`@bot.message_handler(func=lambda message: True)
def handle_message(message):
async def ask_sydney(question):
async with SydneyClient() as sydney:
response = await sydney.ask(question)
return response

question = message.text`

`interrogazione di Sydney per ottenere la risposta
response = asyncio.run(ask_sydney(question))

# Invio della risposta al mittente su Telegram
bot.reply_to(message, f"Sydney: {response}")

Avvio del bot

bot.polling()`

while in the CLI version I get the answer even if I search for it on the web but when the answer ends and it has to show the suggested answers this error appears. Is it a bug in the library or am I doing something wrong in the codes?

this is error: File "/Users/davide/Documents/tutto/jfkdjhfdi.py", line 10, in main await conversation_loop(sydney, colors, color_index) File "/Users/davide/Documents/tutto/jfkdjhfdi.py", line 24, in conversation_loop async for response, suggested_responses in sydney.ask_stream(prompt, suggestions=True): File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/sydney/sydney.py", line 453, in ask_stream async for response, suggested_responses in self._ask( File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/sydney/sydney.py", line 244, in _ask for item in messages[1]["suggestedResponses"] ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^ KeyError: 'suggestedResponses'

`import asyncio
import os
from sydney import SydneyClient
from colorama import Fore, Style

async def main() -> None:
async with SydneyClient(style="balanced") as sydney:
colors = [Fore.RED, Fore.GREEN, Fore.BLUE, Fore.MAGENTA, Fore.CYAN]
color_index = 0
await conversation_loop(sydney, colors, color_index)

async def conversation_loop(sydney, colors, color_index):
while True:
prompt = input("Tu: ")

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

    current_color = colors[color_index % len(colors)]
    print(current_color + "Sydney: ", end="", flush=True)
    async for response, suggested_responses in sydney.ask_stream(prompt, suggestions=True):
        if "```" in response:
            lines = response.split('\n')
            in_backticks = False
            for line in lines:
                if line.strip() == "```":
                    in_backticks = not in_backticks
                    print(Style.RESET_ALL + Style.BRIGHT if in_backticks else current_color, end="")
                else:
                    print(line if in_backticks else current_color + line)
        else:
            print(current_color + response, end="", flush=True)

        if suggested_responses:
            print(Fore.BLUE + "\nSuggerimenti:")
            for suggestion in suggested_responses:
                print(Fore.CYAN + f"- {suggestion}")
    print(Style.RESET_ALL + "\n")

    color_index += 1

print("Conversazione terminata.")

async def composition_menu(sydney):
pass

if name == "main":
os.environ[
"BING_U_COOKIE"] = "u_cookies"

asyncio.run(main())

`

Hi @BlackDicky

I will take a look at the code and see what could cause this. There is some code which is supposed to hide those "Searching the web" messages, but it might need to be updated. I will follow up later on this!

@vsakkas If you could take a look and let me know if it's the code or the library that's the problem, I'd be grateful. and thank you again for everything you do. Your work is highly appreciated by me, you are very good and I will support you in every project you do! <3

Thank you for your kind words @BlackDicky !!

I am looking at your report, and I see this important information:

File "/Users/davide/Documents/tutto/jfkdjhfdi.py", line 10, in main await conversation_loop(sydney, colors, color_index) File "/Users/davide/Documents/tutto/jfkdjhfdi.py", line 24, in conversation_loop async for response, suggested_responses in sydney.ask_stream(prompt, suggestions=True): File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/sydney/sydney.py", line 453, in ask_stream async for response, suggested_responses in self._ask( File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/sydney/sydney.py", line 244, in _ask for item in messages[1]["suggestedResponses"] ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^ KeyError: 'suggestedResponses'

KeyError: 'suggestedResponses' suggests that there is a bug with with the library, not your code. Which version of Sydney.py are you using? I suspect you might be using an older version which contains bugs that were fixed in later versions.

Thank you for your kind words @BlackDicky !!

I am looking at your report, and I see this important information:

File "/Users/davide/Documents/tutto/jfkdjhfdi.py", line 10, in main await conversation_loop(sydney, colors, color_index) File "/Users/davide/Documents/tutto/jfkdjhfdi.py", line 24, in conversation_loop async for response, suggested_responses in sydney.ask_stream(prompt, suggestions=True): File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/sydney/sydney.py", line 453, in ask_stream async for response, suggested_responses in self._ask( File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/sydney/sydney.py", line 244, in _ask for item in messages[1]["suggestedResponses"] ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^ KeyError: 'suggestedResponses'

KeyError: 'suggestedResponses' suggests that there is a bug with with the library, not your code. Which version of Sydney.py are you using?

This is information I received with the pip show sydney-py command:

Name: sydney-py
Version: 0.18.0
Summary: Python Client for Copilot (formerly named Bing Chat), also known as Sydney.
Home-page: 
Author: vsakkas
Author-email: vasileios.sakkas96@gmail.com
License: MIT
Location: /opt/homebrew/lib/python3.11/site-packages
Requires: aiohttp, websockets

@vsakkas

Thank you, good to know! Btw, 0.18 no longer uses the _U cookie, for better compatibility it is suggested to use all the Bing Chat related cookies. Instructions are provided on the README of the repo :)

In any case, I will try to reproduce your issue and investigate more

Is there a momentary solution to resolve this error and allow proper operation? Maybe with a specific older version? @vsakkas

Version 0.17.6 is the same, but uses the _U cookie instead of all the cookies. I don't think I could recommend any older version as the 0.17.1-0.17.6 updates brought several bug fixes with the help of lots of users of the library.

A quick workaround to fixing the KeyError: 'suggestedResponses' error is to not the use the suggestions flags while I investigate it some more.
Regarding the issue with searching the web, I will try to replicate it. Right now, there is no way to disable searching the web, but one user has suggested a way to do it, which I want to integrate as soon as possible.!

La versione 0.17.6 è la stessa, ma utilizza il cookie _U invece di tutti i cookie. Non credo di poter raccomandare alcuna versione precedente poiché gli aggiornamenti 0.17.1-0.17.6 hanno portato diverse correzioni di bug con l'aiuto di molti utenti della libreria.

A quick workaround to fixing the KeyError: 'suggestedResponses' error is to not the use the suggestions flags while I investigate it some more.Per quanto riguarda il problema con la ricerca sul web, cercherò di replicarlo. In questo momento, non c'è modo di disabilitare la ricerca sul web, ma un utente ha suggerito un modo per farlo, che voglio integrare il prima possibile.!

Then I will wait for a new version that can fix this search bug so that the user can through a command choose whether to enable online search or not<3, but to set all cookies what string do I have to put in my python code @vsakkas

You need to use the `BING_COOKIES environment variable and set it to all the cookies that are copy pasted from hereL

Screenshot from 2023-11-26 18-09-14

It is a long, long string (lots of cookies), so make sure you get all of them (there's probably also extensions to make it easier).
To get them without an extension you need to open the Network page on the Copilot website, send a message and find a request that contains create in its name. Copy paste the cookies and you're done!

P.S. Do not copy the "Cookie:" part, only what comes after it (mbox=...)

??Quindi questa parte diventa: os.environ["BING_U_COOKIE"] = mbox= ALLCookiehere @vsakkas

@BlackDicky Should be os.environ["BING_COOKIES"] = "ALLCookiehere"

Yes, if I only enter "_U" it works, it does the search on the web normally and then after as soon as it has finished the answer and must give the "SuggestedResponse" from this error:File "/Users/davide/Documents/tutto/jfkdjhfdi.py", line 10, in main await conversation_loop(sydney, colors, color_index) File "/Users/davide/Documents/tutto/jfkdjhfdi.py", line 24, in conversation_loop async for response, suggested_responses in sydney.ask_stream(prompt, suggestions=True): File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/sydney/sydney.py", line 453, in ask_stream async for response, suggested_responses in self._ask( File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/sydney/sydney.py", line 244, in _ask for item in messages[1]["suggestedResponses"] ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^ KeyError: 'suggestedResponses'

If instead I insert all the long "header string" and paste it, it gives me a SolveCaptcha error if I go to the site and write a message, no captcha verification appears in the chat
@vsakkas

https://copilot.microsoft.com/

I'm sorry if I wrote to you on the other side only now I notice this 😢

@BlackDicky I am worried about this line

line 244, in _ask for item in messages[1]["suggestedResponses"]

On the latest version of the library, line 244 is not related to "suggestedResponses". Also, I see messages[1], but it should be messages[-1].

It seems that despite the output of pip show sydney-py that you showed before, you might be running an older version (hence why using the _U cookie works for you).

I suggest, removing and reinstalling the library to make sure you're using the latest version!

Update: I think this error comes from version 0.15.2, which is kinda old!!

I tried both uninstalling and reinstalling using vscode and I used in fine pycharm with virtualenv so as to isolate everything and have no interference. running my code with vscode and the reinstalled version on my Mac at version 0.18 I still get the same error "line 244, in _ask for item in messages[1]["suggestedResponses"]" While if I run everything on Pycharm with virtualenv by entering the 0.18.0 I get the error File "/Users/davide/PycharmProjects/pythonProject5/main.py", line 57, in ask_question response, suggested_responses = await sydney.ask(question, citations=True, suggestions=True) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/davide/PycharmProjects/pythonProject5/venv/lib/python3.11/site-packages/sydney/sydney.py", line 480, in ask async for response, suggested_responses in self._ask( File "/Users/davide/PycharmProjects/pythonProject5/venv/lib/python3.11/site-packages/sydney/sydney.py", line 392, in _ask raise CaptchaChallengeException("Solve CAPTCHA to continue") sydney.exceptions.CaptchaChallengeException: Solve CAPTCHA to continue I get this error both by entering only the "_U" and all the cookies

@vsakkas

"line 244, in _ask for item in messages[1]["suggestedResponses"] this error only occurs when the telegram bot has to look for the answer on the web, otherwise if I tell him "hello how are you" he answers me because he does not search the internet and also gives me the suggested answers, but as soon as I ask a question that has to look for boom this error appears

Hm, this one:

line 244, in _ask for item in messages[1]["suggestedResponses"]

Indicates that you're using version 0.15.2

This one

line 392, in _ask raise CaptchaChallengeException("Solve CAPTCHA to continue") 

Shows that you are likely using version 0.18.0!

So it seems that you have two versions of the library installed with 0.18.0 being the one installed on the virtual env. So, let's try to fix the Captcha error and the suggestedResponses error should go away as well :)

Ok @BlackDicky I installed the Cookies-Editor extension, but it seems that using that gives me also the same Captcha error, so it does not get exactly what we need. See if you can follow these steps to get the correct cookies, or as workaround, downgrade to 0.17.6 and continue using the `_U cookie!

@BlackDicky Another update, I think I found a bug in the library, but it should be possible to fix how it handles the cookies so that it works with the Cookies-Editor extension!

I am currently testing a fix for this

Now I'll try right away with the method you linked to extract cookies... I'll update you <3 @vsakkas

Ok, I always tried on Pycharm in virtualenv I copied the value "Cookie" going with f12 writing a message inside coopilot sending it and appeared "createbundle? Etc..." I clicked on it and found the cookies that I copied all in full. I went into the code and pasted inside os.environ["BING_COOKIES"] = MUID...etc...(cookie) But I keep getting this error:Traceback (most recent call last): File "/Users/davide/PycharmProjects/pythonProject5/main.py", line 79, in <module> bot.polling() File "/Users/davide/PycharmProjects/pythonProject5/venv/lib/python3.11/site-packages/telebot/__init__.py", line 1043, in polling self.__threaded_polling(non_stop=non_stop, interval=interval, timeout=timeout, long_polling_timeout=long_polling_timeout, File "/Users/davide/PycharmProjects/pythonProject5/venv/lib/python3.11/site-packages/telebot/__init__.py", line 1118, in __threaded_polling raise e File "/Users/davide/PycharmProjects/pythonProject5/venv/lib/python3.11/site-packages/telebot/__init__.py", line 1074, in __threaded_polling self.worker_pool.raise_exceptions() File "/Users/davide/PycharmProjects/pythonProject5/venv/lib/python3.11/site-packages/telebot/util.py", line 147, in raise_exceptions raise self.exception_info File "/Users/davide/PycharmProjects/pythonProject5/venv/lib/python3.11/site-packages/telebot/util.py", line 90, in run task(*args, **kwargs) File "/Users/davide/PycharmProjects/pythonProject5/main.py", line 50, in process_question asyncio.run(ask_question(message.chat.id, question)) File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/runners.py", line 190, in run return runner.run(main) ^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/runners.py", line 118, in run return self._loop.run_until_complete(task) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/base_events.py", line 650, in run_until_complete return future.result() ^^^^^^^^^^^^^^^ File "/Users/davide/PycharmProjects/pythonProject5/main.py", line 57, in ask_question response, suggested_responses = await sydney.ask(question, citations=True, suggestions=True) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/davide/PycharmProjects/pythonProject5/venv/lib/python3.11/site-packages/sydney/sydney.py", line 480, in ask async for response, suggested_responses in self._ask( File "/Users/davide/PycharmProjects/pythonProject5/venv/lib/python3.11/site-packages/sydney/sydney.py", line 392, in _ask raise CaptchaChallengeException("Solve CAPTCHA to continue") sydney.exceptions.CaptchaChallengeException: Solve CAPTCHA to continue

@vsakkas

Hm, the network request you're looking for should be named create?conversationID=.

But, I just released 0.18.1 which should make it possible to use the library by using Cookies-Editor and exporting just like you did before, so hopefully that should make it earlier to get the cookies (and work 🤞 )

Thank you for your patience so far!

Ok then now I try to see this new version, and I try to insert "headers string" as I did before with cookie editor.

Thanks to you for your patience, I hope to help you understand this mistake as soon as possible <3

I just tried and manages to send me the answers on telegram even if he has to look for them from the internet (obviously I'm using 0.18.1) and I copied "headers string" and pasted it into BING_COOKIES. but the joy and duration is short because if I now try to ask questions I get this error:

/Users/davide/PycharmProjects/pythonProject6/venv/bin/python /Users/davide/PycharmProjects/pythonProject6/main.py 
2023-11-26 19:19:08,934 (__init__.py:1083 MainThread) ERROR - TeleBot: "Threaded polling exception: A request to the Telegram API was unsuccessful. Error code: 400. Description: Bad Request: can't parse entities: Can't find end of the entity starting at byte offset 386"
2023-11-26 19:19:08,936 (__init__.py:1085 MainThread) ERROR - TeleBot: "Exception traceback:
Traceback (most recent call last):
  File "/Users/davide/PycharmProjects/pythonProject6/venv/lib/python3.11/site-packages/telebot/__init__.py", line 1074, in __threaded_polling
    self.worker_pool.raise_exceptions()
  File "/Users/davide/PycharmProjects/pythonProject6/venv/lib/python3.11/site-packages/telebot/util.py", line 147, in raise_exceptions
    raise self.exception_info
  File "/Users/davide/PycharmProjects/pythonProject6/venv/lib/python3.11/site-packages/telebot/util.py", line 90, in run
    task(*args, **kwargs)
  File "/Users/davide/PycharmProjects/pythonProject6/main.py", line 50, in process_question
    asyncio.run(ask_question(message.chat.id, question))
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/runners.py", line 190, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/base_events.py", line 650, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/Users/davide/PycharmProjects/pythonProject6/main.py", line 59, in ask_question
    bot.send_message(chat_id, response, parse_mode="Markdown")
  File "/Users/davide/PycharmProjects/pythonProject6/venv/lib/python3.11/site-packages/telebot/__init__.py", line 1549, in send_message
    apihelper.send_message(
  File "/Users/davide/PycharmProjects/pythonProject6/venv/lib/python3.11/site-packages/telebot/apihelper.py", line 264, in send_message
    return _make_request(token, method_url, params=payload, method='post')
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/davide/PycharmProjects/pythonProject6/venv/lib/python3.11/site-packages/telebot/apihelper.py", line 162, in _make_request
    json_result = _check_result(method_name, result)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/davide/PycharmProjects/pythonProject6/venv/lib/python3.11/site-packages/telebot/apihelper.py", line 189, in _check_result
    raise ApiTelegramException(method_name, result, result_json)
telebot.apihelper.ApiTelegramException: A request to the Telegram API was unsuccessful. Error code: 400. Description: Bad Request: can't parse entities: Can't find end of the entity starting at byte offset 386
"

Process finished with exit code 0 ```
Although I also changed the bot token to a new one

@vsakkas

Hm, this seems to be related to the telebot library, not Sydney.py itself.

I suggest you use print and pprint in your code to see what you're sending to telebot. Perhaps you need to format the output before you use it!

If I ask a question that does not have to search on the internet it does not give me any error, as soon as I make one that requires internet search I get this error:sydney.exceptions.ConnectionTimeoutException: Failed to connect to Copilot, connection timed out @vsakkas

It has many bugs beyond this, such as that sometimes it doesn't respond at all many timeouterror errors, in conclusion and went the first time as soon as I asked a question that had to look on the internet to the second started to give all these errors that I'm mentioning to you @vsakkasAh and very often I'm still getting the error: SolveCaptcha despite on coopilot it doesn't ask me to solve any captcha

UPDATE: I have inserted the cookies with the F12 method and it seems that now it gives me answers both without internet and with without giving errors, I have already tried several questions. in case there are other errors to follow I will report here @vsakkas

  • I can't confirm that it works with cookie-editor

  • I confirm that it works with the original method (F12)

V.018.1

@BlackDicky The sydney.exceptions.ConnectionTimeoutException: Failed to connect to Copilot, connection timed out can sometimes appear, in this case typically waiting and retrying can help.

Regarding the Captcha error, you can try to get new cookies, or if you see this error, go to copilot web page, write a message to get the verification response and then retry

It's good to know that the original method works for you!

As for the Captcha error, you can try to get new cookies, or if you see this error, go to the copilot web page, write a message to get the verification response and then try again

I am noticing that the Captcha problem is very frequent. If I go to the coopilot and write a message it does not appear "Verification in progress" as it normally should appear to me, but it responds to me automatically, returning to the script it tells me again to do this captcha check that does not appear even when I write on the copilot @vsakkas

Also sydney.exceptions.ConnectionTimeoutException: Failed to connect to Copilot, connection timed out appears very often making the bot almost unusable

Hm, I will try to reproduce those issues, although usually they do not show up when I test features/fixes for the library.

Regarding the captcha error, I would suggest closing the browser and starting a fresh conversation with Bing Chat and see if the new cookies help. In my case, if the cookies work just once, then I stop seeing Captcha errors 🤔

I solved everything and thank you for all the time you have dedicated to me I also updated to 0.19.0 and I implemented the much and beloved function we have been waiting for! Of the "web Search" you are great !! By any chance you also created a method on how to get images with Bing image creator ? 💘 @vsakkas

Glad to know that you got your code working @BlackDicky !
Unfortunately, right now there is no way to generate images from Sydney.py (but you can do the opposite, provide images through attachment parameter), but it is something that I'd like to add in the upcoming weeks :)

I'm implementing many functions in the bot, maybe we can open a new project together and insert as many cool and useful things as possible inside this bot, make a cli version or create a totally revolutionary bot! @vsakkas