Aivvm.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. from __future__ import annotations
  2. import requests
  3. import json
  4. from ..base_provider import AbstractProvider
  5. from ...typing import CreateResult, Messages
  6. # to recreate this easily, send a post request to https://chat.aivvm.com/api/models
  7. models = {
  8. 'gpt-3.5-turbo': {'id': 'gpt-3.5-turbo', 'name': 'GPT-3.5'},
  9. 'gpt-3.5-turbo-0613': {'id': 'gpt-3.5-turbo-0613', 'name': 'GPT-3.5-0613'},
  10. 'gpt-3.5-turbo-16k': {'id': 'gpt-3.5-turbo-16k', 'name': 'GPT-3.5-16K'},
  11. 'gpt-3.5-turbo-16k-0613': {'id': 'gpt-3.5-turbo-16k-0613', 'name': 'GPT-3.5-16K-0613'},
  12. 'gpt-4': {'id': 'gpt-4', 'name': 'GPT-4'},
  13. 'gpt-4-0613': {'id': 'gpt-4-0613', 'name': 'GPT-4-0613'},
  14. 'gpt-4-32k': {'id': 'gpt-4-32k', 'name': 'GPT-4-32K'},
  15. 'gpt-4-32k-0613': {'id': 'gpt-4-32k-0613', 'name': 'GPT-4-32K-0613'},
  16. }
  17. class Aivvm(AbstractProvider):
  18. url = 'https://chat.aivvm.com'
  19. supports_stream = True
  20. working = False
  21. supports_gpt_35_turbo = True
  22. supports_gpt_4 = True
  23. @classmethod
  24. def create_completion(cls,
  25. model: str,
  26. messages: Messages,
  27. stream: bool,
  28. **kwargs
  29. ) -> CreateResult:
  30. if not model:
  31. model = "gpt-3.5-turbo"
  32. elif model not in models:
  33. raise ValueError(f"Model is not supported: {model}")
  34. json_data = {
  35. "model" : models[model],
  36. "messages" : messages,
  37. "key" : "",
  38. "prompt" : kwargs.get("system_message", "You are ChatGPT, a large language model trained by OpenAI. Follow the user's instructions carefully. Respond using markdown."),
  39. "temperature" : kwargs.get("temperature", 0.7)
  40. }
  41. data = json.dumps(json_data)
  42. headers = {
  43. "accept" : "text/event-stream",
  44. "accept-language" : "en-US,en;q=0.9",
  45. "content-type" : "application/json",
  46. "content-length" : str(len(data)),
  47. "sec-ch-ua" : "\"Chrome\";v=\"117\", \"Not;A=Brand\";v=\"8\", \"Chromium\";v=\"117\"",
  48. "sec-ch-ua-mobile" : "?0",
  49. "sec-ch-ua-platform": "\"Windows\"",
  50. "sec-fetch-dest" : "empty",
  51. "sec-fetch-mode" : "cors",
  52. "sec-fetch-site" : "same-origin",
  53. "sec-gpc" : "1",
  54. "referrer" : "https://chat.aivvm.com/",
  55. "user-agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36"
  56. }
  57. response = requests.post("https://chat.aivvm.com/api/chat", headers=headers, data=data, stream=True)
  58. response.raise_for_status()
  59. for chunk in response.iter_content(chunk_size=4096):
  60. try:
  61. yield chunk.decode("utf-8")
  62. except UnicodeDecodeError:
  63. yield chunk.decode("unicode-escape")