MyShell.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. from __future__ import annotations
  2. import time, json
  3. from ..typing import CreateResult, Messages
  4. from .base_provider import BaseProvider
  5. from .helper import format_prompt
  6. from ..webdriver import WebDriver, WebDriverSession, bypass_cloudflare
  7. class MyShell(BaseProvider):
  8. url = "https://app.myshell.ai/chat"
  9. working = True
  10. supports_gpt_35_turbo = True
  11. supports_stream = True
  12. @classmethod
  13. def create_completion(
  14. cls,
  15. model: str,
  16. messages: Messages,
  17. stream: bool,
  18. proxy: str = None,
  19. timeout: int = 120,
  20. webdriver: WebDriver = None,
  21. **kwargs
  22. ) -> CreateResult:
  23. with WebDriverSession(webdriver, "", proxy=proxy) as driver:
  24. bypass_cloudflare(driver, cls.url, timeout)
  25. # Send request with message
  26. data = {
  27. "botId": "4738",
  28. "conversation_scenario": 3,
  29. "message": format_prompt(messages),
  30. "messageType": 1
  31. }
  32. script = """
  33. response = await fetch("https://api.myshell.ai/v1/bot/chat/send_message", {
  34. "headers": {
  35. "accept": "application/json",
  36. "content-type": "application/json",
  37. "myshell-service-name": "organics-api",
  38. "visitor-id": localStorage.getItem("mix_visitorId")
  39. },
  40. "body": '{body}',
  41. "method": "POST"
  42. })
  43. window._reader = response.body.pipeThrough(new TextDecoderStream()).getReader();
  44. """
  45. driver.execute_script(script.replace("{body}", json.dumps(data)))
  46. script = """
  47. chunk = await window._reader.read();
  48. if (chunk.done) {
  49. return null;
  50. }
  51. content = '';
  52. chunk.value.split('\\n').forEach((line, index) => {
  53. if (line.startsWith('data: ')) {
  54. try {
  55. const data = JSON.parse(line.substring('data: '.length));
  56. if ('content' in data) {
  57. content += data['content'];
  58. }
  59. } catch(e) {}
  60. }
  61. });
  62. return content;
  63. """
  64. while True:
  65. chunk = driver.execute_script(script)
  66. if chunk:
  67. yield chunk
  68. elif chunk != "":
  69. break
  70. else:
  71. time.sleep(0.1)