Cerebras.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. from __future__ import annotations
  2. from aiohttp import ClientSession
  3. from .OpenaiAPI import OpenaiAPI
  4. from ...typing import AsyncResult, Messages, Cookies
  5. from ...requests.raise_for_status import raise_for_status
  6. from ...cookies import get_cookies
  7. class Cerebras(OpenaiAPI):
  8. label = "Cerebras Inference"
  9. url = "https://inference.cerebras.ai/"
  10. login_url = "https://cloud.cerebras.ai"
  11. api_base = "https://api.cerebras.ai/v1"
  12. working = True
  13. default_model = "llama3.1-70b"
  14. models = [
  15. "llama3.1-70b",
  16. "llama3.1-8b",
  17. "llama-3.3-70b"
  18. ]
  19. model_aliases = {"llama-3.1-70b": "llama3.1-70b", "llama-3.1-8b": "llama3.1-8b"}
  20. @classmethod
  21. async def create_async_generator(
  22. cls,
  23. model: str,
  24. messages: Messages,
  25. api_key: str = None,
  26. cookies: Cookies = None,
  27. **kwargs
  28. ) -> AsyncResult:
  29. if api_key is None:
  30. if cookies is None:
  31. cookies = get_cookies(".cerebras.ai")
  32. async with ClientSession(cookies=cookies) as session:
  33. async with session.get("https://inference.cerebras.ai/api/auth/session") as response:
  34. await raise_for_status(response)
  35. data = await response.json()
  36. if data:
  37. api_key = data.get("user", {}).get("demoApiKey")
  38. async for chunk in super().create_async_generator(
  39. model, messages,
  40. impersonate="chrome",
  41. api_key=api_key,
  42. headers={
  43. "User-Agent": "ex/JS 1.5.0",
  44. },
  45. **kwargs
  46. ):
  47. yield chunk