2023-11-16 15:56:23 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import time
|
2023-12-06 08:35:36 +00:00
|
|
|
from selenium.webdriver.common.by import By
|
|
|
|
from selenium.webdriver.support.ui import WebDriverWait
|
|
|
|
from selenium.webdriver.support import expected_conditions as EC
|
|
|
|
from selenium.webdriver.common.keys import Keys
|
2023-11-16 15:56:23 +00:00
|
|
|
|
|
|
|
from ..typing import CreateResult, Messages
|
2024-01-01 16:48:57 +00:00
|
|
|
from .base_provider import AbstractProvider
|
2023-11-20 13:00:40 +00:00
|
|
|
from .helper import format_prompt
|
2023-12-02 04:40:07 +00:00
|
|
|
from ..webdriver import WebDriver, WebDriverSession
|
2023-11-16 15:56:23 +00:00
|
|
|
|
2024-01-01 16:48:57 +00:00
|
|
|
class PerplexityAi(AbstractProvider):
|
2023-11-16 15:56:23 +00:00
|
|
|
url = "https://www.perplexity.ai"
|
|
|
|
working = True
|
2023-11-16 17:10:19 +00:00
|
|
|
supports_gpt_35_turbo = True
|
2023-11-16 15:56:23 +00:00
|
|
|
supports_stream = True
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def create_completion(
|
|
|
|
cls,
|
|
|
|
model: str,
|
|
|
|
messages: Messages,
|
|
|
|
stream: bool,
|
|
|
|
proxy: str = None,
|
|
|
|
timeout: int = 120,
|
2023-11-20 13:00:40 +00:00
|
|
|
webdriver: WebDriver = None,
|
2023-11-19 04:36:04 +00:00
|
|
|
virtual_display: bool = True,
|
2023-11-16 15:56:23 +00:00
|
|
|
copilot: bool = False,
|
|
|
|
**kwargs
|
|
|
|
) -> CreateResult:
|
2023-11-20 13:00:40 +00:00
|
|
|
with WebDriverSession(webdriver, "", virtual_display=virtual_display, proxy=proxy) as driver:
|
2023-11-19 04:36:04 +00:00
|
|
|
prompt = format_prompt(messages)
|
2023-11-16 17:10:19 +00:00
|
|
|
|
2023-11-19 04:36:04 +00:00
|
|
|
driver.get(f"{cls.url}/")
|
|
|
|
wait = WebDriverWait(driver, timeout)
|
2023-11-16 15:56:23 +00:00
|
|
|
|
2023-11-19 04:36:04 +00:00
|
|
|
# Is page loaded?
|
|
|
|
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "textarea[placeholder='Ask anything...']")))
|
2023-11-16 15:56:23 +00:00
|
|
|
|
2023-11-19 04:36:04 +00:00
|
|
|
# Register WebSocket hook
|
|
|
|
script = """
|
2023-11-16 15:56:23 +00:00
|
|
|
window._message = window._last_message = "";
|
|
|
|
window._message_finished = false;
|
|
|
|
const _socket_send = WebSocket.prototype.send;
|
|
|
|
WebSocket.prototype.send = function(...args) {
|
|
|
|
if (!window.socket_onmessage) {
|
|
|
|
window._socket_onmessage = this;
|
|
|
|
this.addEventListener("message", (event) => {
|
|
|
|
if (event.data.startsWith("42")) {
|
|
|
|
let data = JSON.parse(event.data.substring(2));
|
|
|
|
if (data[0] =="query_progress" || data[0] == "query_answered") {
|
|
|
|
let content = JSON.parse(data[1]["text"]);
|
|
|
|
if (data[1]["mode"] == "copilot") {
|
|
|
|
content = content[content.length-1]["content"]["answer"];
|
|
|
|
content = JSON.parse(content);
|
|
|
|
}
|
|
|
|
window._message = content["answer"];
|
2023-11-17 10:16:49 +00:00
|
|
|
if (!window._message_finished) {
|
|
|
|
window._message_finished = data[0] == "query_answered";
|
|
|
|
}
|
2023-11-16 15:56:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return _socket_send.call(this, ...args);
|
|
|
|
};
|
|
|
|
"""
|
2023-11-19 04:36:04 +00:00
|
|
|
driver.execute_script(script)
|
2023-11-16 15:56:23 +00:00
|
|
|
|
2023-11-19 04:36:04 +00:00
|
|
|
if copilot:
|
|
|
|
try:
|
|
|
|
# Check for account
|
|
|
|
driver.find_element(By.CSS_SELECTOR, "img[alt='User avatar']")
|
|
|
|
# Enable copilot
|
|
|
|
driver.find_element(By.CSS_SELECTOR, "button[data-testid='copilot-toggle']").click()
|
|
|
|
except:
|
|
|
|
raise RuntimeError("You need a account for copilot")
|
2023-11-16 15:56:23 +00:00
|
|
|
|
2023-11-19 04:36:04 +00:00
|
|
|
# Submit prompt
|
|
|
|
driver.find_element(By.CSS_SELECTOR, "textarea[placeholder='Ask anything...']").send_keys(prompt)
|
|
|
|
driver.find_element(By.CSS_SELECTOR, "textarea[placeholder='Ask anything...']").send_keys(Keys.ENTER)
|
2023-11-16 15:56:23 +00:00
|
|
|
|
2023-11-17 10:16:49 +00:00
|
|
|
# Stream response
|
2023-11-16 15:56:23 +00:00
|
|
|
script = """
|
|
|
|
if(window._message && window._message != window._last_message) {
|
|
|
|
try {
|
|
|
|
return window._message.substring(window._last_message.length);
|
|
|
|
} finally {
|
|
|
|
window._last_message = window._message;
|
|
|
|
}
|
|
|
|
} else if(window._message_finished) {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
while True:
|
|
|
|
chunk = driver.execute_script(script)
|
|
|
|
if chunk:
|
|
|
|
yield chunk
|
|
|
|
elif chunk != "":
|
|
|
|
break
|
|
|
|
else:
|
2023-11-19 04:36:04 +00:00
|
|
|
time.sleep(0.1)
|