GeekGpt.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. from __future__ import annotations
  2. import requests, json
  3. from .base_provider import AbstractProvider
  4. from ..typing import CreateResult, Messages
  5. from json import dumps
  6. class GeekGpt(AbstractProvider):
  7. url = 'https://chat.geekgpt.org'
  8. working = True
  9. supports_message_history = True
  10. supports_stream = True
  11. supports_gpt_35_turbo = True
  12. supports_gpt_4 = True
  13. @classmethod
  14. def create_completion(
  15. cls,
  16. model: str,
  17. messages: Messages,
  18. stream: bool,
  19. **kwargs
  20. ) -> CreateResult:
  21. if not model:
  22. model = "gpt-3.5-turbo"
  23. json_data = {
  24. 'messages': messages,
  25. 'model': model,
  26. 'temperature': kwargs.get('temperature', 0.9),
  27. 'presence_penalty': kwargs.get('presence_penalty', 0),
  28. 'top_p': kwargs.get('top_p', 1),
  29. 'frequency_penalty': kwargs.get('frequency_penalty', 0),
  30. 'stream': True
  31. }
  32. data = dumps(json_data, separators=(',', ':'))
  33. headers = {
  34. 'authority': 'ai.fakeopen.com',
  35. 'accept': '*/*',
  36. 'accept-language': 'en,fr-FR;q=0.9,fr;q=0.8,es-ES;q=0.7,es;q=0.6,en-US;q=0.5,am;q=0.4,de;q=0.3',
  37. 'authorization': 'Bearer pk-this-is-a-real-free-pool-token-for-everyone',
  38. 'content-type': 'application/json',
  39. 'origin': 'https://chat.geekgpt.org',
  40. 'referer': 'https://chat.geekgpt.org/',
  41. 'sec-ch-ua': '"Chromium";v="118", "Google Chrome";v="118", "Not=A?Brand";v="99"',
  42. 'sec-ch-ua-mobile': '?0',
  43. 'sec-ch-ua-platform': '"macOS"',
  44. 'sec-fetch-dest': 'empty',
  45. 'sec-fetch-mode': 'cors',
  46. 'sec-fetch-site': 'cross-site',
  47. 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36',
  48. }
  49. response = requests.post("https://ai.fakeopen.com/v1/chat/completions",
  50. headers=headers, data=data, stream=True)
  51. response.raise_for_status()
  52. for chunk in response.iter_lines():
  53. if b'content' in chunk:
  54. json_data = chunk.decode().replace("data: ", "")
  55. if json_data == "[DONE]":
  56. break
  57. try:
  58. content = json.loads(json_data)["choices"][0]["delta"].get("content")
  59. except Exception as e:
  60. raise RuntimeError(f'error | {e} :', json_data)
  61. if content:
  62. yield content