GptForLove.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. from __future__ import annotations
  2. from aiohttp import ClientSession
  3. import json
  4. from Crypto.Cipher import AES
  5. from Crypto.Util import Padding
  6. import base64
  7. import hashlib
  8. import time
  9. import math
  10. from ..typing import AsyncResult, Messages
  11. from .base_provider import AsyncGeneratorProvider
  12. from .helper import format_prompt
  13. class GptForLove(AsyncGeneratorProvider):
  14. url = "https://ai18.gptforlove.com"
  15. working = True
  16. supports_gpt_35_turbo = True
  17. @classmethod
  18. async def create_async_generator(
  19. cls,
  20. model: str,
  21. messages: Messages,
  22. proxy: str = None,
  23. **kwargs
  24. ) -> AsyncResult:
  25. if not model:
  26. model = "gpt-3.5-turbo"
  27. headers = {
  28. "authority": "api.gptplus.one",
  29. "accept": "application/json, text/plain, */*",
  30. "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",
  31. "content-type": "application/json",
  32. "origin": cls.url,
  33. "referer": f"{cls.url}/",
  34. "sec-ch-ua": "\"Google Chrome\";v=\"117\", \"Not;A=Brand\";v=\"8\", \"Chromium\";v=\"117\"",
  35. "sec-ch-ua-mobile": "?0",
  36. "sec-ch-ua-platform": "Linux",
  37. "sec-fetch-dest": "empty",
  38. "sec-fetch-mode": "cors",
  39. "sec-fetch-site": "cross-site",
  40. "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36"
  41. }
  42. async with ClientSession(headers=headers) as session:
  43. prompt = format_prompt(messages)
  44. data = {
  45. "prompt": prompt,
  46. "options": {},
  47. "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."),
  48. "temperature": kwargs.get("temperature", 0.8),
  49. "top_p": kwargs.get("top_p", 1),
  50. "secret": get_secret(),
  51. }
  52. async with session.post("https://api.gptplus.one/chat-process", json=data, proxy=proxy) as response:
  53. response.raise_for_status()
  54. async for line in response.content:
  55. try:
  56. line = json.loads(line)
  57. except:
  58. raise RuntimeError(f"Broken line: {line}")
  59. if "detail" in line:
  60. content = line["detail"]["choices"][0]["delta"].get("content")
  61. if content:
  62. yield content
  63. elif "10分钟内提问超过了5次" in line:
  64. raise RuntimeError("Rate limit reached")
  65. else:
  66. raise RuntimeError(f"Response: {line}")
  67. def get_secret() -> str:
  68. k = '14487141bvirvvG'
  69. e = math.floor(time.time())
  70. plaintext = str(e).encode('utf-8')
  71. key = hashlib.md5(k.encode('utf-8')).digest()
  72. cipher = AES.new(key, AES.MODE_ECB)
  73. ciphertext = cipher.encrypt(Padding.pad(plaintext, AES.block_size, style='pkcs7'))
  74. return base64.b64encode(ciphertext).decode()