gpt4free icon indicating copy to clipboard operation
gpt4free copied to clipboard

How to use cookies in Python

Open gamelist1990 opened this issue 4 months ago • 20 comments

I want to embed cookies in gemini, how do I embed them in the g4f.ChatCompletion.create function?

gamelist1990 avatar Feb 28 '24 04:02 gamelist1990

How do I fix this error? use linux server

Using Gemini provider ERROR:root:Error occurred: Response: ERROR:root:Error occurred: Binary Location Must be a String INFO:undetected_chromedriver.patcher:patching driver executable /opt/render/.local/share/undetected_chromedriver/undetected_chromedriver

gamelist1990 avatar Feb 28 '24 04:02 gamelist1990

That's from the Webdriver. You need Google Chrome for that. But you can disable the Webdriver, if you set cookies. pip uninstall undetected-chromedriver

The cookies can added as Dict to the create:

g4f.ChatCompletion.create(
    ...,
    cookies={
        "_U": "cookie value",
        ...
    }
)

You can also try GeminiPro. It need only a api_key.

hlohaus avatar Feb 28 '24 07:02 hlohaus

response = await g4f.ChatCompletion.create_async( model=g4f.models.default, provider=g4f.Provider.Gemini, messages=geminis[user_id], cookies={ "__Secure-1PSID": "xxxxxxxxxxxxxxxxxxxxxxx" } ) It works fine in the window, but on the Lumix server it doesn't work.

gamelist1990 avatar Feb 28 '24 09:02 gamelist1990

Do you have a error message?

hlohaus avatar Feb 28 '24 11:02 hlohaus

Yes, sir. INFO:undetected_chromedriver.patcher:patching driver executable /opt/render/.local/share/undetected_chromedriver/undetected_chromedriver ERROR:root:Error occurred: Binary Location Must be a String Using Gemini provider Error reading cookies from chrome for .google.com: 'DBUS_SESSION_BUS_ADDRESS' Error reading cookies from chromium for .google.com: 'DBUS_SESSION_BUS_ADDRESS' Error reading cookies from opera for .google.com: 'DBUS_SESSION_BUS_ADDRESS' Error reading cookies from opera_gx for .google.com: 'DBUS_SESSION_BUS_ADDRESS' Error reading cookies from brave for .google.com: 'DBUS_SESSION_BUS_ADDRESS' Error reading cookies from edge for .google.com: 'DBUS_SESSION_BUS_ADDRESS' Error reading cookies from vivaldi for .google.com: 'DBUS_SESSION_BUS_ADDRESS' Open browser with config dir: /opt/render/.config/g4f

gamelist1990 avatar Feb 28 '24 13:02 gamelist1990

Cookies={} is not loaded and Open Browser with config dir: is started. set_cookies={} is loaded, but it is unstable on Lumix.

For Cookies={}
Logging :Using Gemini provider Open browser with config dir: C:\Users\xxx\AppData\Local\g4f\g4f INFO:undetected_chromedriver.patcher:patching driver executable C:\Users\xxx\appdata\roaming\undetected_chromedriver\undetected_chromedriver.exe

For set_cookies={} Logging : Using Gemini provider Read cookies from chrome for .google.com Using Gemini provider INFO: 192.168.128.100:30960 - "GET /ask?text=HELLO HTTP/1.1" 200 OK

gamelist1990 avatar Feb 28 '24 13:02 gamelist1990

You need Google Chrome for the Webdriver. Or you uninstall it: pip uninstall undetected_chromedriver Also you can uninstall: pip uninstall browser_cookie3 on your Mac.

Why you not use "GeminiPro". It not requiring cookies. Only a api_key, what you get for free.

hlohaus avatar Feb 28 '24 16:02 hlohaus

I want to do asynchronous processing, can't I use create_async?

gamelist1990 avatar Feb 28 '24 18:02 gamelist1990

When I generate api_key with GeminiPro and start it on the Lumix server, it worked the first time, but after the second time, an error occurred.

ERROR:root:Error occurred: RetryProvider failed:

gamelist1990 avatar Feb 28 '24 19:02 gamelist1990

You have to use GeminiPro as Provider not as Model. You can select the Provider in the arguments or as url parameter:

v1/chat/completions?provider=GeminiPro

create_async is currently only available in the legacy API:

response = await g4f.ChatCompletions.create_async(
    model="",
    messages,
    provider="GeminiPro",
    api_key="..."
)

hlohaus avatar Feb 28 '24 21:02 hlohaus

Using GeminiPro provider ERROR:root:Error occurred: Please ensure that multiturn requests ends with a user role or a function response. ERROR:root:Error occurred: Please ensure that multiturn requests ends with a user role or a function response.

It still works fine on windows, but on Lumix it works fine the first time and causes an error the second and subsequent times.

gamelist1990 avatar Feb 29 '24 08:02 gamelist1990

The last message must be a user message. Like the error message says. You should remove the old response, if a second response are wanted.

messages= [
   {"role": "user", "content": "Hi Bot"},
   {"role": "assistant", "content": "Hi User"},
   {"role": "user", "content": "How are you?"},
]

hlohaus avatar Feb 29 '24 09:02 hlohaus

ok

gamelist1990 avatar Feb 29 '24 11:02 gamelist1990

@hlohaus can you please tell me how to use cookies forchat.openai.com provider ! i don't know wiche cookies should i pass

chekamarue avatar Feb 29 '24 20:02 chekamarue

Okay, you only have to open this url: https://chat.openai.com/api/auth/session If you have a session, you will see a Json with a "accessToken". Copy the AccesToken and use them as api_key for OpenaiChat.

hlohaus avatar Feb 29 '24 20:02 hlohaus

Okay, you only have to open this url: https://chat.openai.com/api/auth/session If have a session, you will see a Json with a "accessToken". Copy the AccesToken and use them as api_key for OpenaiChat.

You guys are doing an amazing work 👍 Thank you very much, it worked. I been seeking for it since a while !

chekamarue avatar Feb 29 '24 21:02 chekamarue

@chekamarue can you give me that code of chat.openai

OE-LUCIFER avatar Mar 01 '24 10:03 OE-LUCIFER

@OE-LUCIFER

Here a ready to use Python code for OpenaiChat provider

import g4f
from g4f.Provider import OpenaiChat

g4f.debug.logging = True
g4f.check_version = True

def handle_chat(prompt):
    response = g4f.ChatCompletion.create(
        model=g4f.models.default,
        provider=OpenaiChat,
        messages=[{"role": "user", "content": prompt}],
        auto_continue= "True", #OpenAi
        access_token="", #get it from https://chat.openai.com/api/auth/session
        )
    return response
    
prompt='tell me a funny ai story'
message=handle_chat(prompt)
print(message)

chekamarue avatar Mar 01 '24 12:03 chekamarue

Bumping this issue because it has been open for 7 days with no activity. Closing automatically in 7 days unless it becomes active again.

github-actions[bot] avatar Mar 09 '24 00:03 github-actions[bot]

Bumping this issue because it has been open for 7 days with no activity. Closing automatically in 7 days unless it becomes active again.

github-actions[bot] avatar Mar 19 '24 00:03 github-actions[bot]

Closing due to inactivity.

github-actions[bot] avatar Apr 20 '24 00:04 github-actions[bot]