ChatgptDemo.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. from __future__ import annotations
  2. import time, json, re
  3. from aiohttp import ClientSession
  4. from ..typing import AsyncResult, Messages
  5. from .base_provider import AsyncGeneratorProvider
  6. from .helper import format_prompt
  7. class ChatgptDemo(AsyncGeneratorProvider):
  8. url = "https://chat.chatgptdemo.net"
  9. supports_gpt_35_turbo = True
  10. working = False
  11. @classmethod
  12. async def create_async_generator(
  13. cls,
  14. model: str,
  15. messages: Messages,
  16. proxy: str = None,
  17. **kwargs
  18. ) -> AsyncResult:
  19. headers = {
  20. "authority": "chat.chatgptdemo.net",
  21. "accept-language": "de-DE,de;q=0.9,en-DE;q=0.8,en;q=0.7,en-US",
  22. "origin": "https://chat.chatgptdemo.net",
  23. "referer": "https://chat.chatgptdemo.net/",
  24. "sec-ch-ua": '"Google Chrome";v="117", "Not;A=Brand";v="8", "Chromium";v="117"',
  25. "sec-ch-ua-mobile": "?0",
  26. "sec-ch-ua-platform": '"Linux"',
  27. "sec-fetch-dest": "empty",
  28. "sec-fetch-mode": "cors",
  29. "sec-fetch-site": "same-origin",
  30. "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36"
  31. }
  32. async with ClientSession(headers=headers) as session:
  33. async with session.get(f"{cls.url}/", proxy=proxy) as response:
  34. response.raise_for_status()
  35. response = await response.text()
  36. result = re.search(
  37. r'<div id="USERID" style="display: none">(.*?)<\/div>',
  38. response,
  39. )
  40. if result:
  41. user_id = result.group(1)
  42. else:
  43. raise RuntimeError("No user id found")
  44. async with session.post(f"{cls.url}/new_chat", json={"user_id": user_id}, proxy=proxy) as response:
  45. response.raise_for_status()
  46. chat_id = (await response.json())["id_"]
  47. if not chat_id:
  48. raise RuntimeError("Could not create new chat")
  49. data = {
  50. "question": format_prompt(messages),
  51. "chat_id": chat_id,
  52. "timestamp": int(time.time()*1000),
  53. }
  54. async with session.post(f"{cls.url}/chat_api_stream", json=data, proxy=proxy) as response:
  55. response.raise_for_status()
  56. async for line in response.content:
  57. if line.startswith(b"data: "):
  58. line = json.loads(line[6:-1])
  59. chunk = line["choices"][0]["delta"].get("content")
  60. if chunk:
  61. yield chunk