diff options
author | Heiner Lohaus <hlohaus@users.noreply.github.com> | 2024-04-07 16:02:49 +0200 |
---|---|---|
committer | Heiner Lohaus <hlohaus@users.noreply.github.com> | 2024-04-07 16:02:49 +0200 |
commit | bdc61ca273dde2771c5bc8f93705652842f78d15 (patch) | |
tree | 9074b23a7b1beac359206910e30e205908513a1d | |
parent | Add local models to gui, Fix You Provider, add AsyncClient (diff) | |
download | gpt4free-bdc61ca273dde2771c5bc8f93705652842f78d15.tar gpt4free-bdc61ca273dde2771c5bc8f93705652842f78d15.tar.gz gpt4free-bdc61ca273dde2771c5bc8f93705652842f78d15.tar.bz2 gpt4free-bdc61ca273dde2771c5bc8f93705652842f78d15.tar.lz gpt4free-bdc61ca273dde2771c5bc8f93705652842f78d15.tar.xz gpt4free-bdc61ca273dde2771c5bc8f93705652842f78d15.tar.zst gpt4free-bdc61ca273dde2771c5bc8f93705652842f78d15.zip |
-rw-r--r-- | g4f/Provider/needs_auth/OpenaiChat.py | 38 | ||||
-rw-r--r-- | g4f/client/async_client.py | 3 | ||||
-rw-r--r-- | g4f/client/client.py | 3 | ||||
-rw-r--r-- | g4f/gui/client/static/css/style.css | 2 |
4 files changed, 43 insertions, 3 deletions
diff --git a/g4f/Provider/needs_auth/OpenaiChat.py b/g4f/Provider/needs_auth/OpenaiChat.py index 921d7394..64e3aeac 100644 --- a/g4f/Provider/needs_auth/OpenaiChat.py +++ b/g4f/Provider/needs_auth/OpenaiChat.py @@ -338,6 +338,8 @@ class OpenaiChat(AsyncGeneratorProvider, ProviderModelMixin): impersonate="chrome", timeout=timeout ) as session: + if cls._expires is not None and cls._expires < time.time(): + cls._headers = cls._api_key = None if cls._headers is None or cookies is not None: cls._create_request_args(cookies) api_key = kwargs["access_token"] if "access_token" in kwargs else api_key @@ -365,6 +367,8 @@ class OpenaiChat(AsyncGeneratorProvider, ProviderModelMixin): cls._set_api_key(api_key) except NoValidHarFileError: ... + if cls._api_key is None: + await cls.nodriver_access_token() cls.default_model = cls.get_model(await cls.get_default_model(session, cls._headers)) async with session.post( @@ -567,6 +571,40 @@ this.fetch = async (url, options) => { cls._update_cookie_header() @classmethod + async def nodriver_access_token(cls): + try: + import nodriver as uc + except ImportError: + return + try: + from platformdirs import user_config_dir + user_data_dir = user_config_dir("g4f-nodriver") + except: + user_data_dir = None + 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: + await asyncio.sleep(1) + api_key = await page.evaluate( + "(async () => {" + "let session = await fetch('/api/auth/session');" + "let data = await session.json();" + "let accessToken = data['accessToken'];" + "let expires = new Date(); expires.setTime(expires.getTime() + 60 * 60 * 4 * 1000);" + "document.cookie = 'access_token=' + accessToken + ';expires=' + expires.toUTCString() + ';path=/';" + "return accessToken;" + "})();", + await_promise=True + ) + cookies = {} + for c in await page.browser.cookies.get_all(): + if c.domain.endswith("chat.openai.com"): + cookies[c.name] = c.value + await page.close() + cls._create_request_args(cookies) + cls._set_api_key(api_key) + + @classmethod def browse_access_token(cls, proxy: str = None, timeout: int = 1200) -> None: """ Browse to obtain an access token. diff --git a/g4f/client/async_client.py b/g4f/client/async_client.py index 25de1c76..bab2fde9 100644 --- a/g4f/client/async_client.py +++ b/g4f/client/async_client.py @@ -87,7 +87,8 @@ def create_response( else: create = provider.create_completion response = create( - model, messages, stream, + model, messages, + stream=stream, **filter_none( proxy=proxy, max_tokens=max_tokens, diff --git a/g4f/client/client.py b/g4f/client/client.py index 8ce5d932..bc0bae6f 100644 --- a/g4f/client/client.py +++ b/g4f/client/client.py @@ -98,7 +98,8 @@ class Completions(): ) stop = [stop] if isinstance(stop, str) else stop response = provider.create_completion( - model, messages, stream, + model, messages, + stream=stream, **filter_none( proxy=self.client.get_proxy() if proxy is None else proxy, max_tokens=max_tokens, diff --git a/g4f/gui/client/static/css/style.css b/g4f/gui/client/static/css/style.css index b0f7a4a0..4db65863 100644 --- a/g4f/gui/client/static/css/style.css +++ b/g4f/gui/client/static/css/style.css @@ -1018,7 +1018,7 @@ a:-webkit-any-link { width: 100%; color: var(--colour-3); min-height: 50px; - height: 50px; + height: 59px; outline: none; padding: var(--inner-gap) var(--section-gap); resize: vertical; |