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-09-03 04:26:26 -04:00
|
|
|
from aiohttp import ClientSession
|
|
|
|
|
2023-08-27 19:43:45 -04:00
|
|
|
from ..typing import AsyncGenerator
|
|
|
|
from .base_provider import AsyncGeneratorProvider, format_prompt, get_cookies
|
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-08-27 19:43:45 -04:00
|
|
|
supports_stream = True
|
2023-07-28 06:07:17 -04:00
|
|
|
|
|
|
|
@staticmethod
|
2023-08-27 19:43:45 -04:00
|
|
|
async def create_async_generator(
|
2023-07-28 06:07:17 -04:00
|
|
|
model: str,
|
|
|
|
messages: list[dict[str, str]],
|
2023-08-27 19:43:45 -04:00
|
|
|
cookies: dict = None,
|
|
|
|
**kwargs,
|
|
|
|
) -> AsyncGenerator:
|
|
|
|
if not cookies:
|
|
|
|
cookies = get_cookies("you.com")
|
|
|
|
headers = {
|
|
|
|
"Accept": "text/event-stream",
|
|
|
|
"Referer": "https://you.com/search?fromSearchBar=true&tbm=youchat",
|
|
|
|
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/116.0"
|
|
|
|
}
|
|
|
|
async with ClientSession(headers=headers, cookies=cookies) as session:
|
|
|
|
async with session.get(
|
|
|
|
"https://you.com/api/streamingSearch",
|
|
|
|
params={"q": format_prompt(messages), "domain": "youchat", "chat": ""},
|
|
|
|
) as response:
|
|
|
|
start = 'data: {"youChatToken": '
|
|
|
|
async for line in response.content:
|
|
|
|
line = line.decode('utf-8')
|
|
|
|
if line.startswith(start):
|
|
|
|
yield json.loads(line[len(start): -2])
|