gpt4free/g4f/Provider/You.py

225 lines
8.2 KiB
Python
Raw Normal View History

import re
import json
import base64
import uuid
2023-07-28 10:07:17 +00:00
from ..typing import AsyncResult, Messages, ImageType, Cookies
from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
from .helper import format_prompt
2024-04-21 13:15:55 +00:00
from ..image import ImageResponse, ImagePreview, to_bytes, is_accepted_format
2024-04-07 10:53:58 +00:00
from ..requests import StreamSession, FormData, raise_for_status
from .you.har_file import get_telemetry_ids
2024-04-21 05:38:36 +00:00
from .. import debug
class You(AsyncGeneratorProvider, ProviderModelMixin):
2024-04-21 23:27:48 +00:00
label = "You.com"
url = "https://you.com"
working = True
2023-07-28 10:07:17 +00:00
supports_gpt_35_turbo = True
supports_gpt_4 = True
default_model = "gpt-3.5-turbo"
2024-04-21 23:27:48 +00:00
default_vision_model = "agent"
image_models = ["dall-e"]
models = [
2024-04-21 23:27:48 +00:00
default_model,
2024-05-14 22:45:13 +00:00
"gpt-4o",
"gpt-4",
"gpt-4-turbo",
"claude-instant",
"claude-2",
"claude-3-opus",
"claude-3-sonnet",
"claude-3-haiku",
"gemini-pro",
"gemini-1-5-pro",
"databricks-dbrx-instruct",
"command-r",
"command-r-plus",
"llama3",
2024-04-07 10:30:07 +00:00
"zephyr",
2024-04-21 23:27:48 +00:00
default_vision_model,
*image_models
]
model_aliases = {
2024-05-14 22:45:13 +00:00
"claude-v2": "claude-2",
"gpt-4o": "gpt-4o",
}
2024-02-11 06:48:07 +00:00
_cookies = None
_cookies_used = 0
_telemetry_ids = []
@classmethod
async def create_async_generator(
cls,
2023-07-28 10:07:17 +00:00
model: str,
messages: Messages,
stream: bool = True,
2024-02-11 06:48:07 +00:00
image: ImageType = None,
image_name: str = None,
proxy: str = None,
timeout: int = 240,
2024-02-11 06:48:07 +00:00
chat_mode: str = "default",
**kwargs,
) -> AsyncResult:
2024-04-21 23:27:48 +00:00
if image is not None or model == cls.default_vision_model:
chat_mode = "agent"
elif not model or model == cls.default_model:
...
elif model.startswith("dall-e"):
chat_mode = "create"
messages = [messages[-1]]
else:
chat_mode = "custom"
model = cls.get_model(model)
async with StreamSession(
2024-04-21 23:27:48 +00:00
proxy=proxy,
impersonate="chrome",
timeout=(30, timeout)
) as session:
cookies = await cls.get_cookies(session) if chat_mode != "default" else None
2024-04-21 23:27:48 +00:00
upload = ""
if image is not None:
upload_file = await cls.upload_file(
session, cookies,
to_bytes(image), image_name
)
upload = json.dumps([upload_file])
headers = {
"Accept": "text/event-stream",
"Referer": f"{cls.url}/search?fromSearchBar=true&tbm=youchat",
}
data = {
2024-02-11 06:48:07 +00:00
"userFiles": upload,
"q": format_prompt(messages),
"domain": "youchat",
"selectedChatMode": chat_mode,
}
params = {
"userFiles": upload,
"selectedChatMode": chat_mode,
}
if chat_mode == "custom":
2024-05-14 22:45:13 +00:00
# print(f"You model: {model}")
params["selectedAIModel"] = model.replace("-", "_")
2024-05-14 22:45:13 +00:00
async with (session.post if chat_mode == "default" else session.get)(
f"{cls.url}/api/streamingSearch",
2024-02-11 06:48:07 +00:00
data=data,
params=params,
headers=headers,
2024-02-11 06:48:07 +00:00
cookies=cookies
) as response:
await raise_for_status(response)
async for line in response.iter_lines():
2024-02-11 06:48:07 +00:00
if line.startswith(b'event: '):
event = line[7:].decode()
2024-02-11 06:48:07 +00:00
elif line.startswith(b'data: '):
if event in ["youChatUpdate", "youChatToken"]:
data = json.loads(line[6:])
if event == "youChatToken" and event in data:
yield data[event]
elif event == "youChatUpdate" and "t" in data and data["t"] is not None:
2024-04-21 13:15:55 +00:00
if chat_mode == "create":
match = re.search(r"!\[(.+?)\]\((.+?)\)", data["t"])
if match:
if match.group(1) == "fig":
yield ImagePreview(match.group(2), messages[-1]["content"])
else:
yield ImageResponse(match.group(2), match.group(1))
else:
yield data["t"]
else:
2024-04-21 13:15:55 +00:00
yield data["t"]
@classmethod
async def upload_file(cls, client: StreamSession, cookies: Cookies, file: bytes, filename: str = None) -> dict:
2024-02-11 06:48:07 +00:00
async with client.get(
f"{cls.url}/api/get_nonce",
cookies=cookies,
) as response:
await raise_for_status(response)
2024-02-11 06:48:07 +00:00
upload_nonce = await response.text()
data = FormData()
data.add_field('file', file, content_type=is_accepted_format(file), filename=filename)
2024-02-11 06:48:07 +00:00
async with client.post(
f"{cls.url}/api/upload",
data=data,
2024-02-11 06:48:07 +00:00
headers={
"X-Upload-Nonce": upload_nonce,
},
cookies=cookies
) as response:
await raise_for_status(response)
2024-02-11 06:48:07 +00:00
result = await response.json()
result["user_filename"] = filename
result["size"] = len(file)
return result
2024-02-11 06:48:07 +00:00
@classmethod
async def get_cookies(cls, client: StreamSession) -> Cookies:
2024-02-11 06:48:07 +00:00
if not cls._cookies or cls._cookies_used >= 5:
cls._cookies = await cls.create_cookies(client)
cls._cookies_used = 0
cls._cookies_used += 1
return cls._cookies
@classmethod
2024-02-11 06:48:07 +00:00
def get_sdk(cls) -> str:
return base64.standard_b64encode(json.dumps({
"event_id":f"event-id-{str(uuid.uuid4())}",
"app_session_id":f"app-session-id-{str(uuid.uuid4())}",
"persistent_id":f"persistent-id-{uuid.uuid4()}",
"client_sent_at":"","timezone":"",
"stytch_user_id":f"user-live-{uuid.uuid4()}",
"stytch_session_id":f"session-live-{uuid.uuid4()}",
"app":{"identifier":"you.com"},
"sdk":{"identifier":"Stytch.js Javascript SDK","version":"3.3.0"
}}).encode()).decode()
2024-02-11 06:48:07 +00:00
def get_auth() -> str:
2024-04-14 01:37:15 +00:00
auth_uuid = "507a52ad-7e69-496b-aee0-1c9863c7c819"
auth_token = f"public-token-live-{auth_uuid}:public-token-live-{auth_uuid}"
2024-02-11 06:48:07 +00:00
auth = base64.standard_b64encode(auth_token.encode()).decode()
return f"Basic {auth}"
2024-02-11 06:48:07 +00:00
@classmethod
async def create_cookies(cls, client: StreamSession) -> Cookies:
if not cls._telemetry_ids:
2024-05-14 22:45:13 +00:00
try:
cls._telemetry_ids = await get_telemetry_ids()
except RuntimeError as e:
if str(e) == "Event loop is closed":
if debug.logging:
print("Event loop is closed error occurred in create_cookies.")
else:
raise
2024-02-11 06:48:07 +00:00
user_uuid = str(uuid.uuid4())
2024-04-21 05:38:36 +00:00
telemetry_id = cls._telemetry_ids.pop()
if debug.logging:
print(f"Use telemetry_id: {telemetry_id}")
2024-02-11 06:48:07 +00:00
async with client.post(
"https://web.stytch.com/sdk/v1/passwords",
headers={
"Authorization": cls.get_auth(),
"X-SDK-Client": cls.get_sdk(),
2024-04-11 17:10:03 +00:00
"X-SDK-Parent-Host": cls.url,
"Origin": "https://you.com",
"Referer": "https://you.com/"
2024-02-11 06:48:07 +00:00
},
json={
2024-04-21 05:38:36 +00:00
"dfp_telemetry_id": telemetry_id,
2024-02-11 06:48:07 +00:00
"email": f"{user_uuid}@gmail.com",
"password": f"{user_uuid}#{user_uuid}",
"session_duration_minutes": 129600
}
) as response:
await raise_for_status(response)
2024-02-11 06:48:07 +00:00
session = (await response.json())["data"]
2024-04-14 01:37:15 +00:00
2024-02-11 06:48:07 +00:00
return {
"stytch_session": session["session_token"],
'stytch_session_jwt': session["session_jwt"],
'ydc_stytch_session': session["session_token"],
'ydc_stytch_session_jwt': session["session_jwt"],
2024-05-14 22:45:13 +00:00
}