2023-09-03 08:26:26 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-08-27 23:43:45 +00:00
|
|
|
import json
|
2023-07-28 10:07:17 +00:00
|
|
|
|
2023-10-05 03:13:37 +00:00
|
|
|
from ..requests import StreamSession
|
2023-10-08 11:59:56 +00:00
|
|
|
from ..typing import AsyncGenerator, Messages
|
2023-09-11 22:47:03 +00:00
|
|
|
from .base_provider import AsyncGeneratorProvider, format_prompt
|
2023-07-28 10:07:17 +00:00
|
|
|
|
|
|
|
|
2023-08-27 23:43:45 +00:00
|
|
|
class You(AsyncGeneratorProvider):
|
|
|
|
url = "https://you.com"
|
|
|
|
working = True
|
2023-07-28 10:07:17 +00:00
|
|
|
supports_gpt_35_turbo = True
|
|
|
|
|
2023-09-11 22:47:03 +00:00
|
|
|
|
|
|
|
@classmethod
|
2023-08-27 23:43:45 +00:00
|
|
|
async def create_async_generator(
|
2023-09-11 22:47:03 +00:00
|
|
|
cls,
|
2023-07-28 10:07:17 +00:00
|
|
|
model: str,
|
2023-10-08 11:59:56 +00:00
|
|
|
messages: Messages,
|
2023-09-11 22:47:03 +00:00
|
|
|
proxy: str = None,
|
2023-10-08 11:59:56 +00:00
|
|
|
timeout: int = 120,
|
2023-08-27 23:43:45 +00:00
|
|
|
**kwargs,
|
|
|
|
) -> AsyncGenerator:
|
2023-10-05 03:13:37 +00:00
|
|
|
async with StreamSession(proxies={"https": proxy}, impersonate="chrome107", timeout=timeout) as session:
|
2023-09-11 22:47:03 +00:00
|
|
|
headers = {
|
|
|
|
"Accept": "text/event-stream",
|
2023-10-08 11:59:56 +00:00
|
|
|
"Referer": f"{cls.url}/search?fromSearchBar=true&tbm=youchat",
|
2023-09-11 22:47:03 +00:00
|
|
|
}
|
2023-10-08 11:59:56 +00:00
|
|
|
data = {"q": format_prompt(messages), "domain": "youchat", "chat": ""}
|
2023-10-05 03:13:37 +00:00
|
|
|
async with session.get(
|
2023-10-08 11:59:56 +00:00
|
|
|
f"{cls.url}/api/streamingSearch",
|
|
|
|
params=data,
|
2023-09-11 22:47:03 +00:00
|
|
|
headers=headers
|
2023-10-05 03:13:37 +00:00
|
|
|
) as response:
|
|
|
|
response.raise_for_status()
|
|
|
|
start = b'data: {"youChatToken": '
|
|
|
|
async for line in response.iter_lines():
|
|
|
|
if line.startswith(start):
|
|
|
|
yield json.loads(line[len(start):-1])
|