diff options
author | Heiner Lohaus <hlohaus@users.noreply.github.com> | 2024-01-22 03:38:11 +0100 |
---|---|---|
committer | Heiner Lohaus <hlohaus@users.noreply.github.com> | 2024-01-22 03:38:11 +0100 |
commit | 38dbe4b8e5ca7f9bc0508e1ba1bf878fd6d8c19c (patch) | |
tree | 6cdb82f14fcb04e9f6b339f5cab13e73a6f75d41 /g4f/Provider/selenium/MyShell.py | |
parent | Fix error in copilot (diff) | |
download | gpt4free-38dbe4b8e5ca7f9bc0508e1ba1bf878fd6d8c19c.tar gpt4free-38dbe4b8e5ca7f9bc0508e1ba1bf878fd6d8c19c.tar.gz gpt4free-38dbe4b8e5ca7f9bc0508e1ba1bf878fd6d8c19c.tar.bz2 gpt4free-38dbe4b8e5ca7f9bc0508e1ba1bf878fd6d8c19c.tar.lz gpt4free-38dbe4b8e5ca7f9bc0508e1ba1bf878fd6d8c19c.tar.xz gpt4free-38dbe4b8e5ca7f9bc0508e1ba1bf878fd6d8c19c.tar.zst gpt4free-38dbe4b8e5ca7f9bc0508e1ba1bf878fd6d8c19c.zip |
Diffstat (limited to 'g4f/Provider/selenium/MyShell.py')
-rw-r--r-- | g4f/Provider/selenium/MyShell.py | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/g4f/Provider/selenium/MyShell.py b/g4f/Provider/selenium/MyShell.py new file mode 100644 index 00000000..a3f246ff --- /dev/null +++ b/g4f/Provider/selenium/MyShell.py @@ -0,0 +1,76 @@ +from __future__ import annotations + +import time, json + +from ...typing import CreateResult, Messages +from ..base_provider import AbstractProvider +from ..helper import format_prompt +from ...webdriver import WebDriver, WebDriverSession, bypass_cloudflare + +class MyShell(AbstractProvider): + url = "https://app.myshell.ai/chat" + working = True + supports_gpt_35_turbo = True + supports_stream = True + + @classmethod + def create_completion( + cls, + model: str, + messages: Messages, + stream: bool, + proxy: str = None, + timeout: int = 120, + webdriver: WebDriver = None, + **kwargs + ) -> CreateResult: + with WebDriverSession(webdriver, "", proxy=proxy) as driver: + bypass_cloudflare(driver, cls.url, timeout) + + # Send request with message + data = { + "botId": "4738", + "conversation_scenario": 3, + "message": format_prompt(messages), + "messageType": 1 + } + 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.pipeThrough(new TextDecoderStream()).getReader(); +""" + driver.execute_script(script.replace("{body}", json.dumps(data))) + script = """ +chunk = await window._reader.read(); +if (chunk.done) { + return null; +} +content = ''; +chunk.value.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; +""" + while True: + chunk = driver.execute_script(script) + if chunk: + yield chunk + elif chunk != "": + break + else: + time.sleep(0.1)
\ No newline at end of file |