Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use this code with sessions and responses on previous requests #68

Open
AkiraShiro opened this issue Oct 13, 2023 · 5 comments
Open
Assignees

Comments

@AkiraShiro
Copy link

I tried to figure it out but it didn’t work out, I ask for help

@eforce67
Copy link

eforce67 commented Oct 16, 2023

Hey @AkiraShiro, the code example should have what you're looking for. Are you having trouble understanding it? Do you need an explanation? Please let us know so we can better help you! I am also using this library for one of my projects, so I have some knowledge of this library!

original code example

import asyncio
from sydney import SydneyClient

async def main() -> None:
    async with SydneyClient(bing_u_cookie='NONE') 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())

here below is my take on the code example:

# asyncio is our best friend
import asyncio

# Loading the imports from sydney and sydney.exceptions to handle errors
from sydney import SydneyClient
from sydney.exceptions import (ConversationLimitException,
                               NoConnectionException,
                               NoResponseException,
                               ThrottledRequestException,
                               CaptchaChallengeException,
                               )
# Example code by @Eforce67/Aka @Neonshark

"""
Please note: 
Your BingChat cookies are affiliated with your Microsoft account. You must be login, in order to find your "__U" cookies and use them.
The benefits of using cookies is that, with cookies you get to send more message to BingChat compare to having no cookies.

You don't need to provide the environment variable if the cookie is already being used in:
os.environ["BING_U_COOKIE"] = 'OPTIONAL' # Not needed

SydneyClient(bing_u_cookie='COOKIES_HERE') # specify your cookies!
"""

# Constants
BING_COOKIE = '<paste-cookies-here>' # or set it to anything if you don't want to use cookies!
TRIES = 0
MAX_ATTEMPT = 5

async def begin_conversation():
    while True:
        # SydneyClient already established a session. Try asking it a question about a previous question.
        async with SydneyClient(style='creative', # You can choose between the following: creative, balanced, precise
                                bing_u_cookie=BING_COOKIE, # Use cookies
                                use_proxy=None, # Proxy format should be: username@password:ip:port
                                ) as sydney: # sydney is our session. Think of it as sydney = requests.Session() but not literary.
            try:
                """
                You can use many methods to send a prompt to BingChat:
                async for response in sydney.ask_stream(prompt=input('prompt your question: '), citations=True):
                        print(response, end="", flush=True)
                
                The available options for the format parameter are:
                paragraph
                email
                blogpost
                ideas <------- ideas format is selected, see example below!
                
                response = sydney.compose(prompt=input('prompt your question: '), format="ideas")
                
                async for response in sydney.compose_stream(prompt=input('prompt your question: '), format="ideas"):
                    print(response, end="", flush=True)
                """
                print('Sending request to BingChat')
                GPT4_response = await sydney.ask(
                    prompt=input('prompt your question: '), # prompt you would like to send to BingChat
                    raw=False, # set to True if you would like to recieve json data
                    )
                #response = GPT4_response['item']['result'] # If raw is True, you can use this to get GPT4 response from raw json
                #print(response)
                print(GPT4_response)
            
            # Error handling incase of a situation...
            except (ConversationLimitException, NoConnectionException, NoResponseException, ThrottledRequestException) as error:
                if isinstance(error, ConversationLimitException):
                    print('The max amount of message(20 w cookies & 10 w/o cookies) we can send to BingChat has been reached')
                    sydney.reset_conversation() # resets the conversation once max message is reached
                elif isinstance(error, (NoConnectionException, NoResponseException, ThrottledRequestException)):
                    print('Unable to connect to BingChat, please check your internet connection and try again...')
                    if (MAX_ATTEMPT >= TRIES):
                        TRIES += 1
                        return await begin_conversation() # Try again by calling the function
                    else:
                        print('Exceed max attempts, now closing all activities')
                        break
                elif isinstance(error, CaptchaChallengeException):
                    """
                    Do something here to handle captcha problems. I recommend using proxies with a reputable IPscore
                    """
                    print('Unable to do anything as we got a captcha problem. Maybe try using proxies?')
                    break
                else:
                    # raise any errors besides the one's presented on the exceptions
                    print('We recieved the following errors:...')
                    raise
    
    sydney.close_conversation() # Encloses the conversation like cleaning up your mess after you're done using something.

if __name__ == "__main__":
    print(asyncio.run(begin_conversation()))

@AkiraShiro
Copy link
Author

AkiraShiro commented Oct 16, 2023

