2023-08-27 11:37:44 -04:00
|
|
|
import urllib.parse, json
|
2023-07-28 06:07:17 -04:00
|
|
|
|
2023-08-27 11:37:44 -04:00
|
|
|
from curl_cffi import requests
|
|
|
|
from ..typing import Any, CreateResult
|
2023-07-28 06:07:17 -04:00
|
|
|
from .base_provider import BaseProvider
|
|
|
|
|
|
|
|
|
|
|
|
class You(BaseProvider):
|
2023-08-27 11:37:44 -04:00
|
|
|
url = "https://you.com"
|
|
|
|
working = True
|
2023-07-28 06:07:17 -04:00
|
|
|
supports_gpt_35_turbo = True
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def create_completion(
|
|
|
|
model: str,
|
|
|
|
messages: list[dict[str, str]],
|
2023-08-27 11:37:44 -04:00
|
|
|
stream: bool, **kwargs: Any) -> CreateResult:
|
|
|
|
|
2023-08-23 03:26:00 -04:00
|
|
|
url_param = _create_url_param(messages, kwargs.get("history", []))
|
2023-08-27 11:37:44 -04:00
|
|
|
headers = _create_header()
|
|
|
|
|
|
|
|
response = requests.get(f"https://you.com/api/streamingSearch?{url_param}",
|
|
|
|
headers=headers, impersonate="chrome107")
|
|
|
|
|
2023-07-28 06:07:17 -04:00
|
|
|
response.raise_for_status()
|
2023-08-25 12:22:25 -04:00
|
|
|
|
2023-08-22 17:27:34 -04:00
|
|
|
start = 'data: {"youChatToken": '
|
|
|
|
for line in response.content.splitlines():
|
|
|
|
line = line.decode('utf-8')
|
|
|
|
if line.startswith(start):
|
|
|
|
yield json.loads(line[len(start): -1])
|
2023-07-28 06:07:17 -04:00
|
|
|
|
2023-08-23 03:26:00 -04:00
|
|
|
def _create_url_param(messages: list[dict[str, str]], history: list[dict[str, str]]):
|
|
|
|
prompt = ""
|
|
|
|
for message in messages:
|
|
|
|
prompt += "%s: %s\n" % (message["role"], message["content"])
|
|
|
|
prompt += "assistant:"
|
|
|
|
chat = _convert_chat(history)
|
2023-07-28 06:07:17 -04:00
|
|
|
param = {"q": prompt, "domain": "youchat", "chat": chat}
|
|
|
|
return urllib.parse.urlencode(param)
|
|
|
|
|
|
|
|
|
|
|
|
def _convert_chat(messages: list[dict[str, str]]):
|
|
|
|
message_iter = iter(messages)
|
|
|
|
return [
|
|
|
|
{"question": user["content"], "answer": assistant["content"]}
|
|
|
|
for user, assistant in zip(message_iter, message_iter)
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def _create_header():
|
|
|
|
return {
|
|
|
|
"accept": "text/event-stream",
|
|
|
|
"referer": "https://you.com/search?fromSearchBar=true&tbm=youchat",
|
2023-08-22 17:27:34 -04:00
|
|
|
}
|