2023-11-18 06:38:31 +03:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import time, json, time
|
|
|
|
|
2024-01-22 05:38:11 +03:00
|
|
|
from ...typing import CreateResult, Messages
|
|
|
|
from ..base_provider import AbstractProvider
|
|
|
|
from ...webdriver import WebDriver, WebDriverSession
|
2023-11-18 06:38:31 +03:00
|
|
|
|
2024-01-01 19:48:57 +03:00
|
|
|
class TalkAi(AbstractProvider):
|
2023-11-18 06:38:31 +03:00
|
|
|
url = "https://talkai.info"
|
|
|
|
working = True
|
|
|
|
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 16:00:40 +03:00
|
|
|
webdriver: WebDriver = None,
|
2023-11-18 06:38:31 +03:00
|
|
|
**kwargs
|
|
|
|
) -> CreateResult:
|
2023-11-20 16:00:40 +03:00
|
|
|
with WebDriverSession(webdriver, "", virtual_display=True, proxy=proxy) as driver:
|
2023-11-19 07:36:04 +03: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-18 06:38:31 +03:00
|
|
|
|
|
|
|
driver.get(f"{cls.url}/chat/")
|
|
|
|
|
|
|
|
# Wait for page load
|
|
|
|
WebDriverWait(driver, 240).until(
|
|
|
|
EC.presence_of_element_located((By.CSS_SELECTOR, "body.chat-page"))
|
|
|
|
)
|
|
|
|
|
|
|
|
data = {
|
|
|
|
"type": "chat",
|
|
|
|
"message": messages[-1]["content"],
|
|
|
|
"messagesHistory": [{
|
|
|
|
"from": "you" if message["role"] == "user" else "chatGPT",
|
|
|
|
"content": message["content"]
|
|
|
|
} for message in messages],
|
|
|
|
"model": model if model else "gpt-3.5-turbo",
|
2024-01-26 14:49:52 +03:00
|
|
|
"max_tokens": 2048,
|
2023-11-18 06:38:31 +03:00
|
|
|
"temperature": 1,
|
|
|
|
"top_p": 1,
|
|
|
|
"presence_penalty": 0,
|
|
|
|
"frequency_penalty": 0,
|
|
|
|
**kwargs
|
|
|
|
}
|
|
|
|
script = """
|
|
|
|
const response = await fetch("/chat/send2/", {
|
|
|
|
"headers": {
|
|
|
|
"Accept": "application/json",
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
"body": {body},
|
|
|
|
"method": "POST"
|
|
|
|
});
|
|
|
|
window._reader = response.body.pipeThrough(new TextDecoderStream()).getReader();
|
|
|
|
"""
|
|
|
|
driver.execute_script(
|
|
|
|
script.replace("{body}", json.dumps(json.dumps(data)))
|
|
|
|
)
|
|
|
|
# Read response
|
|
|
|
while True:
|
|
|
|
chunk = driver.execute_script("""
|
|
|
|
chunk = await window._reader.read();
|
2024-01-26 14:49:52 +03:00
|
|
|
if (chunk.done) {
|
2023-11-18 06:38:31 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
content = "";
|
2024-01-26 14:49:52 +03:00
|
|
|
for (line of chunk.value.split("\\n")) {
|
2023-11-18 06:38:31 +03:00
|
|
|
if (line.startsWith('data: ')) {
|
|
|
|
content += line.substring('data: '.length);
|
|
|
|
}
|
2024-01-26 14:49:52 +03:00
|
|
|
}
|
2023-11-18 06:38:31 +03:00
|
|
|
return content;
|
|
|
|
""")
|
|
|
|
if chunk:
|
|
|
|
yield chunk.replace("\\n", "\n")
|
|
|
|
elif chunk != "":
|
|
|
|
break
|
|
|
|
else:
|
2023-11-19 07:36:04 +03:00
|
|
|
time.sleep(0.1)
|