CodeLinkAva.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. from __future__ import annotations
  2. from aiohttp import ClientSession
  3. import json
  4. from ...typing import AsyncGenerator
  5. from ..base_provider import AsyncGeneratorProvider
  6. class CodeLinkAva(AsyncGeneratorProvider):
  7. url = "https://ava-ai-ef611.web.app"
  8. supports_gpt_35_turbo = True
  9. working = False
  10. @classmethod
  11. async def create_async_generator(
  12. cls,
  13. model: str,
  14. messages: list[dict[str, str]],
  15. **kwargs
  16. ) -> AsyncGenerator:
  17. headers = {
  18. "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36",
  19. "Accept": "*/*",
  20. "Accept-language": "en,fr-FR;q=0.9,fr;q=0.8,es-ES;q=0.7,es;q=0.6,en-US;q=0.5,am;q=0.4,de;q=0.3",
  21. "Origin": cls.url,
  22. "Referer": f"{cls.url}/",
  23. "Sec-Fetch-Dest": "empty",
  24. "Sec-Fetch-Mode": "cors",
  25. "Sec-Fetch-Site": "same-origin",
  26. }
  27. async with ClientSession(
  28. headers=headers
  29. ) as session:
  30. data = {
  31. "messages": messages,
  32. "temperature": 0.6,
  33. "stream": True,
  34. **kwargs
  35. }
  36. async with session.post("https://ava-alpha-api.codelink.io/api/chat", json=data) as response:
  37. response.raise_for_status()
  38. async for line in response.content:
  39. line = line.decode()
  40. if line.startswith("data: "):
  41. if line.startswith("data: [DONE]"):
  42. break
  43. line = json.loads(line[6:-1])
  44. content = line["choices"][0]["delta"].get("content")
  45. if content:
  46. yield content