Yqcloud.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. from __future__ import annotations
  2. import time
  3. from aiohttp import ClientSession
  4. from ..typing import AsyncResult, Messages
  5. from ..requests.raise_for_status import raise_for_status
  6. from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
  7. from .helper import format_prompt
  8. from ..providers.response import FinishReason, JsonConversation
  9. class Conversation(JsonConversation):
  10. userId: str = None
  11. message_history: Messages = []
  12. def __init__(self, model: str):
  13. self.model = model
  14. self.userId = f"#/chat/{int(time.time() * 1000)}"
  15. class Yqcloud(AsyncGeneratorProvider, ProviderModelMixin):
  16. url = "https://chat9.yqcloud.top"
  17. api_endpoint = "https://api.binjie.fun/api/generateStream"
  18. working = True
  19. supports_stream = True
  20. supports_system_message = True
  21. supports_message_history = True
  22. default_model = "gpt-4"
  23. models = [default_model]
  24. @classmethod
  25. async def create_async_generator(
  26. cls,
  27. model: str,
  28. messages: Messages,
  29. stream: bool = True,
  30. proxy: str = None,
  31. conversation: Conversation = None,
  32. return_conversation: bool = False,
  33. **kwargs
  34. ) -> AsyncResult:
  35. model = cls.get_model(model)
  36. headers = {
  37. "accept": "application/json, text/plain, */*",
  38. "accept-language": "en-US,en;q=0.9",
  39. "content-type": "application/json",
  40. "origin": f"{cls.url}",
  41. "referer": f"{cls.url}/",
  42. "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"
  43. }
  44. if conversation is None:
  45. conversation = Conversation(model)
  46. conversation.message_history = messages
  47. else:
  48. conversation.message_history.append(messages[-1])
  49. # Extract system message if present
  50. system_message = ""
  51. current_messages = conversation.message_history
  52. if current_messages and current_messages[0]["role"] == "system":
  53. system_message = current_messages[0]["content"]
  54. current_messages = current_messages[1:]
  55. async with ClientSession(headers=headers) as session:
  56. prompt = format_prompt(current_messages)
  57. data = {
  58. "prompt": prompt,
  59. "userId": conversation.userId,
  60. "network": True,
  61. "system": system_message,
  62. "withoutContext": False,
  63. "stream": stream
  64. }
  65. async with session.post(cls.api_endpoint, json=data, proxy=proxy) as response:
  66. await raise_for_status(response)
  67. full_message = ""
  68. async for chunk in response.content:
  69. if chunk:
  70. message = chunk.decode()
  71. yield message
  72. full_message += message
  73. if return_conversation:
  74. conversation.message_history.append({"role": "assistant", "content": full_message})
  75. yield conversation
  76. yield FinishReason("stop")