2024-01-23 00:50:44 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import random
|
|
|
|
import json
|
2024-01-23 22:48:11 +00:00
|
|
|
from aiohttp import ClientSession, BaseConnector
|
2024-01-23 00:50:44 +00:00
|
|
|
|
|
|
|
from ..typing import AsyncResult, Messages
|
2024-01-23 18:44:48 +00:00
|
|
|
from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
|
2024-01-23 23:46:35 +00:00
|
|
|
from .helper import get_connector
|
2024-01-23 00:50:44 +00:00
|
|
|
|
|
|
|
API_URL = "https://labs-api.perplexity.ai/socket.io/"
|
|
|
|
WS_URL = "wss://labs-api.perplexity.ai/socket.io/"
|
|
|
|
|
2024-01-23 18:44:48 +00:00
|
|
|
class PerplexityLabs(AsyncGeneratorProvider, ProviderModelMixin):
|
2024-01-23 00:50:44 +00:00
|
|
|
url = "https://labs.perplexity.ai"
|
|
|
|
working = True
|
2024-03-12 01:06:06 +00:00
|
|
|
default_model = "mixtral-8x7b-instruct"
|
2024-01-23 18:44:48 +00:00
|
|
|
models = [
|
2024-03-11 01:41:59 +00:00
|
|
|
"sonar-small-online", "sonar-medium-online", "sonar-small-chat", "sonar-medium-chat", "mistral-7b-instruct",
|
|
|
|
"codellama-70b-instruct", "llava-v1.5-7b-wrapper", "llava-v1.6-34b", "mixtral-8x7b-instruct",
|
|
|
|
"gemma-2b-it", "gemma-7b-it"
|
|
|
|
"mistral-medium", "related"
|
2024-01-23 18:44:48 +00:00
|
|
|
]
|
|
|
|
model_aliases = {
|
2024-01-23 04:02:14 +00:00
|
|
|
"mistralai/Mistral-7B-Instruct-v0.1": "mistral-7b-instruct",
|
|
|
|
"mistralai/Mixtral-8x7B-Instruct-v0.1": "mixtral-8x7b-instruct",
|
2024-03-11 01:41:59 +00:00
|
|
|
"codellama/CodeLlama-70b-Instruct-hf": "codellama-70b-instruct",
|
|
|
|
"llava-v1.5-7b": "llava-v1.5-7b-wrapper"
|
2024-01-23 04:02:14 +00:00
|
|
|
}
|
2024-01-23 00:50:44 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
async def create_async_generator(
|
|
|
|
cls,
|
|
|
|
model: str,
|
|
|
|
messages: Messages,
|
|
|
|
proxy: str = None,
|
2024-01-23 22:48:11 +00:00
|
|
|
connector: BaseConnector = None,
|
2024-01-23 00:50:44 +00:00
|
|
|
**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-01-23 23:46:35 +00:00
|
|
|
async with ClientSession(headers=headers, connector=get_connector(connector, proxy)) as session:
|
2024-03-11 01:41:59 +00:00
|
|
|
t = format(random.getrandbits(32), "08x")
|
2024-01-23 00:50:44 +00:00
|
|
|
async with session.get(
|
2024-01-26 06:54:13 +00:00
|
|
|
f"{API_URL}?EIO=4&transport=polling&t={t}"
|
2024-01-23 00:50:44 +00:00
|
|
|
) as response:
|
|
|
|
text = await response.text()
|
|
|
|
|
2024-03-11 01:41:59 +00:00
|
|
|
sid = json.loads(text[1:])["sid"]
|
2024-01-23 00:50:44 +00:00
|
|
|
post_data = '40{"jwt":"anonymous-ask-user"}'
|
|
|
|
async with session.post(
|
2024-03-11 01:41:59 +00:00
|
|
|
f"{API_URL}?EIO=4&transport=polling&t={t}&sid={sid}",
|
2024-01-26 06:54:13 +00:00
|
|
|
data=post_data
|
2024-01-23 00:50:44 +00:00
|
|
|
) as response:
|
2024-03-11 01:41:59 +00:00
|
|
|
assert await response.text() == "OK"
|
2024-01-23 00:50:44 +00:00
|
|
|
|
2024-03-11 01:41:59 +00: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 00:50:44 +00:00
|
|
|
assert(await ws.receive_str())
|
2024-03-11 01:41:59 +00:00
|
|
|
assert(await ws.receive_str() == "6")
|
2024-01-23 00:50:44 +00:00
|
|
|
message_data = {
|
2024-03-11 01:41:59 +00:00
|
|
|
"version": "2.5",
|
|
|
|
"source": "default",
|
|
|
|
"model": cls.get_model(model),
|
|
|
|
"messages": messages
|
2024-01-23 00:50:44 +00:00
|
|
|
}
|
2024-03-11 01:41:59 +00:00
|
|
|
await ws.send_str("42" + json.dumps(["perplexity_labs", message_data]))
|
2024-01-23 00:50:44 +00:00
|
|
|
last_message = 0
|
|
|
|
while True:
|
|
|
|
message = await ws.receive_str()
|
2024-03-11 01:41:59 +00:00
|
|
|
if message == "2":
|
|
|
|
if last_message == 0:
|
|
|
|
raise RuntimeError("Unknown error")
|
|
|
|
await ws.send_str("3")
|
2024-01-23 00:50:44 +00:00
|
|
|
continue
|
|
|
|
try:
|
|
|
|
data = json.loads(message[2:])[1]
|
|
|
|
yield data["output"][last_message:]
|
|
|
|
last_message = len(data["output"])
|
|
|
|
if data["final"]:
|
|
|
|
break
|
|
|
|
except:
|
|
|
|
raise RuntimeError(f"Message: {message}")
|