VoiGpt.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. from __future__ import annotations
  2. import json
  3. import requests
  4. from ..base_provider import AbstractProvider
  5. from ...typing import Messages, CreateResult
  6. class VoiGpt(AbstractProvider):
  7. """
  8. VoiGpt - A provider for VoiGpt.com
  9. **Note** : to use this provider you have to get your csrf token/cookie from the voigpt.com website
  10. Args:
  11. model: The model to use
  12. messages: The messages to send
  13. stream: Whether to stream the response
  14. proxy: The proxy to use
  15. access_token: The access token to use
  16. **kwargs: Additional keyword arguments
  17. Returns:
  18. A CreateResult object
  19. """
  20. url = "https://voigpt.com"
  21. working = False
  22. supports_gpt_35_turbo = True
  23. supports_message_history = True
  24. supports_stream = False
  25. _access_token: str = None
  26. @classmethod
  27. def create_completion(
  28. cls,
  29. model: str,
  30. messages: Messages,
  31. stream: bool,
  32. proxy: str = None,
  33. access_token: str = None,
  34. **kwargs
  35. ) -> CreateResult:
  36. if not model:
  37. model = "gpt-3.5-turbo"
  38. if not access_token:
  39. access_token = cls._access_token
  40. if not access_token:
  41. headers = {
  42. "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
  43. "accept-language": "de-DE,de;q=0.9,en-DE;q=0.8,en;q=0.7,en-US;q=0.6",
  44. "sec-ch-ua": "\"Google Chrome\";v=\"119\", \"Chromium\";v=\"119\", \"Not?A_Brand\";v=\"24\"",
  45. "sec-ch-ua-mobile": "?0",
  46. "sec-ch-ua-platform": "\"Linux\"",
  47. "sec-fetch-dest": "document",
  48. "sec-fetch-mode": "navigate",
  49. "sec-fetch-site": "none",
  50. "sec-fetch-user": "?1",
  51. "upgrade-insecure-requests": "1",
  52. "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36",
  53. }
  54. req_response = requests.get(cls.url, headers=headers)
  55. access_token = cls._access_token = req_response.cookies.get("csrftoken")
  56. headers = {
  57. "Accept-Encoding": "gzip, deflate, br",
  58. "Accept-Language": "de-DE,de;q=0.9,en-DE;q=0.8,en;q=0.7,en-US;q=0.6",
  59. "Cookie": f"csrftoken={access_token};",
  60. "Origin": "https://voigpt.com",
  61. "Referer": "https://voigpt.com/",
  62. "Sec-Ch-Ua": "'Google Chrome';v='119', 'Chromium';v='119', 'Not?A_Brand';v='24'",
  63. "Sec-Ch-Ua-Mobile": "?0",
  64. "Sec-Ch-Ua-Platform": "'Windows'",
  65. "Sec-Fetch-Dest": "empty",
  66. "Sec-Fetch-Mode": "cors",
  67. "Sec-Fetch-Site": "same-origin",
  68. "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36",
  69. "X-Csrftoken": access_token,
  70. }
  71. payload = {
  72. "messages": messages,
  73. }
  74. request_url = f"{cls.url}/generate_response/"
  75. req_response = requests.post(request_url, headers=headers, json=payload)
  76. try:
  77. response = json.loads(req_response.text)
  78. yield response["response"]
  79. except:
  80. raise RuntimeError(f"Response: {req_response.text}")