summaryrefslogtreecommitdiffstats
path: root/g4f/Provider/PerplexityAi.py
blob: 8bf83b6ab1f68065771ecef119961a81e1dc832d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
from __future__ import annotations

import time
try:
    from selenium.webdriver.remote.webdriver import WebDriver
except ImportError:
    class WebDriver():
        pass

from ..typing import CreateResult, Messages
from .base_provider import BaseProvider
from .helper import format_prompt, get_browser

class PerplexityAi(BaseProvider):
    url = "https://www.perplexity.ai"
    working = True
    supports_gpt_4 = True
    supports_stream = True

    @classmethod
    def create_completion(
        cls,
        model: str,
        messages: Messages,
        stream: bool,
        proxy: str = None,
        timeout: int = 120,
        browser: WebDriver = None,
        copilot: bool = False,
        display: bool = True,
        **kwargs
    ) -> CreateResult:
        from selenium.webdriver.common.by import By
        from selenium.webdriver.support.ui import WebDriverWait
        from selenium.webdriver.support import expected_conditions as EC

        if browser:
            driver = browser
        else:
            if display:
                driver, display = get_browser("", True, proxy)
            else:
                driver = get_browser("", False, proxy)

        prompt = format_prompt(messages)

        driver.get(f"{cls.url}/")
        wait = WebDriverWait(driver, timeout)

        script = """
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"];
                    window._message_finished = data[0] == "query_answered";
                    window._web_results = content["web_results"];
                }
            }
        });
    }
    return _socket_send.call(this, ...args);
};
"""
        driver.execute_script(script)

        # Page loaded?
        wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "textarea[placeholder='Ask anything...']")))

        if copilot:
            try:
                driver.find_element(By.CSS_SELECTOR, "img[alt='User avatar']")
                driver.find_element(By.CSS_SELECTOR, "button[data-testid='copilot-toggle']").click()
            except:
                pass

        # Enter question
        driver.find_element(By.CSS_SELECTOR, "textarea[placeholder='Ask anything...']").send_keys(prompt)
        # Submit question
        driver.find_element(By.CSS_SELECTOR, "button.bg-super svg[data-icon='arrow-right']").click()

        try:
            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:
                    time.sleep(0.1)
        finally:
            driver.close()
            if not browser:
                time.sleep(0.1)
                driver.quit()
            if display:
                display.stop()