2023-09-03 04:26:26 -04:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-08-27 19:43:45 -04:00
|
|
|
import json
|
2023-07-28 06:07:17 -04:00
|
|
|
|
2023-10-04 23:13:37 -04:00
|
|
|
from ..requests import StreamSession
|
2023-08-27 19:43:45 -04:00
|
|
|
from ..typing import AsyncGenerator
|
2023-09-11 18:47:03 -04:00
|
|
|
from .base_provider import AsyncGeneratorProvider, format_prompt
|
2023-07-28 06:07:17 -04:00
|
|
|
|
|
|
|
|
2023-08-27 19:43:45 -04:00
|
|
|
class You(AsyncGeneratorProvider):
|
|
|
|
url = "https://you.com"
|
|
|
|
working = True
|
2023-07-28 06:07:17 -04:00
|
|
|
supports_gpt_35_turbo = True
|
|
|
|
|
2023-09-11 18:47:03 -04:00
|
|
|
|
|
|
|
@classmethod
|
2023-08-27 19:43:45 -04:00
|
|
|
async def create_async_generator(
|
2023-09-11 18:47:03 -04:00
|
|
|
cls,
|
2023-07-28 06:07:17 -04:00
|
|
|
model: str,
|
|
|
|
messages: list[dict[str, str]],
|
2023-09-11 18:47:03 -04:00
|
|
|
proxy: str = None,
|
2023-10-04 23:13:37 -04:00
|
|
|
timeout: int = 30,
|
2023-08-27 19:43:45 -04:00
|
|
|
**kwargs,
|
|
|
|
) -> AsyncGenerator:
|
2023-10-04 23:13:37 -04:00
|
|
|
async with StreamSession(proxies={"https": proxy}, impersonate="chrome107", timeout=timeout) as session:
|
2023-09-11 18:47:03 -04:00
|
|
|
headers = {
|
|
|
|
"Accept": "text/event-stream",
|
|
|
|
"Referer": "https://you.com/search?fromSearchBar=true&tbm=youchat",
|
|
|
|
}
|
2023-10-04 23:13:37 -04:00
|
|
|
async with session.get(
|
2023-08-27 19:43:45 -04:00
|
|
|
"https://you.com/api/streamingSearch",
|
|
|
|
params={"q": format_prompt(messages), "domain": "youchat", "chat": ""},
|
2023-09-11 18:47:03 -04:00
|
|
|
headers=headers
|
2023-10-04 23:13:37 -04: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])
|