summaryrefslogtreecommitdiffstats
path: root/g4f/Provider/You.py
blob: 1fbe1eb6b3905044e605e68f0a83538e10412099 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from aiohttp import ClientSession
import json

from ..typing import AsyncGenerator
from .base_provider import AsyncGeneratorProvider, format_prompt, get_cookies


class You(AsyncGeneratorProvider):
    url = "https://you.com"
    working = True
    supports_gpt_35_turbo = True
    supports_stream = True

    @staticmethod
    async def create_async_generator(
        model: str,
        messages: list[dict[str, str]],
        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])