summaryrefslogtreecommitdiffstats
path: root/g4f/Provider/needs_auth/OpenaiChat.py
diff options
context:
space:
mode:
authorHeiner Lohaus <hlohaus@users.noreply.github.com>2024-04-18 20:18:51 +0200
committerHeiner Lohaus <hlohaus@users.noreply.github.com>2024-04-18 20:18:51 +0200
commit7e543f4747914f280454f09b070ead500fb72277 (patch)
tree94e4680f009d25d2437895262e50316c2020b80e /g4f/Provider/needs_auth/OpenaiChat.py
parentAdd missing styling on phone (diff)
downloadgpt4free-7e543f4747914f280454f09b070ead500fb72277.tar
gpt4free-7e543f4747914f280454f09b070ead500fb72277.tar.gz
gpt4free-7e543f4747914f280454f09b070ead500fb72277.tar.bz2
gpt4free-7e543f4747914f280454f09b070ead500fb72277.tar.lz
gpt4free-7e543f4747914f280454f09b070ead500fb72277.tar.xz
gpt4free-7e543f4747914f280454f09b070ead500fb72277.tar.zst
gpt4free-7e543f4747914f280454f09b070ead500fb72277.zip
Diffstat (limited to 'g4f/Provider/needs_auth/OpenaiChat.py')
-rw-r--r--g4f/Provider/needs_auth/OpenaiChat.py28
1 files changed, 16 insertions, 12 deletions
diff --git a/g4f/Provider/needs_auth/OpenaiChat.py b/g4f/Provider/needs_auth/OpenaiChat.py
index b34daa3e..07620c80 100644
--- a/g4f/Provider/needs_auth/OpenaiChat.py
+++ b/g4f/Provider/needs_auth/OpenaiChat.py
@@ -23,7 +23,7 @@ except ImportError:
from ..base_provider import AsyncGeneratorProvider, ProviderModelMixin
from ...webdriver import get_browser
-from ...typing import AsyncResult, Messages, Cookies, ImageType, Union, AsyncIterator
+from ...typing import AsyncResult, Messages, Cookies, ImageType, AsyncIterator
from ...requests import get_args_from_browser, raise_for_status
from ...requests.aiohttp import StreamSession
from ...image import to_image, to_bytes, ImageResponse, ImageRequest
@@ -35,7 +35,7 @@ from ... import debug
class OpenaiChat(AsyncGeneratorProvider, ProviderModelMixin):
"""A class for creating and managing conversations with OpenAI chat service"""
- lebel = "OpenAI ChatGPT"
+ label = "OpenAI ChatGPT"
url = "https://chat.openai.com"
working = True
supports_gpt_35_turbo = True
@@ -295,7 +295,7 @@ class OpenaiChat(AsyncGeneratorProvider, ProviderModelMixin):
model: str,
messages: Messages,
proxy: str = None,
- timeout: int = 120,
+ timeout: int = 180,
api_key: str = None,
cookies: Cookies = None,
auto_continue: bool = False,
@@ -348,7 +348,7 @@ class OpenaiChat(AsyncGeneratorProvider, ProviderModelMixin):
if api_key is not None:
cls._set_api_key(api_key)
- if cls.default_model is None and cls._api_key is not None:
+ if cls.default_model is None and (not cls.needs_auth or cls._api_key is not None):
try:
if not model:
cls.default_model = cls.get_model(await cls.get_default_model(session, cls._headers))
@@ -368,12 +368,12 @@ class OpenaiChat(AsyncGeneratorProvider, ProviderModelMixin):
arkose_token, api_key, cookies = await getArkoseAndAccessToken(proxy)
cls._create_request_args(cookies)
cls._set_api_key(api_key)
- except NoValidHarFileError:
+ except NoValidHarFileError as e:
...
if cls._api_key is None:
- if debug.logging:
- print("Getting access token with nodriver.")
await cls.nodriver_access_token()
+ if cls._api_key is None and cls.needs_auth:
+ raise e
cls.default_model = cls.get_model(await cls.get_default_model(session, cls._headers))
async with session.post(
@@ -589,10 +589,11 @@ this.fetch = async (url, options) => {
user_data_dir = user_config_dir("g4f-nodriver")
except:
user_data_dir = None
-
+ if debug.logging:
+ print(f"Open nodriver with user_dir: {user_data_dir}")
browser = await uc.start(user_data_dir=user_data_dir)
page = await browser.get("https://chat.openai.com/")
- while await page.query_selector("#prompt-textarea") is None:
+ while await page.find("[id^=headlessui-menu-button-]") is None:
await asyncio.sleep(1)
api_key = await page.evaluate(
"(async () => {"
@@ -609,8 +610,9 @@ this.fetch = async (url, options) => {
for c in await page.browser.cookies.get_all():
if c.domain.endswith("chat.openai.com"):
cookies[c.name] = c.value
+ user_agent = await page.evaluate("window.navigator.userAgent")
await page.close()
- cls._create_request_args(cookies)
+ cls._create_request_args(cookies, user_agent)
cls._set_api_key(api_key)
@classmethod
@@ -662,7 +664,7 @@ this.fetch = async (url, options) => {
"content-type": "application/json",
"oai-device-id": str(uuid.uuid4()),
"oai-language": "en-US",
- "sec-ch-ua": "\"Chromium\";v=\"122\", \"Not(A:Brand\";v=\"24\", \"Google Chrome\";v=\"122\"",
+ "sec-ch-ua": "\"Google Chrome\";v=\"123\", \"Not:A-Brand\";v=\"8\", \"Chromium\";v=\"123\"",
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": "\"Linux\"",
"sec-fetch-dest": "empty",
@@ -675,8 +677,10 @@ this.fetch = async (url, options) => {
return "; ".join(f"{k}={v}" for k, v in cookies.items() if k != "access_token")
@classmethod
- def _create_request_args(cls, cookies: Cookies = None):
+ def _create_request_args(cls, cookies: Cookies = None, user_agent: str = None):
cls._headers = cls.get_default_headers()
+ if user_agent is not None:
+ cls._headers["user-agent"] = user_agent
cls._cookies = {} if cookies is None else cookies
cls._update_cookie_header()