gpt4free/g4f/Provider/You.py

41 lines
1.3 KiB
Python
Raw Normal View History

from __future__ import annotations
import json
2023-07-28 13:07:17 +03:00
from ..requests import StreamSession
from ..typing import AsyncGenerator, Messages
from .base_provider import AsyncGeneratorProvider, format_prompt
2023-07-28 13:07:17 +03:00
class You(AsyncGeneratorProvider):
url = "https://you.com"
working = True
2023-07-28 13:07:17 +03:00
supports_gpt_35_turbo = True
@classmethod
async def create_async_generator(
cls,
2023-07-28 13:07:17 +03:00
model: str,
messages: Messages,
proxy: str = None,
timeout: int = 120,
**kwargs,
) -> AsyncGenerator:
async with StreamSession(proxies={"https": proxy}, impersonate="chrome107", timeout=timeout) as session:
headers = {
"Accept": "text/event-stream",
"Referer": f"{cls.url}/search?fromSearchBar=true&tbm=youchat",
}
data = {"q": format_prompt(messages), "domain": "youchat", "chat": ""}
async with session.get(
f"{cls.url}/api/streamingSearch",
params=data,
headers=headers
) 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])