2024-05-15 22:07:49 +03:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-03-11 04:41:59 +03:00
|
|
|
import re
|
2023-08-28 02:43:45 +03:00
|
|
|
import json
|
2024-02-11 03:59:57 +03:00
|
|
|
import base64
|
|
|
|
import uuid
|
2023-07-28 13:07:17 +03:00
|
|
|
|
2024-02-27 13:55:40 +03:00
|
|
|
from ..typing import AsyncResult, Messages, ImageType, Cookies
|
2024-03-11 04:41:59 +03:00
|
|
|
from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
|
2024-03-15 13:46:06 +03:00
|
|
|
from .helper import format_prompt
|
2024-05-21 23:58:56 +03:00
|
|
|
from ..image import ImageResponse, ImagePreview, EXTENSIONS_MAP, to_bytes, is_accepted_format
|
2024-04-07 13:53:58 +03:00
|
|
|
from ..requests import StreamSession, FormData, raise_for_status
|
2024-04-21 08:22:59 +03:00
|
|
|
from .you.har_file import get_telemetry_ids
|
2024-04-21 08:38:36 +03:00
|
|
|
from .. import debug
|
2024-04-07 11:36:13 +03:00
|
|
|
|
2024-03-11 04:41:59 +03:00
|
|
|
class You(AsyncGeneratorProvider, ProviderModelMixin):
|
2024-04-22 02:27:48 +03:00
|
|
|
label = "You.com"
|
2023-08-28 02:43:45 +03:00
|
|
|
url = "https://you.com"
|
|
|
|
working = True
|
2024-08-29 09:03:32 +03:00
|
|
|
default_model = "gpt-4o-mini"
|
2024-04-22 02:27:48 +03:00
|
|
|
default_vision_model = "agent"
|
|
|
|
image_models = ["dall-e"]
|
2024-03-11 04:41:59 +03:00
|
|
|
models = [
|
2024-04-22 02:27:48 +03:00
|
|
|
default_model,
|
2024-05-15 01:45:13 +03:00
|
|
|
"gpt-4o",
|
2024-03-11 04:41:59 +03:00
|
|
|
"gpt-4-turbo",
|
2024-07-25 09:21:55 +03:00
|
|
|
"gpt-4",
|
|
|
|
"claude-3.5-sonnet",
|
2024-03-11 04:41:59 +03:00
|
|
|
"claude-3-opus",
|
|
|
|
"claude-3-sonnet",
|
2024-05-04 21:34:51 +03:00
|
|
|
"claude-3-haiku",
|
2024-07-25 09:21:55 +03:00
|
|
|
"claude-2",
|
|
|
|
"llama-3.1-70b",
|
|
|
|
"llama-3",
|
|
|
|
"gemini-1-5-flash",
|
2024-05-04 21:34:51 +03:00
|
|
|
"gemini-1-5-pro",
|
2024-07-25 09:21:55 +03:00
|
|
|
"gemini-1-0-pro",
|
2024-05-04 21:34:51 +03:00
|
|
|
"databricks-dbrx-instruct",
|
|
|
|
"command-r",
|
|
|
|
"command-r-plus",
|
2024-07-25 09:21:55 +03:00
|
|
|
"dolphin-2.5",
|
2024-04-22 02:27:48 +03:00
|
|
|
default_vision_model,
|
|
|
|
*image_models
|
2024-03-11 04:41:59 +03:00
|
|
|
]
|
2024-02-11 09:48:07 +03:00
|
|
|
_cookies = None
|
|
|
|
_cookies_used = 0
|
2024-04-21 08:22:59 +03:00
|
|
|
_telemetry_ids = []
|
2023-09-12 01:47:03 +03:00
|
|
|
|
|
|
|
@classmethod
|
2023-08-28 02:43:45 +03:00
|
|
|
async def create_async_generator(
|
2023-09-12 01:47:03 +03:00
|
|
|
cls,
|
2023-07-28 13:07:17 +03:00
|
|
|
model: str,
|
2023-10-08 14:59:56 +03:00
|
|
|
messages: Messages,
|
2024-04-07 11:36:13 +03:00
|
|
|
stream: bool = True,
|
2024-02-11 09:48:07 +03:00
|
|
|
image: ImageType = None,
|
|
|
|
image_name: str = None,
|
2023-09-12 01:47:03 +03:00
|
|
|
proxy: str = None,
|
2024-04-10 09:14:50 +03:00
|
|
|
timeout: int = 240,
|
2024-02-11 09:48:07 +03:00
|
|
|
chat_mode: str = "default",
|
2023-08-28 02:43:45 +03:00
|
|
|
**kwargs,
|
2024-02-27 13:55:40 +03:00
|
|
|
) -> AsyncResult:
|
2024-04-22 02:27:48 +03:00
|
|
|
if image is not None or model == cls.default_vision_model:
|
2024-03-12 04:06:06 +03:00
|
|
|
chat_mode = "agent"
|
|
|
|
elif not model or model == cls.default_model:
|
2024-04-07 11:36:13 +03:00
|
|
|
...
|
2024-03-12 04:06:06 +03:00
|
|
|
elif model.startswith("dall-e"):
|
|
|
|
chat_mode = "create"
|
2024-04-10 09:14:50 +03:00
|
|
|
messages = [messages[-1]]
|
2024-03-12 04:06:06 +03:00
|
|
|
else:
|
|
|
|
chat_mode = "custom"
|
|
|
|
model = cls.get_model(model)
|
2024-03-15 13:46:06 +03:00
|
|
|
async with StreamSession(
|
2024-04-22 02:27:48 +03:00
|
|
|
proxy=proxy,
|
2024-04-10 09:14:50 +03:00
|
|
|
impersonate="chrome",
|
|
|
|
timeout=(30, timeout)
|
2024-03-12 04:06:06 +03:00
|
|
|
) as session:
|
|
|
|
cookies = await cls.get_cookies(session) if chat_mode != "default" else None
|
2024-04-22 02:27:48 +03: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])
|
2023-09-12 01:47:03 +03:00
|
|
|
headers = {
|
2024-03-15 13:46:06 +03:00
|
|
|
"Accept": "text/event-stream",
|
|
|
|
"Referer": f"{cls.url}/search?fromSearchBar=true&tbm=youchat",
|
2023-09-12 01:47:03 +03:00
|
|
|
}
|
2024-02-11 03:59:57 +03:00
|
|
|
data = {
|
2024-02-11 09:48:07 +03:00
|
|
|
"userFiles": upload,
|
2024-02-11 03:59:57 +03:00
|
|
|
"q": format_prompt(messages),
|
|
|
|
"domain": "youchat",
|
2024-02-11 09:59:45 +03:00
|
|
|
"selectedChatMode": chat_mode,
|
2024-05-21 23:58:56 +03:00
|
|
|
"conversationTurnId": str(uuid.uuid4()),
|
|
|
|
"chatId": str(uuid.uuid4()),
|
2024-02-11 03:59:57 +03:00
|
|
|
}
|
2024-02-12 13:41:27 +03:00
|
|
|
params = {
|
|
|
|
"userFiles": upload,
|
|
|
|
"selectedChatMode": chat_mode,
|
|
|
|
}
|
2024-03-11 04:41:59 +03:00
|
|
|
if chat_mode == "custom":
|
2024-05-17 19:40:34 +03:00
|
|
|
if debug.logging:
|
|
|
|
print(f"You model: {model}")
|
|
|
|
params["selectedAiModel"] = model.replace("-", "_")
|
|
|
|
|
2024-03-12 04:06:06 +03:00
|
|
|
async with (session.post if chat_mode == "default" else session.get)(
|
2023-10-08 14:59:56 +03:00
|
|
|
f"{cls.url}/api/streamingSearch",
|
2024-05-21 23:58:56 +03:00
|
|
|
data=data if chat_mode == "default" else None,
|
|
|
|
params=params if chat_mode == "default" else data,
|
2024-02-11 03:59:57 +03:00
|
|
|
headers=headers,
|
2024-02-11 09:48:07 +03:00
|
|
|
cookies=cookies
|
2023-10-05 06:13:37 +03:00
|
|
|
) as response:
|
2024-03-12 04:06:06 +03:00
|
|
|
await raise_for_status(response)
|
2024-03-15 13:46:06 +03:00
|
|
|
async for line in response.iter_lines():
|
2024-02-11 09:48:07 +03:00
|
|
|
if line.startswith(b'event: '):
|
2024-03-15 13:46:06 +03:00
|
|
|
event = line[7:].decode()
|
2024-02-11 09:48:07 +03:00
|
|
|
elif line.startswith(b'data: '):
|
2024-02-27 13:55:40 +03:00
|
|
|
if event in ["youChatUpdate", "youChatToken"]:
|
2024-03-15 13:46:06 +03:00
|
|
|
data = json.loads(line[6:])
|
2024-05-17 19:40:34 +03:00
|
|
|
if event == "youChatToken" and event in data and data[event]:
|
2024-02-27 13:55:40 +03:00
|
|
|
yield data[event]
|
2024-05-17 19:40:34 +03:00
|
|
|
elif event == "youChatUpdate" and "t" in data and data["t"]:
|
2024-04-21 16:15:55 +03: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"]
|
2024-03-11 04:41:59 +03:00
|
|
|
else:
|
2024-04-21 16:15:55 +03:00
|
|
|
yield data["t"]
|
2024-02-11 03:59:57 +03:00
|
|
|
|
|
|
|
@classmethod
|
2024-03-15 13:46:06 +03:00
|
|
|
async def upload_file(cls, client: StreamSession, cookies: Cookies, file: bytes, filename: str = None) -> dict:
|
2024-02-11 09:48:07 +03:00
|
|
|
async with client.get(
|
|
|
|
f"{cls.url}/api/get_nonce",
|
|
|
|
cookies=cookies,
|
|
|
|
) as response:
|
2024-03-12 04:06:06 +03:00
|
|
|
await raise_for_status(response)
|
2024-02-11 09:48:07 +03:00
|
|
|
upload_nonce = await response.text()
|
2024-03-15 16:55:53 +03:00
|
|
|
data = FormData()
|
2024-05-21 23:58:56 +03:00
|
|
|
content_type = is_accepted_format(file)
|
|
|
|
filename = f"image.{EXTENSIONS_MAP[content_type]}" if filename is None else filename
|
|
|
|
data.add_field('file', file, content_type=content_type, filename=filename)
|
2024-02-11 09:48:07 +03:00
|
|
|
async with client.post(
|
|
|
|
f"{cls.url}/api/upload",
|
2024-03-15 16:55:53 +03:00
|
|
|
data=data,
|
2024-02-11 09:48:07 +03:00
|
|
|
headers={
|
|
|
|
"X-Upload-Nonce": upload_nonce,
|
|
|
|
},
|
|
|
|
cookies=cookies
|
|
|
|
) as response:
|
2024-03-12 04:06:06 +03:00
|
|
|
await raise_for_status(response)
|
2024-02-11 09:48:07 +03:00
|
|
|
result = await response.json()
|
|
|
|
result["user_filename"] = filename
|
|
|
|
result["size"] = len(file)
|
|
|
|
return result
|
2024-02-11 03:59:57 +03:00
|
|
|
|
2024-02-11 09:48:07 +03:00
|
|
|
@classmethod
|
2024-03-15 13:46:06 +03:00
|
|
|
async def get_cookies(cls, client: StreamSession) -> Cookies:
|
2024-02-11 09:48:07 +03: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
|
2024-02-11 03:59:57 +03:00
|
|
|
|
|
|
|
@classmethod
|
2024-02-11 09:48:07 +03:00
|
|
|
def get_sdk(cls) -> str:
|
2024-02-11 03:59:57 +03:00
|
|
|
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 09:48:07 +03:00
|
|
|
def get_auth() -> str:
|
2024-04-14 04:37:15 +03:00
|
|
|
auth_uuid = "507a52ad-7e69-496b-aee0-1c9863c7c819"
|
|
|
|
auth_token = f"public-token-live-{auth_uuid}:public-token-live-{auth_uuid}"
|
2024-02-11 09:48:07 +03:00
|
|
|
auth = base64.standard_b64encode(auth_token.encode()).decode()
|
2024-02-11 09:59:45 +03:00
|
|
|
return f"Basic {auth}"
|
2024-02-11 09:48:07 +03:00
|
|
|
|
2024-02-11 03:59:57 +03:00
|
|
|
@classmethod
|
2024-03-15 13:46:06 +03:00
|
|
|
async def create_cookies(cls, client: StreamSession) -> Cookies:
|
2024-04-21 08:22:59 +03:00
|
|
|
if not cls._telemetry_ids:
|
2024-05-15 22:07:49 +03:00
|
|
|
cls._telemetry_ids = await get_telemetry_ids()
|
2024-02-11 09:48:07 +03:00
|
|
|
user_uuid = str(uuid.uuid4())
|
2024-04-21 08:38:36 +03:00
|
|
|
telemetry_id = cls._telemetry_ids.pop()
|
|
|
|
if debug.logging:
|
|
|
|
print(f"Use telemetry_id: {telemetry_id}")
|
2024-02-11 09:48:07 +03: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 20:10:03 +03:00
|
|
|
"X-SDK-Parent-Host": cls.url,
|
|
|
|
"Origin": "https://you.com",
|
|
|
|
"Referer": "https://you.com/"
|
2024-02-11 09:48:07 +03:00
|
|
|
},
|
|
|
|
json={
|
2024-04-21 08:38:36 +03:00
|
|
|
"dfp_telemetry_id": telemetry_id,
|
2024-02-11 09:48:07 +03:00
|
|
|
"email": f"{user_uuid}@gmail.com",
|
|
|
|
"password": f"{user_uuid}#{user_uuid}",
|
|
|
|
"session_duration_minutes": 129600
|
|
|
|
}
|
|
|
|
) as response:
|
2024-03-12 04:06:06 +03:00
|
|
|
await raise_for_status(response)
|
2024-02-11 09:48:07 +03:00
|
|
|
session = (await response.json())["data"]
|
2024-04-14 04:37:15 +03:00
|
|
|
|
2024-02-11 09:48:07 +03: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-07-25 09:21:55 +03:00
|
|
|
}
|