RobocodersAPI.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. from __future__ import annotations
  2. import json
  3. import aiohttp
  4. from ..typing import AsyncResult, Messages
  5. from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
  6. from .helper import format_prompt
  7. class RobocodersAPI(AsyncGeneratorProvider, ProviderModelMixin):
  8. label = "API Robocoders AI"
  9. url = "https://api.robocoders.ai/docs"
  10. api_endpoint = "https://api.robocoders.ai/chat"
  11. working = True
  12. supports_message_history = True
  13. default_model = 'GeneralCodingAgent'
  14. agent = [default_model, "RepoAgent", "FrontEndAgent"]
  15. models = [*agent]
  16. @classmethod
  17. async def create_async_generator(
  18. cls,
  19. model: str,
  20. messages: Messages,
  21. proxy: str = None,
  22. **kwargs
  23. ) -> AsyncResult:
  24. async with aiohttp.ClientSession() as session:
  25. access_token = await cls._get_access_token(session)
  26. if not access_token:
  27. raise Exception("Failed to get access token")
  28. session_id = await cls._create_session(session, access_token)
  29. if not session_id:
  30. raise Exception("Failed to create session")
  31. headers = {
  32. "Content-Type": "application/json",
  33. "Authorization": f"Bearer {access_token}"
  34. }
  35. prompt = format_prompt(messages)
  36. data = {
  37. "sid": session_id,
  38. "prompt": prompt,
  39. "agent": model
  40. }
  41. async with session.post(cls.api_endpoint, headers=headers, json=data, proxy=proxy) as response:
  42. if response.status != 200:
  43. raise Exception(f"Error: {response.status}")
  44. async for line in response.content:
  45. if line:
  46. try:
  47. response_data = json.loads(line)
  48. message = response_data.get('message', '')
  49. if message:
  50. yield message
  51. except json.JSONDecodeError:
  52. pass
  53. @staticmethod
  54. async def _get_access_token(session: aiohttp.ClientSession) -> str:
  55. url_auth = 'https://api.robocoders.ai/auth'
  56. headers_auth = {
  57. 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
  58. 'accept-language': 'en-US,en;q=0.9',
  59. 'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36',
  60. }
  61. async with session.get(url_auth, headers=headers_auth) as response:
  62. if response.status == 200:
  63. text = await response.text()
  64. return text.split('id="token">')[1].split('</pre>')[0].strip()
  65. return None
  66. @staticmethod
  67. async def _create_session(session: aiohttp.ClientSession, access_token: str) -> str:
  68. url_create_session = 'https://api.robocoders.ai/create-session'
  69. headers_create_session = {
  70. 'Authorization': f'Bearer {access_token}'
  71. }
  72. async with session.get(url_create_session, headers=headers_create_session) as response:
  73. if response.status == 200:
  74. data = await response.json()
  75. return data.get('sid')
  76. return None