2023-09-25 22:52:29 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-11-17 02:19:27 +00:00
|
|
|
import time
|
|
|
|
import random
|
2023-09-25 22:52:29 +00:00
|
|
|
|
2024-01-22 02:38:11 +00:00
|
|
|
from ...typing import CreateResult, Messages
|
|
|
|
from ..base_provider import AbstractProvider
|
|
|
|
from ..helper import format_prompt, get_random_string
|
2024-01-26 11:49:52 +00:00
|
|
|
from ...webdriver import WebDriver, WebDriverSession, element_send_text
|
2024-01-22 02:38:11 +00:00
|
|
|
from ... import debug
|
2023-09-25 22:52:29 +00:00
|
|
|
|
2024-01-01 16:48:57 +00:00
|
|
|
class AItianhuSpace(AbstractProvider):
|
2023-09-25 22:52:29 +00:00
|
|
|
url = "https://chat3.aiyunos.top/"
|
|
|
|
working = True
|
2023-11-17 10:16:49 +00:00
|
|
|
supports_stream = True
|
2023-09-25 22:52:29 +00:00
|
|
|
supports_gpt_35_turbo = True
|
2023-11-17 02:19:27 +00:00
|
|
|
_domains = ["aitianhu.com", "aitianhu1.top"]
|
2023-09-25 22:52:29 +00:00
|
|
|
|
|
|
|
@classmethod
|
2023-11-17 02:19:27 +00:00
|
|
|
def create_completion(
|
|
|
|
cls,
|
|
|
|
model: str,
|
|
|
|
messages: Messages,
|
|
|
|
stream: bool,
|
|
|
|
domain: str = None,
|
|
|
|
proxy: str = None,
|
|
|
|
timeout: int = 120,
|
2023-11-20 13:00:40 +00:00
|
|
|
webdriver: WebDriver = None,
|
2023-11-17 03:50:37 +00:00
|
|
|
headless: bool = True,
|
2023-11-17 02:19:27 +00:00
|
|
|
**kwargs
|
|
|
|
) -> CreateResult:
|
2023-09-25 22:52:29 +00:00
|
|
|
if not model:
|
|
|
|
model = "gpt-3.5-turbo"
|
2023-10-05 03:13:37 +00:00
|
|
|
if not domain:
|
2023-11-18 03:38:31 +00:00
|
|
|
rand = get_random_string(6)
|
2023-11-17 02:19:27 +00:00
|
|
|
domain = random.choice(cls._domains)
|
|
|
|
domain = f"{rand}.{domain}"
|
2023-10-22 21:53:18 +00:00
|
|
|
if debug.logging:
|
2023-10-20 23:52:19 +00:00
|
|
|
print(f"AItianhuSpace | using domain: {domain}")
|
2023-11-17 02:19:27 +00:00
|
|
|
url = f"https://{domain}"
|
|
|
|
prompt = format_prompt(messages)
|
2023-10-23 07:46:25 +00:00
|
|
|
|
2023-11-20 13:00:40 +00:00
|
|
|
with WebDriverSession(webdriver, "", headless=headless, proxy=proxy) as driver:
|
2023-11-19 04:36:04 +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-10-23 07:46:25 +00:00
|
|
|
|
2023-11-19 04:36:04 +00:00
|
|
|
wait = WebDriverWait(driver, timeout)
|
2023-09-25 22:52:29 +00:00
|
|
|
|
2023-11-19 04:36:04 +00:00
|
|
|
# Bypass devtools detection
|
|
|
|
driver.get("https://blank.page/")
|
|
|
|
wait.until(EC.visibility_of_element_located((By.ID, "sheet")))
|
|
|
|
driver.execute_script(f"""
|
|
|
|
document.getElementById('sheet').addEventListener('click', () => {{
|
2024-01-27 01:00:44 +00:00
|
|
|
window.open(arguments[0]);
|
2023-11-19 04:36:04 +00:00
|
|
|
}});
|
2024-01-27 01:00:44 +00:00
|
|
|
""", url)
|
2023-11-19 04:36:04 +00:00
|
|
|
driver.find_element(By.ID, "sheet").click()
|
|
|
|
time.sleep(10)
|
2023-11-17 02:19:27 +00:00
|
|
|
|
2023-11-19 04:36:04 +00:00
|
|
|
original_window = driver.current_window_handle
|
|
|
|
for window_handle in driver.window_handles:
|
|
|
|
if window_handle != original_window:
|
|
|
|
driver.close()
|
|
|
|
driver.switch_to.window(window_handle)
|
|
|
|
break
|
2023-11-17 02:19:27 +00:00
|
|
|
|
2023-11-19 04:36:04 +00:00
|
|
|
# Wait for page load
|
|
|
|
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "textarea.n-input__textarea-el")))
|
2023-11-17 02:19:27 +00:00
|
|
|
|
2023-11-18 03:38:31 +00:00
|
|
|
# Register hook in XMLHttpRequest
|
2023-11-17 02:19:27 +00:00
|
|
|
script = """
|
|
|
|
const _http_request_open = XMLHttpRequest.prototype.open;
|
|
|
|
window._last_message = window._message = "";
|
|
|
|
window._loadend = false;
|
|
|
|
XMLHttpRequest.prototype.open = function(method, url) {
|
|
|
|
if (url == "/api/chat-process") {
|
|
|
|
this.addEventListener("progress", (event) => {
|
|
|
|
const lines = this.responseText.split("\\n");
|
|
|
|
try {
|
|
|
|
window._message = JSON.parse(lines[lines.length-1])["text"];
|
|
|
|
} catch(e) { }
|
|
|
|
});
|
|
|
|
this.addEventListener("loadend", (event) => {
|
|
|
|
window._loadend = true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return _http_request_open.call(this, method, url);
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
driver.execute_script(script)
|
|
|
|
|
2023-11-18 03:38:31 +00:00
|
|
|
# Submit prompt
|
2024-01-26 11:49:52 +00:00
|
|
|
element_send_text(driver.find_element(By.CSS_SELECTOR, "textarea.n-input__textarea-el"), prompt)
|
2023-11-17 02:19:27 +00:00
|
|
|
|
2023-11-18 03:38:31 +00:00
|
|
|
# Read response
|
2023-11-17 02:19:27 +00:00
|
|
|
while True:
|
|
|
|
chunk = driver.execute_script("""
|
|
|
|
if (window._message && window._message != window._last_message) {
|
|
|
|
try {
|
|
|
|
return window._message.substring(window._last_message.length);
|
|
|
|
} finally {
|
|
|
|
window._last_message = window._message;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (window._loadend) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
""")
|
|
|
|
if chunk:
|
|
|
|
yield chunk
|
|
|
|
elif chunk != "":
|
|
|
|
break
|
|
|
|
else:
|
2023-11-19 04:36:04 +00:00
|
|
|
time.sleep(0.1)
|