MikuChat.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. from __future__ import annotations
  2. import random, json
  3. from datetime import datetime
  4. from ...requests import StreamSession
  5. from ...typing import AsyncGenerator
  6. from ..base_provider import AsyncGeneratorProvider
  7. class MikuChat(AsyncGeneratorProvider):
  8. url = "https://ai.okmiku.com"
  9. supports_gpt_35_turbo = True
  10. @classmethod
  11. async def create_async_generator(
  12. cls,
  13. model: str,
  14. messages: list[dict[str, str]],
  15. **kwargs
  16. ) -> AsyncGenerator:
  17. if not model:
  18. model = "gpt-3.5-turbo"
  19. headers = {
  20. "authority": "api.catgpt.cc",
  21. "accept": "application/json",
  22. "origin": cls.url,
  23. "referer": f"{cls.url}/chat/",
  24. 'x-app-version': 'undefined',
  25. 'x-date': get_datetime(),
  26. 'x-fingerprint': get_fingerprint(),
  27. 'x-platform': 'web'
  28. }
  29. async with StreamSession(headers=headers, impersonate="chrome107") as session:
  30. data = {
  31. "model": model,
  32. "top_p": 0.8,
  33. "temperature": 0.5,
  34. "presence_penalty": 1,
  35. "frequency_penalty": 0,
  36. "max_tokens": 2000,
  37. "stream": True,
  38. "messages": messages,
  39. }
  40. async with session.post("https://api.catgpt.cc/ai/v1/chat/completions", json=data) as response:
  41. print(await response.text())
  42. response.raise_for_status()
  43. async for line in response.iter_lines():
  44. if line.startswith(b"data: "):
  45. line = json.loads(line[6:])
  46. chunk = line["choices"][0]["delta"].get("content")
  47. if chunk:
  48. yield chunk
  49. def k(e: str, t: int):
  50. a = len(e) & 3
  51. s = len(e) - a
  52. i = t
  53. c = 3432918353
  54. o = 461845907
  55. n = 0
  56. r = 0
  57. while n < s:
  58. r = (ord(e[n]) & 255) | ((ord(e[n + 1]) & 255) << 8) | ((ord(e[n + 2]) & 255) << 16) | ((ord(e[n + 3]) & 255) << 24)
  59. n += 4
  60. r = (r & 65535) * c + (((r >> 16) * c & 65535) << 16) & 4294967295
  61. r = (r << 15) | (r >> 17)
  62. r = (r & 65535) * o + (((r >> 16) * o & 65535) << 16) & 4294967295
  63. i ^= r
  64. i = (i << 13) | (i >> 19)
  65. l = (i & 65535) * 5 + (((i >> 16) * 5 & 65535) << 16) & 4294967295
  66. i = (l & 65535) + 27492 + (((l >> 16) + 58964 & 65535) << 16)
  67. if a == 3:
  68. r ^= (ord(e[n + 2]) & 255) << 16
  69. elif a == 2:
  70. r ^= (ord(e[n + 1]) & 255) << 8
  71. elif a == 1:
  72. r ^= ord(e[n]) & 255
  73. r = (r & 65535) * c + (((r >> 16) * c & 65535) << 16) & 4294967295
  74. r = (r << 15) | (r >> 17)
  75. r = (r & 65535) * o + (((r >> 16) * o & 65535) << 16) & 4294967295
  76. i ^= r
  77. i ^= len(e)
  78. i ^= i >> 16
  79. i = (i & 65535) * 2246822507 + (((i >> 16) * 2246822507 & 65535) << 16) & 4294967295
  80. i ^= i >> 13
  81. i = (i & 65535) * 3266489909 + (((i >> 16) * 3266489909 & 65535) << 16) & 4294967295
  82. i ^= i >> 16
  83. return i & 0xFFFFFFFF
  84. def get_fingerprint() -> str:
  85. return str(k(str(int(random.random() * 100000)), 256))
  86. def get_datetime() -> str:
  87. return datetime.now().strftime("%Y-%m-%d %H:%M:%S")