2023-10-22 02:57:31 -04:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-11-16 10:56:23 -05:00
|
|
|
import time, json
|
2023-10-22 02:57:31 -04:00
|
|
|
|
2023-11-16 10:56:23 -05:00
|
|
|
from ..typing import CreateResult, Messages
|
|
|
|
from .base_provider import BaseProvider
|
2023-11-16 12:51:24 -05:00
|
|
|
from .helper import WebDriver, format_prompt, get_browser
|
2023-11-16 10:56:23 -05:00
|
|
|
|
|
|
|
class MyShell(BaseProvider):
|
2023-10-22 03:04:14 -04:00
|
|
|
url = "https://app.myshell.ai/chat"
|
2023-10-24 12:58:12 -04:00
|
|
|
working = True
|
2023-11-12 04:12:05 -05:00
|
|
|
supports_gpt_35_turbo = True
|
2023-11-16 10:56:23 -05:00
|
|
|
supports_stream = True
|
2023-10-22 02:57:31 -04:00
|
|
|
|
|
|
|
@classmethod
|
2023-11-16 10:56:23 -05:00
|
|
|
def create_completion(
|
2023-10-22 02:57:31 -04:00
|
|
|
cls,
|
|
|
|
model: str,
|
|
|
|
messages: Messages,
|
2023-11-16 10:56:23 -05:00
|
|
|
stream: bool,
|
2023-10-22 02:57:31 -04:00
|
|
|
proxy: str = None,
|
|
|
|
timeout: int = 120,
|
2023-11-16 10:56:23 -05:00
|
|
|
browser: WebDriver = None,
|
2023-11-16 22:50:37 -05:00
|
|
|
headless: bool = True,
|
2023-10-22 02:57:31 -04:00
|
|
|
**kwargs
|
2023-11-16 10:56:23 -05:00
|
|
|
) -> CreateResult:
|
2023-11-16 22:50:37 -05:00
|
|
|
driver = browser if browser else get_browser("", headless, proxy)
|
2023-11-16 10:56:23 -05:00
|
|
|
|
|
|
|
from selenium.webdriver.common.by import By
|
|
|
|
from selenium.webdriver.support.ui import WebDriverWait
|
|
|
|
from selenium.webdriver.support import expected_conditions as EC
|
|
|
|
|
|
|
|
driver.get(cls.url)
|
|
|
|
try:
|
2023-11-16 21:19:27 -05:00
|
|
|
# Wait for page load and cloudflare validation
|
2023-11-16 10:56:23 -05:00
|
|
|
WebDriverWait(driver, timeout).until(
|
|
|
|
EC.presence_of_element_located((By.CSS_SELECTOR, "body:not(.no-js)"))
|
|
|
|
)
|
2023-11-16 21:19:27 -05:00
|
|
|
# Send request with message
|
2023-11-16 10:56:23 -05:00
|
|
|
script = """
|
|
|
|
response = await fetch("https://api.myshell.ai/v1/bot/chat/send_message", {
|
|
|
|
"headers": {
|
|
|
|
"accept": "application/json",
|
|
|
|
"content-type": "application/json",
|
|
|
|
"myshell-service-name": "organics-api",
|
|
|
|
"visitor-id": localStorage.getItem("mix_visitorId")
|
|
|
|
},
|
|
|
|
"body": '{body}',
|
|
|
|
"method": "POST"
|
|
|
|
})
|
|
|
|
window.reader = response.body.getReader();
|
|
|
|
"""
|
2023-10-22 02:57:31 -04:00
|
|
|
data = {
|
2023-11-16 10:56:23 -05:00
|
|
|
"botId": "4738",
|
2023-10-22 02:57:31 -04:00
|
|
|
"conversation_scenario": 3,
|
2023-11-16 10:56:23 -05:00
|
|
|
"message": format_prompt(messages),
|
2023-10-22 02:57:31 -04:00
|
|
|
"messageType": 1
|
|
|
|
}
|
2023-11-16 10:56:23 -05:00
|
|
|
driver.execute_script(script.replace("{body}", json.dumps(data)))
|
|
|
|
script = """
|
|
|
|
chunk = await window.reader.read();
|
2023-11-16 12:54:59 -05:00
|
|
|
if (chunk['done']) return null;
|
2023-11-16 21:19:27 -05:00
|
|
|
text = (new TextDecoder()).decode(chunk['value']);
|
2023-11-16 10:56:23 -05:00
|
|
|
content = '';
|
|
|
|
text.split('\\n').forEach((line, index) => {
|
|
|
|
if (line.startsWith('data: ')) {
|
|
|
|
try {
|
|
|
|
const data = JSON.parse(line.substring('data: '.length));
|
|
|
|
if ('content' in data) {
|
|
|
|
content += data['content'];
|
|
|
|
}
|
|
|
|
} catch(e) {}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return content;
|
|
|
|
"""
|
2023-11-16 12:54:59 -05:00
|
|
|
while True:
|
2023-11-16 21:19:27 -05:00
|
|
|
chunk = driver.execute_script(script)
|
2023-11-16 12:54:59 -05:00
|
|
|
if chunk:
|
|
|
|
yield chunk
|
|
|
|
elif chunk != "":
|
|
|
|
break
|
2023-11-16 10:56:23 -05:00
|
|
|
finally:
|
|
|
|
if not browser:
|
2023-11-16 22:50:37 -05:00
|
|
|
driver.close()
|
2023-11-16 10:56:23 -05:00
|
|
|
time.sleep(0.1)
|
2023-11-16 22:50:37 -05:00
|
|
|
driver.quit()
|