Skip to content

Commit

Permalink
Add nodriver to OpenaiChat
Browse files Browse the repository at this point in the history
  • Loading branch information
hlohaus committed Apr 7, 2024
1 parent b35dfcd commit bdc61ca
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
38 changes: 38 additions & 0 deletions g4f/Provider/needs_auth/OpenaiChat.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,8 @@ async def create_async_generator(
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
Expand Down Expand Up @@ -365,6 +367,8 @@ async def create_async_generator(
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(
Expand Down Expand Up @@ -566,6 +570,40 @@ async def webview_access_token(cls) -> str:
cls._expires = int(time.time()) + 60 * 60 * 4
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:
"""
Expand Down
3 changes: 2 additions & 1 deletion g4f/client/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion g4f/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ def create(
)
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,
Expand Down
2 changes: 1 addition & 1 deletion g4f/gui/client/static/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit bdc61ca

Please sign in to comment.