You.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. from __future__ import annotations
  2. import json
  3. import base64
  4. import uuid
  5. from aiohttp import ClientSession, FormData, BaseConnector
  6. from ..typing import AsyncResult, Messages, ImageType, Cookies
  7. from .base_provider import AsyncGeneratorProvider
  8. from ..providers.helper import get_connector, format_prompt
  9. from ..image import to_bytes
  10. from ..requests.defaults import DEFAULT_HEADERS
  11. class You(AsyncGeneratorProvider):
  12. url = "https://you.com"
  13. working = True
  14. supports_gpt_35_turbo = True
  15. supports_gpt_4 = True
  16. _cookies = None
  17. _cookies_used = 0
  18. @classmethod
  19. async def create_async_generator(
  20. cls,
  21. model: str,
  22. messages: Messages,
  23. image: ImageType = None,
  24. image_name: str = None,
  25. connector: BaseConnector = None,
  26. proxy: str = None,
  27. chat_mode: str = "default",
  28. **kwargs,
  29. ) -> AsyncResult:
  30. async with ClientSession(
  31. connector=get_connector(connector, proxy),
  32. headers=DEFAULT_HEADERS
  33. ) as client:
  34. if image:
  35. chat_mode = "agent"
  36. elif model == "gpt-4":
  37. chat_mode = model
  38. cookies = await cls.get_cookies(client) if chat_mode != "default" else None
  39. upload = json.dumps([await cls.upload_file(client, cookies, to_bytes(image), image_name)]) if image else ""
  40. #questions = [message["content"] for message in messages if message["role"] == "user"]
  41. # chat = [
  42. # {"question": questions[idx-1], "answer": message["content"]}
  43. # for idx, message in enumerate(messages)
  44. # if message["role"] == "assistant"
  45. # and idx < len(questions)
  46. # ]
  47. headers = {
  48. "Accept": "text/event-stream",
  49. "Referer": f"{cls.url}/search?fromSearchBar=true&tbm=youchat",
  50. }
  51. data = {
  52. "userFiles": upload,
  53. "q": format_prompt(messages),
  54. "domain": "youchat",
  55. "selectedChatMode": chat_mode,
  56. #"chat": json.dumps(chat),
  57. }
  58. params = {
  59. "userFiles": upload,
  60. "selectedChatMode": chat_mode,
  61. }
  62. async with (client.post if chat_mode == "default" else client.get)(
  63. f"{cls.url}/api/streamingSearch",
  64. data=data,
  65. params=params,
  66. headers=headers,
  67. cookies=cookies
  68. ) as response:
  69. response.raise_for_status()
  70. async for line in response.content:
  71. if line.startswith(b'event: '):
  72. event = line[7:-1].decode()
  73. elif line.startswith(b'data: '):
  74. if event in ["youChatUpdate", "youChatToken"]:
  75. data = json.loads(line[6:-1])
  76. if event == "youChatToken" and event in data:
  77. yield data[event]
  78. elif event == "youChatUpdate" and "t" in data:
  79. yield data["t"]
  80. @classmethod
  81. async def upload_file(cls, client: ClientSession, cookies: Cookies, file: bytes, filename: str = None) -> dict:
  82. async with client.get(
  83. f"{cls.url}/api/get_nonce",
  84. cookies=cookies,
  85. ) as response:
  86. response.raise_for_status()
  87. upload_nonce = await response.text()
  88. data = FormData()
  89. data.add_field('file', file, filename=filename)
  90. async with client.post(
  91. f"{cls.url}/api/upload",
  92. data=data,
  93. headers={
  94. "X-Upload-Nonce": upload_nonce,
  95. },
  96. cookies=cookies
  97. ) as response:
  98. if not response.ok:
  99. raise RuntimeError(f"Response: {await response.text()}")
  100. result = await response.json()
  101. result["user_filename"] = filename
  102. result["size"] = len(file)
  103. return result
  104. @classmethod
  105. async def get_cookies(cls, client: ClientSession) -> Cookies:
  106. if not cls._cookies or cls._cookies_used >= 5:
  107. cls._cookies = await cls.create_cookies(client)
  108. cls._cookies_used = 0
  109. cls._cookies_used += 1
  110. return cls._cookies
  111. @classmethod
  112. def get_sdk(cls) -> str:
  113. return base64.standard_b64encode(json.dumps({
  114. "event_id":f"event-id-{str(uuid.uuid4())}",
  115. "app_session_id":f"app-session-id-{str(uuid.uuid4())}",
  116. "persistent_id":f"persistent-id-{uuid.uuid4()}",
  117. "client_sent_at":"","timezone":"",
  118. "stytch_user_id":f"user-live-{uuid.uuid4()}",
  119. "stytch_session_id":f"session-live-{uuid.uuid4()}",
  120. "app":{"identifier":"you.com"},
  121. "sdk":{"identifier":"Stytch.js Javascript SDK","version":"3.3.0"
  122. }}).encode()).decode()
  123. def get_auth() -> str:
  124. auth_uuid = "507a52ad-7e69-496b-aee0-1c9863c7c8"
  125. auth_token = f"public-token-live-{auth_uuid}bb:public-token-live-{auth_uuid}19"
  126. auth = base64.standard_b64encode(auth_token.encode()).decode()
  127. return f"Basic {auth}"
  128. @classmethod
  129. async def create_cookies(cls, client: ClientSession) -> Cookies:
  130. user_uuid = str(uuid.uuid4())
  131. async with client.post(
  132. "https://web.stytch.com/sdk/v1/passwords",
  133. headers={
  134. "Authorization": cls.get_auth(),
  135. "X-SDK-Client": cls.get_sdk(),
  136. "X-SDK-Parent-Host": cls.url
  137. },
  138. json={
  139. "email": f"{user_uuid}@gmail.com",
  140. "password": f"{user_uuid}#{user_uuid}",
  141. "session_duration_minutes": 129600
  142. }
  143. ) as response:
  144. if not response.ok:
  145. raise RuntimeError(f"Response: {await response.text()}")
  146. session = (await response.json())["data"]
  147. return {
  148. "stytch_session": session["session_token"],
  149. 'stytch_session_jwt': session["session_jwt"],
  150. 'ydc_stytch_session': session["session_token"],
  151. 'ydc_stytch_session_jwt': session["session_jwt"],
  152. }