Overview

Some providers require authentication or cookies to access their API. This guide will show you how to set cookies and authenticate with these providers in the g4f package.

Setting Cookies

Authentication

Troubleshooting

Example

Here’s an example of setting cookies and using authentication:

from g4f.cookies import set_cookies
from g4f.Provider import Bing, OpenaiChat

# Set cookies
set_cookies(".bing.com", {
  "_U": "your_bing_cookie_here"
})

# Create completion with Bing
response = Bing.create_completion(
    model="gpt-4",
    messages=[{"role": "user", "content": "Hello Bing"}]
)

# Create completion with OpenAI authentication
response = OpenaiChat.create_completion(
    model="gpt-3.5-turbo",
    messages=[{"role": "user", "content": "Hello OpenAI"}],
    auth="your_openai_access_token"
)

By setting the appropriate cookies and providing authentication, you can successfully use providers that require them.

Conclusion

Adding cookies and authentication is straightforward with the g4f package. Use the set_cookies function to manually set cookies or let the package automatically read them from your browser. For providers needing authentication, pass your access token using the auth parameter.