Koala.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. from __future__ import annotations
  2. import json
  3. from aiohttp import ClientSession
  4. from ..typing import AsyncResult, Messages
  5. from .base_provider import AsyncGeneratorProvider
  6. from .helper import get_random_string
  7. class Koala(AsyncGeneratorProvider):
  8. url = "https://koala.sh"
  9. supports_gpt_35_turbo = True
  10. supports_message_history = True
  11. working = True
  12. @classmethod
  13. async def create_async_generator(
  14. cls,
  15. model: str,
  16. messages: Messages,
  17. proxy: str = None,
  18. **kwargs
  19. ) -> AsyncResult:
  20. if not model:
  21. model = "gpt-3.5-turbo"
  22. headers = {
  23. "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/119.0",
  24. "Accept": "text/event-stream",
  25. "Accept-Language": "de,en-US;q=0.7,en;q=0.3",
  26. "Accept-Encoding": "gzip, deflate, br",
  27. "Referer": f"{cls.url}/chat",
  28. "Content-Type": "application/json",
  29. "Flag-Real-Time-Data": "false",
  30. "Visitor-ID": get_random_string(20),
  31. "Origin": cls.url,
  32. "Alt-Used": "koala.sh",
  33. "Connection": "keep-alive",
  34. "Sec-Fetch-Dest": "empty",
  35. "Sec-Fetch-Mode": "cors",
  36. "Sec-Fetch-Site": "same-origin",
  37. "Pragma": "no-cache",
  38. "Cache-Control": "no-cache",
  39. "TE": "trailers",
  40. }
  41. async with ClientSession(headers=headers) as session:
  42. data = {
  43. "input": messages[-1]["content"],
  44. "inputHistory": [
  45. message["content"]
  46. for message in messages
  47. if message["role"] == "user"
  48. ],
  49. "outputHistory": [
  50. message["content"]
  51. for message in messages
  52. if message["role"] == "assistant"
  53. ],
  54. "model": model,
  55. }
  56. async with session.post(f"{cls.url}/api/gpt/", json=data, proxy=proxy) as response:
  57. response.raise_for_status()
  58. async for chunk in response.content:
  59. if chunk.startswith(b"data: "):
  60. yield json.loads(chunk[6:])