Wuguokai.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. from __future__ import annotations
  2. import random
  3. import requests
  4. from ...typing import Any, CreateResult
  5. from ..base_provider import AbstractProvider, format_prompt
  6. class Wuguokai(AbstractProvider):
  7. url = 'https://chat.wuguokai.xyz'
  8. supports_gpt_35_turbo = True
  9. working = False
  10. @staticmethod
  11. def create_completion(
  12. model: str,
  13. messages: list[dict[str, str]],
  14. stream: bool,
  15. **kwargs: Any,
  16. ) -> CreateResult:
  17. headers = {
  18. 'authority': 'ai-api.wuguokai.xyz',
  19. 'accept': 'application/json, text/plain, */*',
  20. 'accept-language': 'id-ID,id;q=0.9,en-US;q=0.8,en;q=0.7',
  21. 'content-type': 'application/json',
  22. 'origin': 'https://chat.wuguokai.xyz',
  23. 'referer': 'https://chat.wuguokai.xyz/',
  24. 'sec-ch-ua': '"Not.A/Brand";v="8", "Chromium";v="114", "Google Chrome";v="114"',
  25. 'sec-ch-ua-mobile': '?0',
  26. 'sec-ch-ua-platform': '"Windows"',
  27. 'sec-fetch-dest': 'empty',
  28. 'sec-fetch-mode': 'cors',
  29. 'sec-fetch-site': 'same-site',
  30. 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'
  31. }
  32. data ={
  33. "prompt": format_prompt(messages),
  34. "options": {},
  35. "userId": f"#/chat/{random.randint(1,99999999)}",
  36. "usingContext": True
  37. }
  38. response = requests.post(
  39. "https://ai-api20.wuguokai.xyz/api/chat-process",
  40. headers=headers,
  41. timeout=3,
  42. json=data,
  43. proxies=kwargs.get('proxy', {}),
  44. )
  45. _split = response.text.split("> 若回答失败请重试或多刷新几次界面后重试")
  46. if response.status_code != 200:
  47. raise Exception(f"Error: {response.status_code} {response.reason}")
  48. if len(_split) > 1:
  49. yield _split[1].strip()
  50. else:
  51. yield _split[0].strip()