Hi @eforce67. Thank you for your decision, but unfortunately it was not a worker. It is easy to check if used print(GPT4_response['item']['throttling']['numUserMessagesInConversation']) (taken from your example). When sending a request, the number of messages already used for the session is issued, using your code the value is always 1.

example code:

async def begin_conversation():
    while True:
        # SydneyClient already established a session. Try asking it a question about a previous question.
        async with SydneyClient(style='creative',  # You can choose between the following: creative, balanced, precise
                                bing_u_cookie=BING_COOKIE,  # Use cookies
                                use_proxy=None,  # Proxy format should be: username@password:ip:port
                                ) as sydney:  # sydney is our session. Think of it as sydney = requests.Session() but not literary.
            try:
                """
                You can use many methods to send a prompt to BingChat:
                async for response in sydney.ask_stream(prompt=input('prompt your question: '), citations=True):
                        print(response, end="", flush=True)

                The available options for the format parameter are:
                paragraph
                email
                blogpost
                ideas <------- ideas format is selected, see example below!

                response = sydney.compose(prompt=input('prompt your question: '), format="ideas")

                async for response in sydney.compose_stream(prompt=input('prompt your question: '), format="ideas"):
                    print(response, end="", flush=True)
                """
                print('Sending request to BingChat')
                GPT4_response = await sydney.ask(
                    prompt=input('prompt your question: '),  # prompt you would like to send to BingChat
                    raw=True,  # set to True if you would like to recieve json data
                )
                # response = GPT4_response['item']['result'] # If raw is True, you can use this to get GPT4 response from raw json
                # print(response)
                print(GPT4_response['item']['throttling']['numUserMessagesInConversation']) # Always gives out a unit, although it should grow with the number of requests
                print(GPT4_response['item']['result']['message']) # Answer

            # Error handling incase of a situation...
            except (
            ConversationLimitException, NoConnectionException, NoResponseException, ThrottledRequestException) as error:
                if isinstance(error, ConversationLimitException):
                    print(
                        'The max amount of message(20 w cookies & 10 w/o cookies) we can send to BingChat has been reached')
                    sydney.reset_conversation()  # resets the conversation once max message is reached


    sydney.close_conversation()  # Encloses the conversation like cleaning up your mess after you're done using something.```

@eforce67
Copy link

Oh I see, so the value isn't changing for numUserMessagesInConversation? I'll try to see a workaround for this.

@vsakkas
Copy link
Owner

vsakkas commented Oct 19, 2023

Hi @AkiraShiro I had a quick look an it seems that the numUserMessagesInConversation value is indeed increasing when we send multiple prompts. Example code:

async def main() -> None:
    async with SydneyClient() as sydney:
        response = await sydney.ask("What does this picture show?", attachment=url)

        print(response) # Will describe a photo of a golden retriever.

        response = await sydney.ask(
            "Can you tell me something interesting about it?", attachment=url
        )

        print(response)

        response = await sydney.compose(
            prompt="Why they are a great pet",
            format="ideas",

        )

        print(response)

I went through this with a debugger and saw that in the end throttling looked like this:

{
    "maxNumLongDocSummaryUserMessagesInConversation": 50,
    "maxNumUserMessagesInConversation": 30,
    "numLongDocSummaryUserMessagesInConversation": 0,
    "numUserMessagesInConversation": 3
}

I haven't tested myself the above code, but I see that async with SydneyClient() is inside the while True. This means that every time, the conversation gets restarted. Is this the behavior you're looking for?

@vsakkas vsakkas self-assigned this Oct 19, 2023
@AkiraShiro
Copy link
Author

AkiraShiro commented Oct 19, 2023

Hi @vsakkas. Yes, I’m trying to find a way to record a telegram user so that he can use a separate session, but my attempts led to the reset of the session after sending the next message. It would be nice if you added instructions to your session code

upd. This my code with create session

x = SydneyClient()
y = SydneyClient()
sessions = {}
async def main() -> None:
    sessions['1'] = x
    sessions['2'] = y
    while True:
        if input('Session') == '1':
            print('Session', sessions)
            async with sessions['1'] as sydney:
                print(sessions['1'])
                response = await sydney.ask(str(input('Session 1 prompt: ')), raw = True)
            print(response['item']['result']['message'])
            print(response['item']['throttling']['numUserMessagesInConversation'])
        else:
            async with sessions['2'] as sydney:
                print(sessions['2'])
                response = await sydney.ask(str(input('Session 2 prompt: ')), raw=True)
            print(response['item']['result']['message'])
            print(response['item']['throttling']['numUserMessagesInConversation'])

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants