MyShell.py 2.1 KB

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