diff options
author | Ryan Jordan <ryjordan@gmail.com> | 2023-09-06 02:39:57 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-06 02:39:57 +0200 |
commit | f81e618958318a092ca4c70a1b3ea15260bda97c (patch) | |
tree | 3ce022a8d719011268da7f1a0befc97bf32a60d8 /g4f/Provider/OpenaiChat.py | |
parent | feat(docker): add Docker and Docker Compose support (diff) | |
parent | ~ | Merge pull request #869 from ahobsonsayers/add-console-script (diff) | |
download | gpt4free-f81e618958318a092ca4c70a1b3ea15260bda97c.tar gpt4free-f81e618958318a092ca4c70a1b3ea15260bda97c.tar.gz gpt4free-f81e618958318a092ca4c70a1b3ea15260bda97c.tar.bz2 gpt4free-f81e618958318a092ca4c70a1b3ea15260bda97c.tar.lz gpt4free-f81e618958318a092ca4c70a1b3ea15260bda97c.tar.xz gpt4free-f81e618958318a092ca4c70a1b3ea15260bda97c.tar.zst gpt4free-f81e618958318a092ca4c70a1b3ea15260bda97c.zip |
Diffstat (limited to 'g4f/Provider/OpenaiChat.py')
-rw-r--r-- | g4f/Provider/OpenaiChat.py | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/g4f/Provider/OpenaiChat.py b/g4f/Provider/OpenaiChat.py new file mode 100644 index 00000000..f2d1ed6f --- /dev/null +++ b/g4f/Provider/OpenaiChat.py @@ -0,0 +1,86 @@ +from __future__ import annotations + +has_module = True +try: + from revChatGPT.V1 import AsyncChatbot +except ImportError: + has_module = False + +import json + +from httpx import AsyncClient + +from ..typing import AsyncGenerator +from .base_provider import AsyncGeneratorProvider, format_prompt, get_cookies + + +class OpenaiChat(AsyncGeneratorProvider): + url = "https://chat.openai.com" + needs_auth = True + working = has_module + supports_gpt_35_turbo = True + supports_gpt_4 = True + supports_stream = True + _access_token = None + + @classmethod + async def create_async_generator( + cls, + model: str, + messages: list[dict[str, str]], + proxy: str = None, + access_token: str = _access_token, + cookies: dict = None, + **kwargs: dict + ) -> AsyncGenerator: + + config = {"access_token": access_token, "model": model} + if proxy: + if "://" not in proxy: + proxy = f"http://{proxy}" + config["proxy"] = proxy + + bot = AsyncChatbot( + config=config + ) + + if not access_token: + cookies = cookies if cookies else get_cookies("chat.openai.com") + cls._access_token = await get_access_token(bot.session, cookies) + bot.set_access_token(cls._access_token) + + returned = None + async for message in bot.ask(format_prompt(messages)): + message = message["message"] + if returned: + if message.startswith(returned): + new = message[len(returned):] + if new: + yield new + else: + yield message + returned = message + + await bot.delete_conversation(bot.conversation_id) + + + @classmethod + @property + def params(cls): + params = [ + ("model", "str"), + ("messages", "list[dict[str, str]]"), + ("stream", "bool"), + ("proxy", "str"), + ] + param = ", ".join([": ".join(p) for p in params]) + return f"g4f.provider.{cls.__name__} supports: ({param})" + + +async def get_access_token(session: AsyncClient, cookies: dict): + response = await session.get("https://chat.openai.com/api/auth/session", cookies=cookies) + response.raise_for_status() + try: + return response.json()["accessToken"] + except json.decoder.JSONDecodeError: + raise RuntimeError(f"Response: {response.text}")
\ No newline at end of file |