AutonomousAI.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from __future__ import annotations
  2. from aiohttp import ClientSession
  3. import base64
  4. import json
  5. from ..typing import AsyncResult, Messages
  6. from ..requests.raise_for_status import raise_for_status
  7. from ..providers.response import FinishReason
  8. from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
  9. class AutonomousAI(AsyncGeneratorProvider, ProviderModelMixin):
  10. url = "https://www.autonomous.ai/anon/"
  11. api_endpoints = {
  12. "llama": "https://chatgpt.autonomous.ai/api/v1/ai/chat",
  13. "qwen_coder": "https://chatgpt.autonomous.ai/api/v1/ai/chat",
  14. "hermes": "https://chatgpt.autonomous.ai/api/v1/ai/chat-hermes",
  15. "vision": "https://chatgpt.autonomous.ai/api/v1/ai/chat-vision",
  16. "summary": "https://chatgpt.autonomous.ai/api/v1/ai/summary"
  17. }
  18. working = True
  19. supports_stream = True
  20. supports_system_message = True
  21. supports_message_history = True
  22. default_model = "llama"
  23. models = [default_model, "qwen_coder", "hermes", "vision", "summary"]
  24. model_aliases = {
  25. "llama-3.3-70b": default_model,
  26. "qwen-2.5-coder-32b": "qwen_coder",
  27. "hermes-3": "hermes",
  28. "llama-3.2-90b": "vision",
  29. "llama-3.2-70b": "summary",
  30. }
  31. @classmethod
  32. async def create_async_generator(
  33. cls,
  34. model: str,
  35. messages: Messages,
  36. proxy: str = None,
  37. stream: bool = False,
  38. **kwargs
  39. ) -> AsyncResult:
  40. api_endpoint = cls.api_endpoints[model]
  41. headers = {
  42. 'accept': '*/*',
  43. 'accept-language': 'en-US,en;q=0.9',
  44. 'content-type': 'application/json',
  45. 'country-code': 'US',
  46. 'origin': 'https://www.autonomous.ai',
  47. 'referer': 'https://www.autonomous.ai/',
  48. 'time-zone': 'America/New_York',
  49. 'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36'
  50. }
  51. async with ClientSession(headers=headers) as session:
  52. message_json = json.dumps(messages)
  53. encoded_message = base64.b64encode(message_json.encode()).decode(errors="ignore")
  54. data = {
  55. "messages": encoded_message,
  56. "threadId": model,
  57. "stream": stream,
  58. "aiAgent": model
  59. }
  60. async with session.post(api_endpoint, json=data, proxy=proxy) as response:
  61. await raise_for_status(response)
  62. async for chunk in response.content:
  63. if chunk:
  64. chunk_str = chunk.decode()
  65. if chunk_str == "data: [DONE]":
  66. continue
  67. try:
  68. # Remove "data: " prefix and parse JSON
  69. chunk_data = json.loads(chunk_str.replace("data: ", ""))
  70. if "choices" in chunk_data and chunk_data["choices"]:
  71. delta = chunk_data["choices"][0].get("delta", {})
  72. if "content" in delta and delta["content"]:
  73. yield delta["content"]
  74. if "finish_reason" in chunk_data and chunk_data["finish_reason"]:
  75. yield FinishReason(chunk_data["finish_reason"])
  76. except json.JSONDecodeError:
  77. continue