Cloudflare.py 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. from __future__ import annotations
  2. import asyncio
  3. import json
  4. import uuid
  5. from ..typing import AsyncResult, Messages, Cookies
  6. from .base_provider import AsyncGeneratorProvider, ProviderModelMixin, get_running_loop
  7. from ..requests import Session, StreamSession, get_args_from_nodriver, raise_for_status, merge_cookies
  8. from ..errors import ResponseStatusError
  9. class Cloudflare(AsyncGeneratorProvider, ProviderModelMixin):
  10. label = "Cloudflare AI"
  11. url = "https://playground.ai.cloudflare.com"
  12. api_endpoint = "https://playground.ai.cloudflare.com/api/inference"
  13. models_url = "https://playground.ai.cloudflare.com/api/models"
  14. working = True
  15. supports_stream = True
  16. supports_system_message = True
  17. supports_message_history = True
  18. default_model = "@cf/meta/llama-3.1-8b-instruct"
  19. model_aliases = {
  20. "llama-2-7b": "@cf/meta/llama-2-7b-chat-fp16",
  21. "llama-2-7b": "@cf/meta/llama-2-7b-chat-int8",
  22. "llama-3-8b": "@cf/meta/llama-3-8b-instruct",
  23. "llama-3-8b": "@cf/meta/llama-3-8b-instruct-awq",
  24. "llama-3-8b": "@hf/meta-llama/meta-llama-3-8b-instruct",
  25. "llama-3.1-8b": "@cf/meta/llama-3.1-8b-instruct-awq",
  26. "llama-3.1-8b": "@cf/meta/llama-3.1-8b-instruct-fp8",
  27. "llama-3.2-1b": "@cf/meta/llama-3.2-1b-instruct",
  28. "qwen-1.5-7b": "@cf/qwen/qwen1.5-7b-chat-awq",
  29. }
  30. _args: dict = None
  31. @classmethod
  32. def get_models(cls) -> str:
  33. if not cls.models:
  34. if cls._args is None:
  35. get_running_loop(check_nested=True)
  36. args = get_args_from_nodriver(cls.url, cookies={
  37. '__cf_bm': uuid.uuid4().hex,
  38. })
  39. cls._args = asyncio.run(args)
  40. with Session(**cls._args) as session:
  41. response = session.get(cls.models_url)
  42. cls._args["cookies"] = merge_cookies(cls._args["cookies"] , response)
  43. try:
  44. raise_for_status(response)
  45. except ResponseStatusError as e:
  46. cls._args = None
  47. raise e
  48. json_data = response.json()
  49. cls.models = [model.get("name") for model in json_data.get("models")]
  50. return cls.models
  51. @classmethod
  52. async def create_async_generator(
  53. cls,
  54. model: str,
  55. messages: Messages,
  56. proxy: str = None,
  57. max_tokens: int = 2048,
  58. cookies: Cookies = None,
  59. timeout: int = 300,
  60. **kwargs
  61. ) -> AsyncResult:
  62. model = cls.get_model(model)
  63. if cls._args is None:
  64. cls._args = await get_args_from_nodriver(cls.url, proxy, timeout, cookies)
  65. data = {
  66. "messages": messages,
  67. "lora": None,
  68. "model": model,
  69. "max_tokens": max_tokens,
  70. "stream": True
  71. }
  72. async with StreamSession(**cls._args) as session:
  73. async with session.post(
  74. cls.api_endpoint,
  75. json=data,
  76. ) as response:
  77. cls._args["cookies"] = merge_cookies(cls._args["cookies"] , response)
  78. try:
  79. await raise_for_status(response)
  80. except ResponseStatusError as e:
  81. cls._args = None
  82. raise e
  83. async for line in response.iter_lines():
  84. if line.startswith(b'data: '):
  85. if line == b'data: [DONE]':
  86. break
  87. try:
  88. content = json.loads(line[6:].decode())
  89. if content.get("response") and content.get("response") != '</s>':
  90. yield content['response']
  91. except Exception:
  92. continue