summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHeiner Lohaus <heiner@lohaus.eu>2023-10-03 23:53:17 +0200
committerHeiner Lohaus <heiner@lohaus.eu>2023-10-03 23:53:17 +0200
commit5b2efa4aaf00cb5b5b169c83f2690630495a962e (patch)
tree6cf0aa0eb496f606ea5fe0b91de809df199154f0
parentAdd streaming in openai chat (diff)
downloadgpt4free-5b2efa4aaf00cb5b5b169c83f2690630495a962e.tar
gpt4free-5b2efa4aaf00cb5b5b169c83f2690630495a962e.tar.gz
gpt4free-5b2efa4aaf00cb5b5b169c83f2690630495a962e.tar.bz2
gpt4free-5b2efa4aaf00cb5b5b169c83f2690630495a962e.tar.lz
gpt4free-5b2efa4aaf00cb5b5b169c83f2690630495a962e.tar.xz
gpt4free-5b2efa4aaf00cb5b5b169c83f2690630495a962e.tar.zst
gpt4free-5b2efa4aaf00cb5b5b169c83f2690630495a962e.zip
-rw-r--r--g4f/Provider/OpenaiChat.py26
1 files changed, 16 insertions, 10 deletions
diff --git a/g4f/Provider/OpenaiChat.py b/g4f/Provider/OpenaiChat.py
index fbd26d7c..ca148da2 100644
--- a/g4f/Provider/OpenaiChat.py
+++ b/g4f/Provider/OpenaiChat.py
@@ -63,7 +63,7 @@ class OpenaiChat(AsyncGeneratorProvider):
last_message = new_message
@classmethod
- def fetch_access_token(cls) -> str:
+ def browse_access_token(cls) -> str:
try:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
@@ -86,18 +86,24 @@ class OpenaiChat(AsyncGeneratorProvider):
driver.quit()
@classmethod
+ async def fetch_access_token(cls, cookies: dict, proxies: dict = None) -> str:
+ async with StreamSession(proxies=proxies, cookies=cookies, impersonate="chrome107") as session:
+ async with session.get(f"{cls.url}/api/auth/session") as response:
+ response.raise_for_status()
+ auth = await response.json()
+ if "accessToken" in auth:
+ return auth["accessToken"]
+
+ @classmethod
async def get_access_token(cls, cookies: dict = None, proxies: dict = None) -> str:
if not cls._access_token:
cookies = cookies if cookies else get_cookies("chat.openai.com")
- async with StreamSession(proxies=proxies, cookies=cookies, impersonate="chrome107") as session:
- async with session.get(f"{cls.url}/api/auth/session") as response:
- response.raise_for_status()
- auth = await response.json()
- if "accessToken" in auth:
- cls._access_token = auth["accessToken"]
- cls._access_token = cls.fetch_access_token()
- if not cls._access_token:
- raise RuntimeError("Missing access token")
+ if cookies:
+ cls._access_token = await cls.fetch_access_token(cookies, proxies)
+ if not cls._access_token:
+ cls._access_token = cls.browse_access_token()
+ if not cls._access_token:
+ raise RuntimeError("Read access token failed")
return cls._access_token
@classmethod