2023-10-22 09:57:31 +03:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-11-16 18:56:23 +03:00
|
|
|
import time, json
|
2023-10-22 09:57:31 +03:00
|
|
|
|
2023-11-16 18:56:23 +03:00
|
|
|
from ..typing import CreateResult, Messages
|
2024-01-01 19:48:57 +03:00
|
|
|
from .base_provider import AbstractProvider
|
2023-11-20 16:00:40 +03:00
|
|
|
from .helper import format_prompt
|
2023-12-07 09:18:05 +03:00
|
|
|
from ..webdriver import WebDriver, WebDriverSession, bypass_cloudflare
|
2023-11-16 18:56:23 +03:00
|
|
|
|
2024-01-01 19:48:57 +03:00
|
|
|
class MyShell(AbstractProvider):
|
2023-10-22 10:04:14 +03:00
|
|
|
url = "https://app.myshell.ai/chat"
|
2023-10-24 19:58:12 +03:00
|
|
|
working = True
|
2023-11-12 12:12:05 +03:00
|
|
|
supports_gpt_35_turbo = True
|
2023-11-16 18:56:23 +03:00
|
|
|
supports_stream = True
|
2023-10-22 09:57:31 +03:00
|
|
|
|
|
|
|
@classmethod
|
2023-11-16 18:56:23 +03:00
|
|
|
def create_completion(
|
2023-10-22 09:57:31 +03:00
|
|
|
cls,
|
|
|
|
model: str,
|
|
|
|
messages: Messages,
|
2023-11-16 18:56:23 +03:00
|
|
|
stream: bool,
|
2023-10-22 09:57:31 +03:00
|
|
|
proxy: str = None,
|
|
|
|
timeout: int = 120,
|
2023-11-20 16:00:40 +03:00
|
|
|
webdriver: WebDriver = None,
|
2023-10-22 09:57:31 +03:00
|
|
|
**kwargs
|
2023-11-16 18:56:23 +03:00
|
|
|
) -> CreateResult:
|
2023-11-20 16:00:40 +03:00
|
|
|
with WebDriverSession(webdriver, "", proxy=proxy) as driver:
|
2023-12-07 09:18:05 +03:00
|
|
|
bypass_cloudflare(driver, cls.url, timeout)
|
|
|
|
|
2023-11-17 05:19:27 +03:00
|
|
|
# Send request with message
|
2023-11-19 07:36:04 +03:00
|
|
|
data = {
|
|
|
|
"botId": "4738",
|
|
|
|
"conversation_scenario": 3,
|
|
|
|
"message": format_prompt(messages),
|
|
|
|
"messageType": 1
|
|
|
|
}
|
2023-11-16 18:56:23 +03: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"
|
|
|
|
})
|
2023-11-20 16:00:40 +03:00
|
|
|
window._reader = response.body.pipeThrough(new TextDecoderStream()).getReader();
|
2023-11-16 18:56:23 +03:00
|
|
|
"""
|
|
|
|
driver.execute_script(script.replace("{body}", json.dumps(data)))
|
|
|
|
script = """
|
2023-11-20 16:00:40 +03:00
|
|
|
chunk = await window._reader.read();
|
2023-12-07 09:18:05 +03:00
|
|
|
if (chunk.done) {
|
2023-11-20 16:00:40 +03:00
|
|
|
return null;
|
|
|
|
}
|
2023-11-16 18:56:23 +03:00
|
|
|
content = '';
|
2023-12-07 09:18:05 +03:00
|
|
|
chunk.value.split('\\n').forEach((line, index) => {
|
2023-11-16 18:56:23 +03:00
|
|
|
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 20:54:59 +03:00
|
|
|
while True:
|
2023-11-17 05:19:27 +03:00
|
|
|
chunk = driver.execute_script(script)
|
2023-11-16 20:54:59 +03:00
|
|
|
if chunk:
|
|
|
|
yield chunk
|
|
|
|
elif chunk != "":
|
|
|
|
break
|
2023-11-18 06:38:31 +03:00
|
|
|
else:
|
2023-11-19 07:36:04 +03:00
|
|
|
time.sleep(0.1)
|