2024-01-23 03:50:44 +03:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import random
|
|
|
|
import json
|
|
|
|
|
|
|
|
from ..typing import AsyncResult, Messages
|
2024-04-17 11:33:23 +03:00
|
|
|
from ..requests import StreamSession, raise_for_status
|
2024-01-23 21:44:48 +03:00
|
|
|
from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
|
2024-01-23 03:50:44 +03:00
|
|
|
|
2024-04-17 11:33:23 +03:00
|
|
|
API_URL = "https://www.perplexity.ai/socket.io/"
|
|
|
|
WS_URL = "wss://www.perplexity.ai/socket.io/"
|
2024-01-23 03:50:44 +03:00
|
|
|
|
2024-01-23 21:44:48 +03:00
|
|
|
class PerplexityLabs(AsyncGeneratorProvider, ProviderModelMixin):
|
2024-08-01 22:56:30 +03:00
|
|
|
url = "https://labs.perplexity.ai"
|
2024-01-23 03:50:44 +03:00
|
|
|
working = True
|
2024-08-25 11:00:06 +03:00
|
|
|
default_model = "llama-3.1-8b-instruct"
|
2024-01-23 21:44:48 +03:00
|
|
|
models = [
|
2024-08-01 22:56:30 +03:00
|
|
|
"llama-3.1-sonar-large-128k-online",
|
|
|
|
"llama-3.1-sonar-small-128k-online",
|
|
|
|
"llama-3.1-sonar-large-128k-chat",
|
|
|
|
"llama-3.1-sonar-small-128k-chat",
|
|
|
|
"llama-3.1-8b-instruct",
|
|
|
|
"llama-3.1-70b-instruct",
|
2024-01-23 21:44:48 +03:00
|
|
|
]
|
2024-01-23 03:50:44 +03:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
async def create_async_generator(
|
|
|
|
cls,
|
|
|
|
model: str,
|
|
|
|
messages: Messages,
|
|
|
|
proxy: str = None,
|
|
|
|
**kwargs
|
|
|
|
) -> AsyncResult:
|
|
|
|
headers = {
|
|
|
|
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:121.0) Gecko/20100101 Firefox/121.0",
|
|
|
|
"Accept": "*/*",
|
|
|
|
"Accept-Language": "de,en-US;q=0.7,en;q=0.3",
|
|
|
|
"Accept-Encoding": "gzip, deflate, br",
|
|
|
|
"Origin": cls.url,
|
|
|
|
"Connection": "keep-alive",
|
|
|
|
"Referer": f"{cls.url}/",
|
|
|
|
"Sec-Fetch-Dest": "empty",
|
|
|
|
"Sec-Fetch-Mode": "cors",
|
|
|
|
"Sec-Fetch-Site": "same-site",
|
|
|
|
"TE": "trailers",
|
|
|
|
}
|
2024-04-17 11:33:23 +03:00
|
|
|
async with StreamSession(headers=headers, proxies={"all": proxy}) as session:
|
2024-03-11 04:41:59 +03:00
|
|
|
t = format(random.getrandbits(32), "08x")
|
2024-01-23 03:50:44 +03:00
|
|
|
async with session.get(
|
2024-01-26 09:54:13 +03:00
|
|
|
f"{API_URL}?EIO=4&transport=polling&t={t}"
|
2024-01-23 03:50:44 +03:00
|
|
|
) as response:
|
2024-04-17 11:33:23 +03:00
|
|
|
await raise_for_status(response)
|
2024-01-23 03:50:44 +03:00
|
|
|
text = await response.text()
|
2024-04-17 11:33:23 +03:00
|
|
|
assert text.startswith("0")
|
2024-03-11 04:41:59 +03:00
|
|
|
sid = json.loads(text[1:])["sid"]
|
2024-01-23 03:50:44 +03:00
|
|
|
post_data = '40{"jwt":"anonymous-ask-user"}'
|
|
|
|
async with session.post(
|
2024-03-11 04:41:59 +03:00
|
|
|
f"{API_URL}?EIO=4&transport=polling&t={t}&sid={sid}",
|
2024-01-26 09:54:13 +03:00
|
|
|
data=post_data
|
2024-01-23 03:50:44 +03:00
|
|
|
) as response:
|
2024-04-17 11:33:23 +03:00
|
|
|
await raise_for_status(response)
|
2024-08-01 22:56:30 +03:00
|
|
|
assert await response.text() == "OK"
|
2024-03-11 04:41:59 +03:00
|
|
|
async with session.ws_connect(f"{WS_URL}?EIO=4&transport=websocket&sid={sid}", autoping=False) as ws:
|
|
|
|
await ws.send_str("2probe")
|
|
|
|
assert(await ws.receive_str() == "3probe")
|
|
|
|
await ws.send_str("5")
|
2024-01-23 03:50:44 +03:00
|
|
|
assert(await ws.receive_str())
|
2024-03-11 04:41:59 +03:00
|
|
|
assert(await ws.receive_str() == "6")
|
2024-01-23 03:50:44 +03:00
|
|
|
message_data = {
|
2024-03-11 04:41:59 +03:00
|
|
|
"version": "2.5",
|
|
|
|
"source": "default",
|
|
|
|
"model": cls.get_model(model),
|
|
|
|
"messages": messages
|
2024-01-23 03:50:44 +03:00
|
|
|
}
|
2024-03-11 04:41:59 +03:00
|
|
|
await ws.send_str("42" + json.dumps(["perplexity_labs", message_data]))
|
2024-01-23 03:50:44 +03:00
|
|
|
last_message = 0
|
|
|
|
while True:
|
|
|
|
message = await ws.receive_str()
|
2024-03-11 04:41:59 +03:00
|
|
|
if message == "2":
|
|
|
|
if last_message == 0:
|
|
|
|
raise RuntimeError("Unknown error")
|
|
|
|
await ws.send_str("3")
|
2024-01-23 03:50:44 +03:00
|
|
|
continue
|
|
|
|
try:
|
|
|
|
data = json.loads(message[2:])[1]
|
|
|
|
yield data["output"][last_message:]
|
|
|
|
last_message = len(data["output"])
|
|
|
|
if data["final"]:
|
|
|
|
break
|
|
|
|
except:
|
2024-04-19 15:10:56 +03:00
|
|
|
raise RuntimeError(f"Message: {message}")
|