xtekky / gpt4free

The official gpt4free repository | various collection of powerful language models
https://g4f.ai
GNU General Public License v3.0
61.73k stars 13.35k forks source link

How to use cookies in Python #1638

Closed gamelist1990 closed 7 months ago

gamelist1990 commented 8 months ago

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

gamelist1990 commented 8 months ago

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

hlohaus commented 8 months ago

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.

gamelist1990 commented 8 months ago

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.

hlohaus commented 8 months ago

Do you have a error message?

gamelist1990 commented 8 months ago

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 commented 8 months ago

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

hlohaus commented 8 months ago

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.

gamelist1990 commented 8 months ago

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

gamelist1990 commented 8 months ago

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:

hlohaus commented 8 months ago

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="..."
)
gamelist1990 commented 8 months ago

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.

hlohaus commented 8 months ago

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?"},
]
gamelist1990 commented 8 months ago

ok

chekamarue commented 8 months ago

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

hlohaus commented 8 months ago

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.

chekamarue commented 8 months ago

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 !

OE-LUCIFER commented 8 months ago

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

chekamarue commented 8 months ago

@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)
github-actions[bot] commented 8 months ago

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] commented 8 months ago

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] commented 7 months ago

Closing due to inactivity.