GptForLove.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. from __future__ import annotations
  2. from aiohttp import ClientSession
  3. import os
  4. import json
  5. try:
  6. import execjs
  7. has_requirements = True
  8. except ImportError:
  9. has_requirements = False
  10. from ..typing import AsyncResult, Messages
  11. from .base_provider import AsyncGeneratorProvider
  12. from .helper import format_prompt
  13. from ..errors import MissingRequirementsError
  14. class GptForLove(AsyncGeneratorProvider):
  15. url = "https://ai18.gptforlove.com"
  16. working = True
  17. supports_gpt_35_turbo = True
  18. @classmethod
  19. async def create_async_generator(
  20. cls,
  21. model: str,
  22. messages: Messages,
  23. proxy: str = None,
  24. **kwargs
  25. ) -> AsyncResult:
  26. if not has_requirements:
  27. raise MissingRequirementsError('Install "PyExecJS" package')
  28. if not model:
  29. model = "gpt-3.5-turbo"
  30. headers = {
  31. "authority": "api.gptplus.one",
  32. "accept": "application/json, text/plain, */*",
  33. "accept-language": "de-DE,de;q=0.9,en-DE;q=0.8,en;q=0.7,en-US;q=0.6,nl;q=0.5,zh-CN;q=0.4,zh-TW;q=0.3,zh;q=0.2",
  34. "content-type": "application/json",
  35. "origin": cls.url,
  36. "referer": f"{cls.url}/",
  37. "sec-ch-ua": "\"Google Chrome\";v=\"117\", \"Not;A=Brand\";v=\"8\", \"Chromium\";v=\"117\"",
  38. "sec-ch-ua-mobile": "?0",
  39. "sec-ch-ua-platform": "Linux",
  40. "sec-fetch-dest": "empty",
  41. "sec-fetch-mode": "cors",
  42. "sec-fetch-site": "cross-site",
  43. "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36"
  44. }
  45. async with ClientSession(headers=headers) as session:
  46. prompt = format_prompt(messages)
  47. data = {
  48. "prompt": prompt,
  49. "options": {},
  50. "systemMessage": kwargs.get("system_message", "You are ChatGPT, the version is GPT3.5, a large language model trained by OpenAI. Follow the user's instructions carefully."),
  51. "temperature": kwargs.get("temperature", 0.8),
  52. "top_p": kwargs.get("top_p", 1),
  53. "secret": get_secret(),
  54. }
  55. async with session.post("https://api.gptplus.one/chat-process", json=data, proxy=proxy) as response:
  56. response.raise_for_status()
  57. async for line in response.content:
  58. try:
  59. line = json.loads(line)
  60. except:
  61. raise RuntimeError(f"Broken line: {line}")
  62. if "detail" in line:
  63. content = line["detail"]["choices"][0]["delta"].get("content")
  64. if content:
  65. yield content
  66. elif "10分钟内提问超过了5次" in line:
  67. raise RuntimeError("Rate limit reached")
  68. else:
  69. raise RuntimeError(f"Response: {line}")
  70. def get_secret() -> str:
  71. dir = os.path.dirname(__file__)
  72. include = f'{dir}/npm/node_modules/crypto-js/crypto-js'
  73. source = """
  74. CryptoJS = require({include})
  75. var k = 'fjfsdwiuhfwf'
  76. , e = Math.floor(new Date().getTime() / 1e3);
  77. var t = CryptoJS.enc.Utf8.parse(e)
  78. , o = CryptoJS.AES.encrypt(t, k, {
  79. mode: CryptoJS.mode.ECB,
  80. padding: CryptoJS.pad.Pkcs7
  81. });
  82. return o.toString()
  83. """
  84. source = source.replace('{include}', json.dumps(include))
  85. return execjs.compile(source).call('')