Cloudflare.py 4.4 KB

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