2023-10-02 15:01:15 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-11-16 15:56:23 +00:00
|
|
|
import time
|
|
|
|
from urllib.parse import quote
|
2023-10-02 15:01:15 +00:00
|
|
|
|
2023-11-16 15:56:23 +00:00
|
|
|
from ..typing import CreateResult, Messages
|
|
|
|
from .base_provider import BaseProvider
|
2023-11-19 04:36:04 +00:00
|
|
|
from .helper import WebDriver, WebDriverSession, format_prompt
|
2023-10-02 15:01:15 +00:00
|
|
|
|
2023-11-16 15:56:23 +00:00
|
|
|
class Phind(BaseProvider):
|
2023-10-02 15:01:15 +00:00
|
|
|
url = "https://www.phind.com"
|
|
|
|
working = True
|
|
|
|
supports_gpt_4 = True
|
2023-11-16 15:56:23 +00:00
|
|
|
supports_stream = True
|
2023-10-02 15:01:15 +00:00
|
|
|
|
|
|
|
@classmethod
|
2023-11-16 15:56:23 +00:00
|
|
|
def create_completion(
|
2023-10-02 15:01:15 +00:00
|
|
|
cls,
|
|
|
|
model: str,
|
2023-10-10 07:49:29 +00:00
|
|
|
messages: Messages,
|
2023-11-16 15:56:23 +00:00
|
|
|
stream: bool,
|
2023-10-02 15:01:15 +00:00
|
|
|
proxy: str = None,
|
2023-10-09 08:22:17 +00:00
|
|
|
timeout: int = 120,
|
2023-11-19 04:36:04 +00:00
|
|
|
web_driver: WebDriver = None,
|
2023-11-16 15:56:23 +00:00
|
|
|
creative_mode: bool = None,
|
2023-10-02 15:01:15 +00:00
|
|
|
**kwargs
|
2023-11-16 15:56:23 +00:00
|
|
|
) -> CreateResult:
|
2023-11-19 04:36:04 +00:00
|
|
|
with WebDriverSession(web_driver, "", proxy=proxy) as driver:
|
2023-11-18 03:38:31 +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
|
2023-11-16 15:56:23 +00:00
|
|
|
|
2023-11-18 03:38:31 +00:00
|
|
|
prompt = quote(format_prompt(messages))
|
|
|
|
driver.get(f"{cls.url}/search?q={prompt}&source=searchbox")
|
2023-11-16 15:56:23 +00:00
|
|
|
|
2023-11-18 03:38:31 +00:00
|
|
|
# Register fetch hook
|
|
|
|
driver.execute_script("""
|
2023-11-16 15:56:23 +00:00
|
|
|
window._fetch = window.fetch;
|
|
|
|
window.fetch = (url, options) => {
|
2023-11-16 18:24:15 +00:00
|
|
|
// Call parent fetch method
|
2023-11-16 15:56:23 +00:00
|
|
|
const result = window._fetch(url, options);
|
2023-11-18 03:38:31 +00:00
|
|
|
if (url != "/api/infer/answer") {
|
|
|
|
return result;
|
|
|
|
}
|
2023-11-16 18:24:15 +00:00
|
|
|
// Load response reader
|
2023-11-16 15:56:23 +00:00
|
|
|
result.then((response) => {
|
|
|
|
if (!response.body.locked) {
|
2023-11-18 03:38:31 +00:00
|
|
|
window._reader = response.body.getReader();
|
2023-11-16 15:56:23 +00:00
|
|
|
}
|
|
|
|
});
|
2023-11-16 18:24:15 +00:00
|
|
|
// Return dummy response
|
2023-11-16 15:56:23 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
resolve(new Response(new ReadableStream()))
|
|
|
|
});
|
|
|
|
}
|
2023-11-18 03:38:31 +00:00
|
|
|
""")
|
|
|
|
|
|
|
|
# Need to change settings
|
|
|
|
if model.startswith("gpt-4") or creative_mode:
|
|
|
|
wait = WebDriverWait(driver, timeout)
|
|
|
|
# Open settings dropdown
|
|
|
|
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button.text-dark.dropdown-toggle")))
|
|
|
|
driver.find_element(By.CSS_SELECTOR, "button.text-dark.dropdown-toggle").click()
|
|
|
|
# Wait for dropdown toggle
|
|
|
|
wait.until(EC.visibility_of_element_located((By.XPATH, "//button[text()='GPT-4']")))
|
|
|
|
# Enable GPT-4
|
|
|
|
if model.startswith("gpt-4"):
|
|
|
|
driver.find_element(By.XPATH, "//button[text()='GPT-4']").click()
|
|
|
|
# Enable creative mode
|
|
|
|
if creative_mode or creative_mode == None:
|
|
|
|
driver.find_element(By.ID, "Creative Mode").click()
|
|
|
|
# Submit changes
|
|
|
|
driver.find_element(By.CSS_SELECTOR, ".search-bar-input-group button[type='submit']").click()
|
|
|
|
# Wait for page reload
|
|
|
|
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".search-container")))
|
|
|
|
|
|
|
|
while True:
|
|
|
|
chunk = driver.execute_script("""
|
|
|
|
if(window._reader) {
|
|
|
|
chunk = await window._reader.read();
|
2023-11-16 15:56:23 +00:00
|
|
|
if (chunk['done']) return null;
|
2023-11-16 18:02:53 +00:00
|
|
|
text = (new TextDecoder()).decode(chunk['value']);
|
2023-11-16 15:56:23 +00:00
|
|
|
content = '';
|
|
|
|
text.split('\\r\\n').forEach((line, index) => {
|
|
|
|
if (line.startsWith('data: ')) {
|
|
|
|
line = line.substring('data: '.length);
|
|
|
|
if (!line.startsWith('<PHIND_METADATA>')) {
|
|
|
|
if (line) content += line;
|
|
|
|
else content += '\\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return content.replace('\\n\\n', '\\n');
|
|
|
|
} else {
|
|
|
|
return ''
|
|
|
|
}
|
2023-11-18 03:38:31 +00:00
|
|
|
""")
|
2023-11-16 15:56:23 +00:00
|
|
|
if chunk:
|
|
|
|
yield chunk
|
|
|
|
elif chunk != "":
|
|
|
|
break
|
|
|
|
else:
|
2023-11-19 04:36:04 +00:00
|
|
|
time.sleep(0.1)
|