Komo.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. from __future__ import annotations
  2. import json
  3. from ...requests import StreamSession
  4. from ...typing import AsyncGenerator
  5. from ..base_provider import AsyncGeneratorProvider, format_prompt
  6. class Komo(AsyncGeneratorProvider):
  7. url = "https://komo.ai/api/ask"
  8. supports_gpt_35_turbo = True
  9. @classmethod
  10. async def create_async_generator(
  11. cls,
  12. model: str,
  13. messages: list[dict[str, str]],
  14. **kwargs
  15. ) -> AsyncGenerator:
  16. async with StreamSession(impersonate="chrome107") as session:
  17. prompt = format_prompt(messages)
  18. data = {
  19. "query": prompt,
  20. "FLAG_URLEXTRACT": "false",
  21. "token": "",
  22. "FLAG_MODELA": "1",
  23. }
  24. headers = {
  25. 'authority': 'komo.ai',
  26. 'accept': 'text/event-stream',
  27. 'cache-control': 'no-cache',
  28. 'referer': 'https://komo.ai/',
  29. }
  30. async with session.get(cls.url, params=data, headers=headers) as response:
  31. response.raise_for_status()
  32. next = False
  33. async for line in response.iter_lines():
  34. if line == b"event: line":
  35. next = True
  36. elif next and line.startswith(b"data: "):
  37. yield json.loads(line[6:])
  38. next = False