VoiGpt.py 3.4 KB

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