diff options
author | H Lohaus <hlohaus@users.noreply.github.com> | 2024-03-26 21:50:35 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-26 21:50:35 +0100 |
commit | dd08125bb4740ab5dc1116868ff237ce51ae3d5e (patch) | |
tree | ed6339c66505409e7dced6d3499814b00446ff12 /g4f/Provider | |
parent | Merge pull request #1754 from hlohaus/goo (diff) | |
parent | Fix load .har files, add hardir to docker, add docs (diff) | |
download | gpt4free-0.2.7.1.tar gpt4free-0.2.7.1.tar.gz gpt4free-0.2.7.1.tar.bz2 gpt4free-0.2.7.1.tar.lz gpt4free-0.2.7.1.tar.xz gpt4free-0.2.7.1.tar.zst gpt4free-0.2.7.1.zip |
Diffstat (limited to 'g4f/Provider')
-rw-r--r-- | g4f/Provider/bing/conversation.py | 5 | ||||
-rw-r--r-- | g4f/Provider/needs_auth/OpenaiChat.py | 50 | ||||
-rw-r--r-- | g4f/Provider/openai/har_file.py | 36 |
3 files changed, 45 insertions, 46 deletions
diff --git a/g4f/Provider/bing/conversation.py b/g4f/Provider/bing/conversation.py index de5716b7..e9cdfe31 100644 --- a/g4f/Provider/bing/conversation.py +++ b/g4f/Provider/bing/conversation.py @@ -3,8 +3,9 @@ from __future__ import annotations from aiohttp import ClientSession from ...requests import raise_for_status from ...errors import RateLimitError +from ...providers.conversation import BaseConversation -class Conversation: +class Conversation(BaseConversation): """ Represents a conversation with specific attributes. """ @@ -32,7 +33,7 @@ async def create_conversation(session: ClientSession, headers: dict, tone: str) Returns: Conversation: An instance representing the created conversation. """ - if tone == "copilot": + if tone == "Copilot": url = "https://copilot.microsoft.com/turing/conversation/create?bundleVersion=1.1634.3-nodesign2" else: url = "https://www.bing.com/turing/conversation/create?bundleVersion=1.1626.1" diff --git a/g4f/Provider/needs_auth/OpenaiChat.py b/g4f/Provider/needs_auth/OpenaiChat.py index 0aff99a7..72f9f224 100644 --- a/g4f/Provider/needs_auth/OpenaiChat.py +++ b/g4f/Provider/needs_auth/OpenaiChat.py @@ -3,10 +3,10 @@ from __future__ import annotations import asyncio import uuid import json -import os import base64 import time from aiohttp import ClientWebSocketResponse +from copy import copy try: import webview @@ -22,13 +22,13 @@ except ImportError: pass from ..base_provider import AsyncGeneratorProvider, ProviderModelMixin -from ..helper import get_cookies from ...webdriver import get_browser from ...typing import AsyncResult, Messages, Cookies, ImageType, Union, 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 -from ...errors import MissingRequirementsError, MissingAuthError, ProviderNotWorkingError +from ...errors import MissingAuthError +from ...providers.conversation import BaseConversation from ..openai.har_file import getArkoseAndAccessToken from ... import debug @@ -56,11 +56,6 @@ class OpenaiChat(AsyncGeneratorProvider, ProviderModelMixin): prompt: str = None, model: str = "", messages: Messages = [], - history_disabled: bool = False, - action: str = "next", - conversation_id: str = None, - parent_id: str = None, - image: ImageType = None, **kwargs ) -> Response: """ @@ -89,12 +84,7 @@ class OpenaiChat(AsyncGeneratorProvider, ProviderModelMixin): generator = cls.create_async_generator( model, messages, - history_disabled=history_disabled, - action=action, - conversation_id=conversation_id, - parent_id=parent_id, - image=image, - response_fields=True, + return_conversation=True, **kwargs ) return Response( @@ -209,7 +199,7 @@ class OpenaiChat(AsyncGeneratorProvider, ProviderModelMixin): } for message in messages] # Check if there is an image response - if image_request: + if image_request is not None: # Change content in last user message messages[-1]["content"] = { "content_type": "multimodal_text", @@ -308,10 +298,11 @@ class OpenaiChat(AsyncGeneratorProvider, ProviderModelMixin): history_disabled: bool = True, action: str = "next", conversation_id: str = None, + conversation: Conversation = None, parent_id: str = None, image: ImageType = None, image_name: str = None, - response_fields: bool = False, + return_conversation: bool = False, **kwargs ) -> AsyncResult: """ @@ -330,7 +321,7 @@ class OpenaiChat(AsyncGeneratorProvider, ProviderModelMixin): conversation_id (str): ID of the conversation. parent_id (str): ID of the parent message. image (ImageType): Image to include in the conversation. - response_fields (bool): Flag to include response fields in the output. + return_conversation (bool): Flag to include response fields in the output. **kwargs: Additional keyword arguments. Yields: @@ -387,6 +378,8 @@ class OpenaiChat(AsyncGeneratorProvider, ProviderModelMixin): arkose_token, api_key, cookies = await getArkoseAndAccessToken(proxy) cls._create_request_args(cookies) cls._set_api_key(api_key) + if arkose_token is None: + raise MissingAuthError("No arkose token found in .har file") try: image_request = await cls.upload_image(session, cls._headers, image, image_name) if image else None @@ -396,7 +389,8 @@ class OpenaiChat(AsyncGeneratorProvider, ProviderModelMixin): print(f"{e.__class__.__name__}: {e}") model = cls.get_model(model).replace("gpt-3.5-turbo", "text-davinci-002-render-sha") - fields = ResponseFields() + fields = Conversation() if conversation is None else copy(conversation) + fields.finish_reason = None while fields.finish_reason is None: conversation_id = conversation_id if fields.conversation_id is None else fields.conversation_id parent_id = parent_id if fields.message_id is None else fields.message_id @@ -409,7 +403,7 @@ class OpenaiChat(AsyncGeneratorProvider, ProviderModelMixin): "conversation_id": conversation_id, "parent_message_id": parent_id, "model": model, - "history_and_training_disabled": history_disabled and not auto_continue, + "history_and_training_disabled": history_disabled and not auto_continue and not return_conversation, "websocket_request_id": websocket_request_id } if action != "continue": @@ -422,8 +416,6 @@ class OpenaiChat(AsyncGeneratorProvider, ProviderModelMixin): } if need_arkose: headers["OpenAI-Sentinel-Arkose-Token"] = arkose_token - headers["OpenAI-Sentinel-Chat-Requirements-Token"] = chat_token - async with session.post( f"{cls.url}/backend-api/conversation", json=data, @@ -432,15 +424,15 @@ class OpenaiChat(AsyncGeneratorProvider, ProviderModelMixin): cls._update_request_args(session) await raise_for_status(response) async for chunk in cls.iter_messages_chunk(response.iter_lines(), session, fields): - if response_fields: - response_fields = False + if return_conversation: + return_conversation = False yield fields yield chunk if not auto_continue: break action = "continue" await asyncio.sleep(5) - if history_disabled and auto_continue: + if history_disabled and auto_continue and not return_conversation: await cls.delete_conversation(session, cls._headers, fields.conversation_id) @staticmethod @@ -458,7 +450,7 @@ class OpenaiChat(AsyncGeneratorProvider, ProviderModelMixin): cls, messages: AsyncIterator, session: StreamSession, - fields: ResponseFields + fields: Conversation ) -> AsyncIterator: last_message: int = 0 async for message in messages: @@ -487,7 +479,7 @@ class OpenaiChat(AsyncGeneratorProvider, ProviderModelMixin): break @classmethod - async def iter_messages_line(cls, session: StreamSession, line: bytes, fields: ResponseFields) -> AsyncIterator: + async def iter_messages_line(cls, session: StreamSession, line: bytes, fields: Conversation) -> AsyncIterator: if not line.startswith(b"data: "): return elif line.startswith(b"data: [DONE]"): @@ -618,7 +610,7 @@ this.fetch = async (url, options) => { @classmethod def _update_request_args(cls, session: StreamSession): for c in session.cookie_jar if hasattr(session, "cookie_jar") else session.cookies.jar: - cls._cookies[c.name if hasattr(c, "name") else c.key] = c.value + cls._cookies[c.key if hasattr(c, "key") else c.name] = c.value cls._update_cookie_header() @classmethod @@ -631,7 +623,7 @@ this.fetch = async (url, options) => { def _update_cookie_header(cls): cls._headers["Cookie"] = cls._format_cookies(cls._cookies) -class ResponseFields: +class Conversation(BaseConversation): """ Class to encapsulate response fields. """ @@ -664,7 +656,7 @@ class Response(): self._generator = None chunks = [] async for chunk in self._generator: - if isinstance(chunk, ResponseFields): + if isinstance(chunk, Conversation): self._fields = chunk else: yield chunk diff --git a/g4f/Provider/openai/har_file.py b/g4f/Provider/openai/har_file.py index 3e8535ad..68fe7420 100644 --- a/g4f/Provider/openai/har_file.py +++ b/g4f/Provider/openai/har_file.py @@ -11,11 +11,6 @@ from copy import deepcopy from .crypt import decrypt, encrypt from ...requests import StreamSession -arkPreURL = "https://tcr9i.chat.openai.com/fc/gt2/public_key/35536E1E-65B4-4D96-9D97-6ADB7EFF8147" -sessionUrl = "https://chat.openai.com/api/auth/session" -chatArk = None -accessToken = None - class arkReq: def __init__(self, arkURL, arkBx, arkHeader, arkBody, arkCookies, userAgent): self.arkURL = arkURL @@ -25,21 +20,30 @@ class arkReq: self.arkCookies = arkCookies self.userAgent = userAgent +arkPreURL = "https://tcr9i.chat.openai.com/fc/gt2/public_key/35536E1E-65B4-4D96-9D97-6ADB7EFF8147" +sessionUrl = "https://chat.openai.com/api/auth/session" +chatArk: arkReq = None +accessToken: str = None +cookies: dict = None + def readHAR(): dirPath = "./" harPath = [] chatArks = [] accessToken = None + cookies = {} for root, dirs, files in os.walk(dirPath): for file in files: if file.endswith(".har"): harPath.append(os.path.join(root, file)) + if harPath: + break if not harPath: raise RuntimeError("No .har file found") for path in harPath: - with open(path, 'r') as file: + with open(path, 'rb') as file: try: - harFile = json.load(file) + harFile = json.loads(file.read()) except json.JSONDecodeError: # Error: not a HAR file! continue @@ -48,11 +52,12 @@ def readHAR(): chatArks.append(parseHAREntry(v)) elif v['request']['url'] == sessionUrl: accessToken = json.loads(v["response"]["content"]["text"]).get("accessToken") - if not chatArks: - RuntimeError("No arkose requests found in .har files") + cookies = {c['name']: c['value'] for c in v['request']['cookies']} if not accessToken: RuntimeError("No accessToken found in .har files") - return chatArks.pop(), accessToken + if not chatArks: + return None, accessToken, cookies + return chatArks.pop(), accessToken, cookies def parseHAREntry(entry) -> arkReq: tmpArk = arkReq( @@ -60,7 +65,7 @@ def parseHAREntry(entry) -> arkReq: arkBx="", arkHeader={h['name'].lower(): h['value'] for h in entry['request']['headers'] if h['name'].lower() not in ['content-length', 'cookie'] and not h['name'].startswith(':')}, arkBody={p['name']: unquote(p['value']) for p in entry['request']['postData']['params'] if p['name'] not in ['rnd']}, - arkCookies=[{'name': c['name'], 'value': c['value'], 'expires': c['expires']} for c in entry['request']['cookies']], + arkCookies={c['name']: c['value'] for c in entry['request']['cookies']}, userAgent="" ) tmpArk.userAgent = tmpArk.arkHeader.get('user-agent', '') @@ -81,7 +86,6 @@ def genArkReq(chatArk: arkReq) -> arkReq: tmpArk.arkBody['bda'] = base64.b64encode(bda.encode()).decode() tmpArk.arkBody['rnd'] = str(random.random()) tmpArk.arkHeader['x-ark-esync-value'] = bw - tmpArk.arkCookies = {cookie['name']: cookie['value'] for cookie in tmpArk.arkCookies} return tmpArk async def sendRequest(tmpArk: arkReq, proxy: str = None): @@ -117,8 +121,10 @@ def getN() -> str: return base64.b64encode(timestamp.encode()).decode() async def getArkoseAndAccessToken(proxy: str): - global chatArk, accessToken + global chatArk, accessToken, cookies if chatArk is None or accessToken is None: - chatArk, accessToken = readHAR() + chatArk, accessToken, cookies = readHAR() + if chatArk is None: + return None, accessToken, cookies newReq = genArkReq(chatArk) - return await sendRequest(newReq, proxy), accessToken, newReq.arkCookies
\ No newline at end of file + return await sendRequest(newReq, proxy), accessToken, cookies
\ No newline at end of file |