2023-11-17 02:19:27 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import time
|
|
|
|
|
|
|
|
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
|
2024-01-26 11:49:52 +00:00
|
|
|
from ...webdriver import WebDriver, WebDriverSession, element_send_text
|
2023-11-17 02:19:27 +00:00
|
|
|
|
|
|
|
models = {
|
|
|
|
"meta-llama/Llama-2-7b-chat-hf": {"name": "Llama-2-7b"},
|
|
|
|
"meta-llama/Llama-2-13b-chat-hf": {"name": "Llama-2-13b"},
|
|
|
|
"meta-llama/Llama-2-70b-chat-hf": {"name": "Llama-2-70b"},
|
|
|
|
"codellama/CodeLlama-7b-Instruct-hf": {"name": "Code-Llama-7b"},
|
|
|
|
"codellama/CodeLlama-13b-Instruct-hf": {"name": "Code-Llama-13b"},
|
|
|
|
"codellama/CodeLlama-34b-Instruct-hf": {"name": "Code-Llama-34b"},
|
|
|
|
"gpt-3.5-turbo": {"name": "GPT-3.5-Turbo"},
|
|
|
|
"gpt-3.5-turbo-instruct": {"name": "GPT-3.5-Turbo-Instruct"},
|
|
|
|
"gpt-4": {"name": "GPT-4"},
|
|
|
|
"palm": {"name": "Google-PaLM"},
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:48:57 +00:00
|
|
|
class Poe(AbstractProvider):
|
2023-11-17 02:19:27 +00:00
|
|
|
url = "https://poe.com"
|
|
|
|
working = True
|
2023-11-17 03:50:37 +00:00
|
|
|
needs_auth = True
|
2023-11-17 02:19:27 +00:00
|
|
|
supports_gpt_35_turbo = True
|
|
|
|
supports_stream = True
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def create_completion(
|
|
|
|
cls,
|
|
|
|
model: str,
|
|
|
|
messages: Messages,
|
|
|
|
stream: bool,
|
|
|
|
proxy: str = None,
|
2023-11-20 13:00:40 +00:00
|
|
|
webdriver: WebDriver = None,
|
2023-11-17 03:50:37 +00:00
|
|
|
user_data_dir: str = None,
|
|
|
|
headless: bool = True,
|
2023-11-17 02:19:27 +00:00
|
|
|
**kwargs
|
|
|
|
) -> CreateResult:
|
|
|
|
if not model:
|
|
|
|
model = "gpt-3.5-turbo"
|
|
|
|
elif model not in models:
|
|
|
|
raise ValueError(f"Model are not supported: {model}")
|
|
|
|
prompt = format_prompt(messages)
|
|
|
|
|
2023-11-20 13:00:40 +00:00
|
|
|
session = WebDriverSession(webdriver, user_data_dir, headless, proxy=proxy)
|
2023-11-19 04:36:04 +00:00
|
|
|
with session as driver:
|
|
|
|
from selenium.webdriver.common.by import By
|
|
|
|
from selenium.webdriver.support.ui import WebDriverWait
|
|
|
|
from selenium.webdriver.support import expected_conditions as EC
|
|
|
|
|
|
|
|
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
|
|
|
|
"source": """
|
|
|
|
window._message = window._last_message = "";
|
|
|
|
window._message_finished = false;
|
|
|
|
class ProxiedWebSocket extends WebSocket {
|
|
|
|
constructor(url, options) {
|
|
|
|
super(url, options);
|
|
|
|
this.addEventListener("message", (e) => {
|
|
|
|
const data = JSON.parse(JSON.parse(e.data)["messages"][0])["payload"]["data"];
|
|
|
|
if ("messageAdded" in data) {
|
|
|
|
if (data["messageAdded"]["author"] != "human") {
|
|
|
|
window._message = data["messageAdded"]["text"];
|
|
|
|
if (data["messageAdded"]["state"] == "complete") {
|
|
|
|
window._message_finished = true;
|
|
|
|
}
|
2023-11-17 02:19:27 +00:00
|
|
|
}
|
|
|
|
}
|
2023-11-19 04:36:04 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
window.WebSocket = ProxiedWebSocket;
|
|
|
|
"""
|
|
|
|
})
|
2023-11-17 02:19:27 +00:00
|
|
|
|
2023-11-19 04:36:04 +00:00
|
|
|
try:
|
2023-11-17 02:19:27 +00:00
|
|
|
driver.get(f"{cls.url}/{models[model]['name']}")
|
2023-11-19 04:36:04 +00:00
|
|
|
wait = WebDriverWait(driver, 10 if headless else 240)
|
2023-11-17 02:19:27 +00:00
|
|
|
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "textarea[class^='GrowingTextArea']")))
|
2023-11-19 04:36:04 +00:00
|
|
|
except:
|
|
|
|
# Reopen browser for login
|
2023-11-20 13:00:40 +00:00
|
|
|
if not webdriver:
|
|
|
|
driver = session.reopen()
|
2023-11-19 04:36:04 +00:00
|
|
|
driver.get(f"{cls.url}/{models[model]['name']}")
|
|
|
|
wait = WebDriverWait(driver, 240)
|
|
|
|
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "textarea[class^='GrowingTextArea']")))
|
|
|
|
else:
|
|
|
|
raise RuntimeError("Prompt textarea not found. You may not be logged in.")
|
2023-11-17 02:19:27 +00:00
|
|
|
|
2024-01-26 11:49:52 +00:00
|
|
|
element_send_text(driver.find_element(By.CSS_SELECTOR, "footer textarea[class^='GrowingTextArea']"), prompt)
|
2023-11-19 04:36:04 +00:00
|
|
|
driver.find_element(By.CSS_SELECTOR, "footer button[class*='ChatMessageSendButton']").click()
|
2023-11-17 02:19:27 +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)
|