NexraBlackbox.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. from __future__ import annotations
  2. import json
  3. from aiohttp import ClientSession, ClientTimeout, ClientError
  4. from ...typing import AsyncResult, Messages
  5. from ..base_provider import AsyncGeneratorProvider, ProviderModelMixin
  6. class NexraBlackbox(AsyncGeneratorProvider, ProviderModelMixin):
  7. label = "Nexra Blackbox"
  8. url = "https://nexra.aryahcr.cc/documentation/blackbox/en"
  9. api_endpoint = "https://nexra.aryahcr.cc/api/chat/complements"
  10. working = True
  11. supports_stream = True
  12. default_model = 'blackbox'
  13. models = [default_model]
  14. model_aliases = {
  15. "blackboxai": "blackbox",
  16. }
  17. @classmethod
  18. def get_model(cls, model: str) -> str:
  19. if model in cls.models:
  20. return model
  21. elif model in cls.model_aliases:
  22. return cls.model_aliases[model]
  23. else:
  24. return cls.default_model
  25. @classmethod
  26. async def create_async_generator(
  27. cls,
  28. model: str,
  29. messages: Messages,
  30. proxy: str = None,
  31. stream: bool = False,
  32. markdown: bool = False,
  33. websearch: bool = False,
  34. **kwargs
  35. ) -> AsyncResult:
  36. model = cls.get_model(model)
  37. headers = {
  38. "Content-Type": "application/json"
  39. }
  40. payload = {
  41. "messages": [{"role": msg["role"], "content": msg["content"]} for msg in messages],
  42. "websearch": websearch,
  43. "stream": stream,
  44. "markdown": markdown,
  45. "model": model
  46. }
  47. timeout = ClientTimeout(total=600) # 10 minutes timeout
  48. try:
  49. async with ClientSession(headers=headers, timeout=timeout) as session:
  50. async with session.post(cls.api_endpoint, json=payload, proxy=proxy) as response:
  51. if response.status != 200:
  52. error_text = await response.text()
  53. raise Exception(f"Error: {response.status} - {error_text}")
  54. content = await response.text()
  55. # Split content by Record Separator character
  56. parts = content.split('\x1e')
  57. full_message = ""
  58. links = []
  59. for part in parts:
  60. if part:
  61. try:
  62. json_response = json.loads(part)
  63. if json_response.get("message"):
  64. full_message = json_response["message"] # Overwrite instead of append
  65. if isinstance(json_response.get("search"), list):
  66. links = json_response["search"] # Overwrite instead of extend
  67. if json_response.get("finish", False):
  68. break
  69. except json.JSONDecodeError:
  70. pass
  71. if full_message:
  72. yield full_message.strip()
  73. if payload["websearch"] and links:
  74. yield "\n\n**Source:**"
  75. for i, link in enumerate(links, start=1):
  76. yield f"\n{i}. {link['title']}: {link['link']}"
  77. except ClientError:
  78. raise
  79. except Exception:
  80. raise