AiService.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. from __future__ import annotations
  2. import requests
  3. from ...typing import Any, CreateResult, Messages
  4. from ..base_provider import AbstractProvider
  5. class AiService(AbstractProvider):
  6. url = "https://aiservice.vercel.app/"
  7. working = False
  8. supports_gpt_35_turbo = True
  9. @staticmethod
  10. def create_completion(
  11. model: str,
  12. messages: Messages,
  13. stream: bool,
  14. **kwargs: Any,
  15. ) -> CreateResult:
  16. base = (
  17. "\n".join(
  18. f"{message['role']}: {message['content']}" for message in messages
  19. )
  20. + "\nassistant: "
  21. )
  22. headers = {
  23. "accept": "*/*",
  24. "content-type": "text/plain;charset=UTF-8",
  25. "sec-fetch-dest": "empty",
  26. "sec-fetch-mode": "cors",
  27. "sec-fetch-site": "same-origin",
  28. "Referer": "https://aiservice.vercel.app/chat",
  29. }
  30. data = {"input": base}
  31. url = "https://aiservice.vercel.app/api/chat/answer"
  32. response = requests.post(url, headers=headers, json=data)
  33. response.raise_for_status()
  34. yield response.json()["data"]