summaryrefslogtreecommitdiffstats
path: root/g4f/Provider
diff options
context:
space:
mode:
authorHeiner Lohaus <hlohaus@users.noreply.github.com>2024-01-13 15:37:36 +0100
committerHeiner Lohaus <hlohaus@users.noreply.github.com>2024-01-13 15:37:36 +0100
commitea8d6b847a0e656cc5583948c5745592adda7103 (patch)
treecb796ff258d339a5ee4a4ec3fb4b2617e6639677 /g4f/Provider
parentChange pypi to live (diff)
downloadgpt4free-ea8d6b847a0e656cc5583948c5745592adda7103.tar
gpt4free-ea8d6b847a0e656cc5583948c5745592adda7103.tar.gz
gpt4free-ea8d6b847a0e656cc5583948c5745592adda7103.tar.bz2
gpt4free-ea8d6b847a0e656cc5583948c5745592adda7103.tar.lz
gpt4free-ea8d6b847a0e656cc5583948c5745592adda7103.tar.xz
gpt4free-ea8d6b847a0e656cc5583948c5745592adda7103.tar.zst
gpt4free-ea8d6b847a0e656cc5583948c5745592adda7103.zip
Diffstat (limited to '')
-rw-r--r--g4f/Provider/Bing.py18
-rw-r--r--g4f/Provider/base_provider.py7
-rw-r--r--g4f/Provider/bing/conversation.py7
-rw-r--r--g4f/Provider/bing/create_images.py18
-rw-r--r--g4f/Provider/bing/upload_image.py166
-rw-r--r--g4f/Provider/create_images.py10
-rw-r--r--g4f/Provider/needs_auth/OpenaiChat.py344
7 files changed, 316 insertions, 254 deletions
diff --git a/g4f/Provider/Bing.py b/g4f/Provider/Bing.py
index b0949397..da9b0172 100644
--- a/g4f/Provider/Bing.py
+++ b/g4f/Provider/Bing.py
@@ -8,11 +8,10 @@ import time
from urllib import parse
from aiohttp import ClientSession, ClientTimeout
-from ..typing import AsyncResult, Messages
+from ..typing import AsyncResult, Messages, ImageType
from .base_provider import AsyncGeneratorProvider
-from ..webdriver import get_browser, get_driver_cookies
from .bing.upload_image import upload_image
-from .bing.create_images import create_images, format_images_markdown, wait_for_login
+from .bing.create_images import create_images, format_images_markdown
from .bing.conversation import Conversation, create_conversation, delete_conversation
class Tones():
@@ -34,7 +33,7 @@ class Bing(AsyncGeneratorProvider):
timeout: int = 900,
cookies: dict = None,
tone: str = Tones.balanced,
- image: str = None,
+ image: ImageType = None,
web_search: bool = False,
**kwargs
) -> AsyncResult:
@@ -247,7 +246,7 @@ def create_message(
async def stream_generate(
prompt: str,
tone: str,
- image: str = None,
+ image: ImageType = None,
context: str = None,
proxy: str = None,
cookies: dict = None,
@@ -315,14 +314,7 @@ async def stream_generate(
result = response['item']['result']
if result.get('error'):
if result["value"] == "CaptchaChallenge":
- driver = get_browser(proxy=proxy)
- try:
- wait_for_login(driver)
- cookies = get_driver_cookies(driver)
- finally:
- driver.quit()
- async for chunk in stream_generate(prompt, tone, image, context, proxy, cookies, web_search, gpt4_turbo, timeout):
- yield chunk
+ raise Exception(f"{result['value']}: Use other cookies or/and ip address")
else:
raise Exception(f"{result['value']}: {result['message']}")
return
diff --git a/g4f/Provider/base_provider.py b/g4f/Provider/base_provider.py
index 6da7f6c6..e7e88841 100644
--- a/g4f/Provider/base_provider.py
+++ b/g4f/Provider/base_provider.py
@@ -7,7 +7,7 @@ from concurrent.futures import ThreadPoolExecutor
from abc import abstractmethod
from inspect import signature, Parameter
from .helper import get_event_loop, get_cookies, format_prompt
-from ..typing import CreateResult, AsyncResult, Messages, Union
+from ..typing import CreateResult, AsyncResult, Messages
from ..base_provider import BaseProvider
if sys.version_info < (3, 10):
@@ -77,8 +77,7 @@ class AbstractProvider(BaseProvider):
continue
if args:
args += ", "
- args += "\n"
- args += " " + name
+ args += "\n " + name
if name != "model" and param.annotation is not Parameter.empty:
args += f": {get_type_name(param.annotation)}"
if param.default == "":
@@ -156,7 +155,7 @@ class AsyncGeneratorProvider(AsyncProvider):
messages,
stream=False,
**kwargs
- )
+ ) if not isinstance(chunk, Exception)
])
@staticmethod
diff --git a/g4f/Provider/bing/conversation.py b/g4f/Provider/bing/conversation.py
index ef45cd82..9e011c26 100644
--- a/g4f/Provider/bing/conversation.py
+++ b/g4f/Provider/bing/conversation.py
@@ -10,7 +10,10 @@ class Conversation():
async def create_conversation(session: ClientSession, proxy: str = None) -> Conversation:
url = 'https://www.bing.com/turing/conversation/create?bundleVersion=1.1199.4'
async with session.get(url, proxy=proxy) as response:
- data = await response.json()
+ try:
+ data = await response.json()
+ except:
+ raise RuntimeError(f"Response: {await response.text()}")
conversationId = data.get('conversationId')
clientId = data.get('clientId')
@@ -26,7 +29,7 @@ async def list_conversations(session: ClientSession) -> list:
response = await response.json()
return response["chats"]
-async def delete_conversation(session: ClientSession, conversation: Conversation, proxy: str = None) -> list:
+async def delete_conversation(session: ClientSession, conversation: Conversation, proxy: str = None) -> bool:
url = "https://sydney.bing.com/sydney/DeleteSingleConversation"
json = {
"conversationId": conversation.conversationId,
diff --git a/g4f/Provider/bing/create_images.py b/g4f/Provider/bing/create_images.py
index b203a0dc..a1ecace3 100644
--- a/g4f/Provider/bing/create_images.py
+++ b/g4f/Provider/bing/create_images.py
@@ -9,6 +9,7 @@ from ..create_images import CreateImagesProvider
from ..helper import get_cookies, get_event_loop
from ...webdriver import WebDriver, get_driver_cookies, get_browser
from ...base_provider import ProviderType
+from ...image import format_images_markdown
BING_URL = "https://www.bing.com"
@@ -23,6 +24,7 @@ def wait_for_login(driver: WebDriver, timeout: int = 1200) -> None:
raise RuntimeError("Timeout error")
value = driver.get_cookie("_U")
if value:
+ time.sleep(1)
return
time.sleep(0.5)
@@ -62,7 +64,8 @@ async def create_images(session: ClientSession, prompt: str, proxy: str = None,
errors = [
"this prompt is being reviewed",
"this prompt has been blocked",
- "we're working hard to offer image creator in more languages"
+ "we're working hard to offer image creator in more languages",
+ "we can't create your images right now"
]
text = (await response.text()).lower()
for error in errors:
@@ -72,7 +75,7 @@ async def create_images(session: ClientSession, prompt: str, proxy: str = None,
url = f"{BING_URL}/images/create?q={url_encoded_prompt}&rt=3&FORM=GENCRE"
async with session.post(url, allow_redirects=False, proxy=proxy, timeout=timeout) as response:
if response.status != 302:
- raise RuntimeError(f"Create images failed. Status Code: {response.status}")
+ raise RuntimeError(f"Create images failed. Code: {response.status}")
redirect_url = response.headers["Location"].replace("&nfy=1", "")
redirect_url = f"{BING_URL}{redirect_url}"
@@ -84,10 +87,10 @@ async def create_images(session: ClientSession, prompt: str, proxy: str = None,
start_time = time.time()
while True:
if time.time() - start_time > timeout:
- raise RuntimeError(f"Timeout error after {timeout} seconds")
+ raise RuntimeError(f"Timeout error after {timeout} sec")
async with session.get(polling_url) as response:
if response.status != 200:
- raise RuntimeError(f"Polling images faild. Status Code: {response.status}")
+ raise RuntimeError(f"Polling images faild. Code: {response.status}")
text = await response.text()
if not text:
await asyncio.sleep(1)
@@ -119,13 +122,6 @@ def read_images(text: str) -> list:
raise RuntimeError("No images found")
return images
-def format_images_markdown(images: list, prompt: str) -> str:
- images = [f"[![#{idx+1} {prompt}]({image}?w=200&h=200)]({image})" for idx, image in enumerate(images)]
- images = "\n".join(images)
- start_flag = "<!-- generated images start -->\n"
- end_flag = "<!-- generated images end -->\n"
- return f"\n{start_flag}{images}\n{end_flag}\n"
-
async def create_images_markdown(cookies: dict, prompt: str, proxy: str = None) -> str:
session = create_session(cookies)
try:
diff --git a/g4f/Provider/bing/upload_image.py b/g4f/Provider/bing/upload_image.py
index 329e6df4..a7413207 100644
--- a/g4f/Provider/bing/upload_image.py
+++ b/g4f/Provider/bing/upload_image.py
@@ -3,70 +3,59 @@ from __future__ import annotations
import string
import random
import json
-import re
-import io
-import base64
import numpy as np
-from PIL import Image
+from ...typing import ImageType
from aiohttp import ClientSession
+from ...image import to_image, process_image, to_base64
+
+image_config = {
+ "maxImagePixels": 360000,
+ "imageCompressionRate": 0.7,
+ "enableFaceBlurDebug": 0,
+}
async def upload_image(
session: ClientSession,
- image: str,
+ image: ImageType,
tone: str,
proxy: str = None
-):
- try:
- image_config = {
- "maxImagePixels": 360000,
- "imageCompressionRate": 0.7,
- "enableFaceBlurDebug": 0,
- }
- is_data_uri_an_image(image)
- img_binary_data = extract_data_uri(image)
- is_accepted_format(img_binary_data)
- img = Image.open(io.BytesIO(img_binary_data))
- width, height = img.size
- max_image_pixels = image_config['maxImagePixels']
- if max_image_pixels / (width * height) < 1:
- new_width = int(width * np.sqrt(max_image_pixels / (width * height)))
- new_height = int(height * np.sqrt(max_image_pixels / (width * height)))
- else:
- new_width = width
- new_height = height
- try:
- orientation = get_orientation(img)
- except Exception:
- orientation = None
- new_img = process_image(orientation, img, new_width, new_height)
- new_img_binary_data = compress_image_to_base64(new_img, image_config['imageCompressionRate'])
- data, boundary = build_image_upload_api_payload(new_img_binary_data, tone)
- headers = session.headers.copy()
- headers["content-type"] = f'multipart/form-data; boundary={boundary}'
- headers["referer"] = 'https://www.bing.com/search?q=Bing+AI&showconv=1&FORM=hpcodx'
- headers["origin"] = 'https://www.bing.com'
- async with session.post("https://www.bing.com/images/kblob", data=data, headers=headers, proxy=proxy) as response:
- if response.status != 200:
- raise RuntimeError("Failed to upload image.")
- image_info = await response.json()
- if not image_info.get('blobId'):
- raise RuntimeError("Failed to parse image info.")
- result = {'bcid': image_info.get('blobId', "")}
- result['blurredBcid'] = image_info.get('processedBlobId', "")
- if result['blurredBcid'] != "":
- result["imageUrl"] = "https://www.bing.com/images/blob?bcid=" + result['blurredBcid']
- elif result['bcid'] != "":
- result["imageUrl"] = "https://www.bing.com/images/blob?bcid=" + result['bcid']
- result['originalImageUrl'] = (
- "https://www.bing.com/images/blob?bcid="
- + result['blurredBcid']
- if image_config["enableFaceBlurDebug"]
- else "https://www.bing.com/images/blob?bcid="
- + result['bcid']
- )
- return result
- except Exception as e:
- raise RuntimeError(f"Upload image failed: {e}")
+) -> dict:
+ image = to_image(image)
+ width, height = image.size
+ max_image_pixels = image_config['maxImagePixels']
+ if max_image_pixels / (width * height) < 1:
+ new_width = int(width * np.sqrt(max_image_pixels / (width * height)))
+ new_height = int(height * np.sqrt(max_image_pixels / (width * height)))
+ else:
+ new_width = width
+ new_height = height
+ new_img = process_image(image, new_width, new_height)
+ new_img_binary_data = to_base64(new_img, image_config['imageCompressionRate'])
+ data, boundary = build_image_upload_api_payload(new_img_binary_data, tone)
+ headers = session.headers.copy()
+ headers["content-type"] = f'multipart/form-data; boundary={boundary}'
+ headers["referer"] = 'https://www.bing.com/search?q=Bing+AI&showconv=1&FORM=hpcodx'
+ headers["origin"] = 'https://www.bing.com'
+ async with session.post("https://www.bing.com/images/kblob", data=data, headers=headers, proxy=proxy) as response:
+ if response.status != 200:
+ raise RuntimeError("Failed to upload image.")
+ image_info = await response.json()
+ if not image_info.get('blobId'):
+ raise RuntimeError("Failed to parse image info.")
+ result = {'bcid': image_info.get('blobId', "")}
+ result['blurredBcid'] = image_info.get('processedBlobId', "")
+ if result['blurredBcid'] != "":
+ result["imageUrl"] = "https://www.bing.com/images/blob?bcid=" + result['blurredBcid']
+ elif result['bcid'] != "":
+ result["imageUrl"] = "https://www.bing.com/images/blob?bcid=" + result['bcid']
+ result['originalImageUrl'] = (
+ "https://www.bing.com/images/blob?bcid="
+ + result['blurredBcid']
+ if image_config["enableFaceBlurDebug"]
+ else "https://www.bing.com/images/blob?bcid="
+ + result['bcid']
+ )
+ return result
def build_image_upload_api_payload(image_bin: str, tone: str):
@@ -98,65 +87,4 @@ def build_image_upload_api_payload(image_bin: str, tone: str):
+ boundary
+ "--\r\n"
)
- return data, boundary
-
-def is_data_uri_an_image(data_uri: str):
- # Check if the data URI starts with 'data:image' and contains an image format (e.g., jpeg, png, gif)
- if not re.match(r'data:image/(\w+);base64,', data_uri):
- raise ValueError("Invalid data URI image.")
- # Extract the image format from the data URI
- image_format = re.match(r'data:image/(\w+);base64,', data_uri).group(1)
- # Check if the image format is one of the allowed formats (jpg, jpeg, png, gif)
- if image_format.lower() not in ['jpeg', 'jpg', 'png', 'gif']:
- raise ValueError("Invalid image format (from mime file type).")
-
-def is_accepted_format(binary_data: bytes) -> bool:
- if binary_data.startswith(b'\xFF\xD8\xFF'):
- pass # It's a JPEG image
- elif binary_data.startswith(b'\x89PNG\r\n\x1a\n'):
- pass # It's a PNG image
- elif binary_data.startswith(b'GIF87a') or binary_data.startswith(b'GIF89a'):
- pass # It's a GIF image
- elif binary_data.startswith(b'\x89JFIF') or binary_data.startswith(b'JFIF\x00'):
- pass # It's a JPEG image
- elif binary_data.startswith(b'\xFF\xD8'):
- pass # It's a JPEG image
- elif binary_data.startswith(b'RIFF') and binary_data[8:12] == b'WEBP':
- pass # It's a WebP image
- else:
- raise ValueError("Invalid image format (from magic code).")
-
-def extract_data_uri(data_uri: str) -> bytes:
- data = data_uri.split(",")[1]
- data = base64.b64decode(data)
- return data
-
-def get_orientation(data: bytes) -> int:
- if data[:2] != b'\xFF\xD8':
- raise Exception('NotJpeg')
- with Image.open(data) as img:
- exif_data = img._getexif()
- if exif_data is not None:
- orientation = exif_data.get(274) # 274 corresponds to the orientation tag in EXIF
- if orientation is not None:
- return orientation
-
-def process_image(orientation: int, img: Image.Image, new_width: int, new_height: int) -> Image.Image:
- # Initialize the canvas
- new_img = Image.new("RGB", (new_width, new_height), color="#FFFFFF")
- if orientation:
- if orientation > 4:
- img = img.transpose(Image.FLIP_LEFT_RIGHT)
- if orientation in [3, 4]:
- img = img.transpose(Image.ROTATE_180)
- if orientation in [5, 6]:
- img = img.transpose(Image.ROTATE_270)
- if orientation in [7, 8]:
- img = img.transpose(Image.ROTATE_90)
- new_img.paste(img, (0, 0))
- return new_img
-
-def compress_image_to_base64(image: Image.Image, compression_rate: float) -> str:
- output_buffer = io.BytesIO()
- image.save(output_buffer, format="JPEG", quality=int(compression_rate * 100))
- return base64.b64encode(output_buffer.getvalue()).decode('utf-8') \ No newline at end of file
+ return data, boundary \ No newline at end of file
diff --git a/g4f/Provider/create_images.py b/g4f/Provider/create_images.py
index 29f88a80..f8a0442d 100644
--- a/g4f/Provider/create_images.py
+++ b/g4f/Provider/create_images.py
@@ -2,6 +2,7 @@ from __future__ import annotations
import re
import asyncio
+from .. import debug
from ..typing import CreateResult, Messages
from ..base_provider import BaseProvider, ProviderType
@@ -26,12 +27,11 @@ class CreateImagesProvider(BaseProvider):
self.create_images = create_images
self.create_images_async = create_async
self.system_message = system_message
+ self.include_placeholder = include_placeholder
self.__name__ = provider.__name__
+ self.url = provider.url
self.working = provider.working
self.supports_stream = provider.supports_stream
- self.include_placeholder = include_placeholder
- if hasattr(provider, "url"):
- self.url = provider.url
def create_completion(
self,
@@ -54,6 +54,8 @@ class CreateImagesProvider(BaseProvider):
yield start
if self.include_placeholder:
yield placeholder
+ if debug.logging:
+ print(f"Create images with prompt: {prompt}")
yield from self.create_images(prompt)
if append:
yield append
@@ -76,6 +78,8 @@ class CreateImagesProvider(BaseProvider):
placeholders = []
for placeholder, prompt in matches:
if placeholder not in placeholders:
+ if debug.logging:
+ print(f"Create images with prompt: {prompt}")
results.append(self.create_images_async(prompt))
placeholders.append(placeholder)
results = await asyncio.gather(*results)
diff --git a/g4f/Provider/needs_auth/OpenaiChat.py b/g4f/Provider/needs_auth/OpenaiChat.py
index 4651955c..4b11aeaf 100644
--- a/g4f/Provider/needs_auth/OpenaiChat.py
+++ b/g4f/Provider/needs_auth/OpenaiChat.py
@@ -2,17 +2,18 @@ from __future__ import annotations
import uuid, json, asyncio, os
from py_arkose_generator.arkose import get_values_for_request
-from asyncstdlib.itertools import tee
from async_property import async_cached_property
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from ..base_provider import AsyncGeneratorProvider
-from ..helper import get_event_loop, format_prompt, get_cookies
-from ...webdriver import get_browser
+from ..helper import format_prompt, get_cookies
+from ...webdriver import get_browser, get_driver_cookies
from ...typing import AsyncResult, Messages
from ...requests import StreamSession
+from ...image import to_image, to_bytes, ImageType, ImageResponse
+from ... import debug
models = {
"gpt-3.5": "text-davinci-002-render-sha",
@@ -28,6 +29,7 @@ class OpenaiChat(AsyncGeneratorProvider):
supports_gpt_35_turbo = True
supports_gpt_4 = True
_cookies: dict = {}
+ _default_model: str = None
@classmethod
async def create(
@@ -39,6 +41,7 @@ class OpenaiChat(AsyncGeneratorProvider):
action: str = "next",
conversation_id: str = None,
parent_id: str = None,
+ image: ImageType = None,
**kwargs
) -> Response:
if prompt:
@@ -53,16 +56,120 @@ class OpenaiChat(AsyncGeneratorProvider):
action=action,
conversation_id=conversation_id,
parent_id=parent_id,
+ image=image,
response_fields=True,
**kwargs
)
return Response(
generator,
- await anext(generator),
action,
messages,
kwargs
)
+
+ @classmethod
+ async def upload_image(
+ cls,
+ session: StreamSession,
+ headers: dict,
+ image: ImageType
+ ) -> ImageResponse:
+ image = to_image(image)
+ extension = image.format.lower()
+ data_bytes = to_bytes(image)
+ data = {
+ "file_name": f"{image.width}x{image.height}.{extension}",
+ "file_size": len(data_bytes),
+ "use_case": "multimodal"
+ }
+ async with session.post(f"{cls.url}/backend-api/files", json=data, headers=headers) as response:
+ response.raise_for_status()
+ image_data = {
+ **data,
+ **await response.json(),
+ "mime_type": f"image/{extension}",
+ "extension": extension,
+ "height": image.height,
+ "width": image.width
+ }
+ async with session.put(
+ image_data["upload_url"],
+ data=data_bytes,
+ headers={
+ "Content-Type": image_data["mime_type"],
+ "x-ms-blob-type": "BlockBlob"
+ }
+ ) as response:
+ response.raise_for_status()
+ async with session.post(
+ f"{cls.url}/backend-api/files/{image_data['file_id']}/uploaded",
+ json={},
+ headers=headers
+ ) as response:
+ response.raise_for_status()
+ download_url = (await response.json())["download_url"]
+ return ImageResponse(download_url, image_data["file_name"], image_data)
+
+ @classmethod
+ async def get_default_model(cls, session: StreamSession, headers: dict):
+ if cls._default_model:
+ model = cls._default_model
+ else:
+ async with session.get(f"{cls.url}/backend-api/models", headers=headers) as response:
+ data = await response.json()
+ if "categories" in data:
+ model = data["categories"][-1]["default_model"]
+ else:
+ RuntimeError(f"Response: {data}")
+ cls._default_model = model
+ return model
+
+ @classmethod
+ def create_messages(cls, prompt: str, image_response: ImageResponse = None):
+ if not image_response:
+ content = {"content_type": "text", "parts": [prompt]}
+ else:
+ content = {
+ "content_type": "multimodal_text",
+ "parts": [{
+ "asset_pointer": f"file-service://{image_response.get('file_id')}",
+ "height": image_response.get("height"),
+ "size_bytes": image_response.get("file_size"),
+ "width": image_response.get("width"),
+ }, prompt]
+ }
+ messages = [{
+ "id": str(uuid.uuid4()),
+ "author": {"role": "user"},
+ "content": content,
+ }]
+ if image_response:
+ messages[0]["metadata"] = {
+ "attachments": [{
+ "height": image_response.get("height"),
+ "id": image_response.get("file_id"),
+ "mimeType": image_response.get("mime_type"),
+ "name": image_response.get("file_name"),
+ "size": image_response.get("file_size"),
+ "width": image_response.get("width"),
+ }]
+ }
+ return messages
+
+ @classmethod
+ async def get_image_response(cls, session: StreamSession, headers: dict, line: dict):
+ if "parts" in line["message"]["content"]:
+ part = line["message"]["content"]["parts"][0]
+ if "asset_pointer" in part and part["metadata"]:
+ file_id = part["asset_pointer"].split("file-service://", 1)[1]
+ prompt = part["metadata"]["dalle"]["prompt"]
+ async with session.get(
+ f"{cls.url}/backend-api/files/{file_id}/download",
+ headers=headers
+ ) as response:
+ response.raise_for_status()
+ download_url = (await response.json())["download_url"]
+ return ImageResponse(download_url, prompt)
@classmethod
async def create_async_generator(
@@ -78,13 +185,12 @@ class OpenaiChat(AsyncGeneratorProvider):
action: str = "next",
conversation_id: str = None,
parent_id: str = None,
+ image: ImageType = None,
response_fields: bool = False,
**kwargs
) -> AsyncResult:
- if not model:
- model = "gpt-3.5"
- elif model not in models:
- raise ValueError(f"Model are not supported: {model}")
+ if model in models:
+ model = models[model]
if not parent_id:
parent_id = str(uuid.uuid4())
if not cookies:
@@ -98,115 +204,131 @@ class OpenaiChat(AsyncGeneratorProvider):
login_url = os.environ.get("G4F_LOGIN_URL")
if login_url:
yield f"Please login: [ChatGPT]({login_url})\n\n"
- cls._cookies["access_token"] = access_token = await cls.browse_access_token(proxy)
+ access_token, cookies = cls.browse_access_token(proxy)
+ cls._cookies = cookies
headers = {
- "Accept": "text/event-stream",
"Authorization": f"Bearer {access_token}",
}
async with StreamSession(
proxies={"https": proxy},
impersonate="chrome110",
- headers=headers,
timeout=timeout,
cookies=dict([(name, value) for name, value in cookies.items() if name == "_puid"])
) as session:
+ if not model:
+ model = await cls.get_default_model(session, headers)
+ try:
+ image_response = None
+ if image:
+ image_response = await cls.upload_image(session, headers, image)
+ yield image_response
+ except Exception as e:
+ yield e
end_turn = EndTurn()
while not end_turn.is_end:
data = {
"action": action,
- "arkose_token": await get_arkose_token(proxy, timeout),
+ "arkose_token": await cls.get_arkose_token(session),
"conversation_id": conversation_id,
"parent_message_id": parent_id,
- "model": models[model],
+ "model": model,
"history_and_training_disabled": history_disabled and not auto_continue,
}
if action != "continue":
prompt = format_prompt(messages) if not conversation_id else messages[-1]["content"]
- data["messages"] = [{
- "id": str(uuid.uuid4()),
- "author": {"role": "user"},
- "content": {"content_type": "text", "parts": [prompt]},
- }]
- async with session.post(f"{cls.url}/backend-api/conversation", json=data) as response:
+ data["messages"] = cls.create_messages(prompt, image_response)
+ async with session.post(
+ f"{cls.url}/backend-api/conversation",
+ json=data,
+ headers={"Accept": "text/event-stream", **headers}
+ ) as response:
try:
response.raise_for_status()
except:
- raise RuntimeError(f"Error {response.status_code}: {await response.text()}")
- last_message = 0
- async for line in response.iter_lines():
- if not line.startswith(b"data: "):
- continue
- line = line[6:]
- if line == b"[DONE]":
- break
- try:
- line = json.loads(line)
- except:
- continue
- if "message" not in line:
- continue
- if "error" in line and line["error"]:
- raise RuntimeError(line["error"])
- if "message_type" not in line["message"]["metadata"]:
- continue
- if line["message"]["author"]["role"] != "assistant":
- continue
- if line["message"]["metadata"]["message_type"] in ("next", "continue", "variant"):
- conversation_id = line["conversation_id"]
- parent_id = line["message"]["id"]
- if response_fields:
- response_fields = False
- yield ResponseFields(conversation_id, parent_id, end_turn)
- new_message = line["message"]["content"]["parts"][0]
- yield new_message[last_message:]
- last_message = len(new_message)
- if "finish_details" in line["message"]["metadata"]:
- if line["message"]["metadata"]["finish_details"]["type"] == "stop":
- end_turn.end()
+ raise RuntimeError(f"Response {response.status_code}: {await response.text()}")
+ try:
+ last_message: int = 0
+ async for line in response.iter_lines():
+ if not line.startswith(b"data: "):
+ continue
+ elif line.startswith(b"data: [DONE]"):
+ break
+ try:
+ line = json.loads(line[6:])
+ except:
+ continue
+ if "message" not in line:
+ continue
+ if "error" in line and line["error"]:
+ raise RuntimeError(line["error"])
+ if "message_type" not in line["message"]["metadata"]:
+ continue
+ try:
+ image_response = await cls.get_image_response(session, headers, line)
+ if image_response:
+ yield image_response
+ except Exception as e:
+ yield e
+ if line["message"]["author"]["role"] != "assistant":
+ continue
+ if line["message"]["metadata"]["message_type"] in ("next", "continue", "variant"):
+ conversation_id = line["conversation_id"]
+ parent_id = line["message"]["id"]
+ if response_fields:
+ response_fields = False
+ yield ResponseFields(conversation_id, parent_id, end_turn)
+ if "parts" in line["message"]["content"]:
+ new_message = line["message"]["content"]["parts"][0]
+ if len(new_message) > last_message:
+ yield new_message[last_message:]
+ last_message = len(new_message)
+ if "finish_details" in line["message"]["metadata"]:
+ if line["message"]["metadata"]["finish_details"]["type"] == "stop":
+ end_turn.end()
+ break
+ except Exception as e:
+ yield e
if not auto_continue:
break
action = "continue"
await asyncio.sleep(5)
+ if history_disabled:
+ async with session.patch(
+ f"{cls.url}/backend-api/conversation/{conversation_id}",
+ json={"is_visible": False},
+ headers=headers
+ ) as response:
+ response.raise_for_status()
@classmethod
- async def browse_access_token(cls, proxy: str = None) -> str:
- def browse() -> str:
- driver = get_browser(proxy=proxy)
- try:
- driver.get(f"{cls.url}/")
- WebDriverWait(driver, 1200).until(
- EC.presence_of_element_located((By.ID, "prompt-textarea"))
- )
- javascript = """
+ def browse_access_token(cls, proxy: str = None) -> tuple[str, dict]:
+ driver = get_browser(proxy=proxy)
+ try:
+ driver.get(f"{cls.url}/")
+ WebDriverWait(driver, 1200).until(
+ EC.presence_of_element_located((By.ID, "prompt-textarea"))
+ )
+ javascript = """
access_token = (await (await fetch('/api/auth/session')).json())['accessToken'];
expires = new Date(); expires.setTime(expires.getTime() + 60 * 60 * 24 * 7); // One week
document.cookie = 'access_token=' + access_token + ';expires=' + expires.toUTCString() + ';path=/';
return access_token;
"""
- return driver.execute_script(javascript)
- finally:
- driver.quit()
- loop = get_event_loop()
- return await loop.run_in_executor(
- None,
- browse
- )
-
-async def get_arkose_token(proxy: str = None, timeout: int = None) -> str:
- config = {
- "pkey": "3D86FBBA-9D22-402A-B512-3420086BA6CC",
- "surl": "https://tcr9i.chat.openai.com",
- "headers": {
- "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'
- },
- "site": "https://chat.openai.com",
- }
- args_for_request = get_values_for_request(config)
- async with StreamSession(
- proxies={"https": proxy},
- impersonate="chrome107",
- timeout=timeout
- ) as session:
+ return driver.execute_script(javascript), get_driver_cookies(driver)
+ finally:
+ driver.quit()
+
+ @classmethod
+ async def get_arkose_token(cls, session: StreamSession) -> str:
+ config = {
+ "pkey": "3D86FBBA-9D22-402A-B512-3420086BA6CC",
+ "surl": "https://tcr9i.chat.openai.com",
+ "headers": {
+ "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'
+ },
+ "site": cls.url,
+ }
+ args_for_request = get_values_for_request(config)
async with session.post(**args_for_request) as response:
response.raise_for_status()
decoded_json = await response.json()
@@ -236,23 +358,47 @@ class Response():
def __init__(
self,
generator: AsyncResult,
- fields: ResponseFields,
action: str,
messages: Messages,
options: dict
):
- self.aiter, self.copy = tee(generator)
- self.fields = fields
- self.action = action
+ self._generator = generator
+ self.action: str = action
+ self.is_end: bool = False
+ self._message = None
self._messages = messages
self._options = options
+ self._fields = None
+
+ async def generator(self):
+ if self._generator:
+ self._generator = None
+ chunks = []
+ async for chunk in self._generator:
+ if isinstance(chunk, ResponseFields):
+ self._fields = chunk
+ else:
+ yield chunk
+ chunks.append(str(chunk))
+ self._message = "".join(chunks)
+ if not self._fields:
+ raise RuntimeError("Missing response fields")
+ self.is_end = self._fields._end_turn.is_end
def __aiter__(self):
- return self.aiter
+ return self.generator()
@async_cached_property
async def message(self) -> str:
- return "".join([chunk async for chunk in self.copy])
+ [_ async for _ in self.generator()]
+ return self._message
+
+ async def get_fields(self):
+ [_ async for _ in self.generator()]
+ return {
+ "conversation_id": self._fields.conversation_id,
+ "parent_id": self._fields.message_id,
+ }
async def next(self, prompt: str, **kwargs) -> Response:
return await OpenaiChat.create(
@@ -260,20 +406,19 @@ class Response():
prompt=prompt,
messages=await self.messages,
action="next",
- conversation_id=self.fields.conversation_id,
- parent_id=self.fields.message_id,
+ **await self.get_fields(),
**kwargs
)
async def do_continue(self, **kwargs) -> Response:
- if self.end_turn:
+ fields = await self.get_fields()
+ if self.is_end:
raise RuntimeError("Can't continue message. Message already finished.")
return await OpenaiChat.create(
**self._options,
messages=await self.messages,
action="continue",
- conversation_id=self.fields.conversation_id,
- parent_id=self.fields.message_id,
+ **fields,
**kwargs
)
@@ -284,8 +429,7 @@ class Response():
**self._options,
messages=self._messages,
action="variant",
- conversation_id=self.fields.conversation_id,
- parent_id=self.fields.message_id,
+ **await self.get_fields(),
**kwargs
)
@@ -295,8 +439,4 @@ class Response():
messages.append({
"role": "assistant", "content": await self.message
})
- return messages
-
- @property
- def end_turn(self):
- return self.fields._end_turn.is_end \ No newline at end of file
+ return messages \ No newline at end of file