1: from __future__ import annotations
2:
3:+from aiohttp import ClientSession
4: import asyncio
5: import json
6: import uuid
7: import cloudscraper
8: from typing import AsyncGenerator
9:+
10: from ..typing import AsyncResult, Messages
11: from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
12: from .helper import format_prompt
13:
14: class Cloudflare(AsyncGeneratorProvider, ProviderModelMixin):
15:+ label = "Cloudflare AI"
16: url = "https://playground.ai.cloudflare.com"
17: api_endpoint = "https://playground.ai.cloudflare.com/api/inference"
18: working = True
20: supports_system_message = True
21: supports_message_history = True
22:
- default_model = '@cf/meta/llama-3.1-8b-instruct'
- models = [
- '@cf/deepseek-ai/deepseek-math-7b-instruct', # Specific answer
-
-
- '@cf/thebloke/discolm-german-7b-v1-awq',
-
-
- '@cf/tiiuae/falcon-7b-instruct', # Specific answer
-
-
- '@hf/google/gemma-7b-it',
-
-
23:+ default_model = '@cf/meta/llama-3.1-8b-instruct-awq'
24:+ models = [
25: '@cf/meta/llama-2-7b-chat-fp16',
26: '@cf/meta/llama-2-7b-chat-int8',
27:
28: '@cf/meta/llama-3-8b-instruct',
29: '@cf/meta/llama-3-8b-instruct-awq',
- default_model,
30: '@hf/meta-llama/meta-llama-3-8b-instruct',
31:
- '@cf/meta/llama-3.1-8b-instruct-awq',
32:+ default_model,
33: '@cf/meta/llama-3.1-8b-instruct-fp8',
- '@cf/meta/llama-3.2-11b-vision-instruct',
34:+
35: '@cf/meta/llama-3.2-1b-instruct',
- '@cf/meta/llama-3.2-3b-instruct',
36:
- '@cf/mistral/mistral-7b-instruct-v0.1',
37: '@hf/mistral/mistral-7b-instruct-v0.2',
38:
- '@cf/openchat/openchat-3.5-0106',
-
- '@cf/microsoft/phi-2',
-
- '@cf/qwen/qwen1.5-0.5b-chat',
- '@cf/qwen/qwen1.5-1.8b-chat',
- '@cf/qwen/qwen1.5-14b-chat-awq',
- '@cf/qwen/qwen1.5-7b-chat-awq',
-
- '@cf/defog/sqlcoder-7b-2', # Specific answer
-
- '@cf/tinyllama/tinyllama-1.1b-chat-v1.0',
39:+ '@cf/qwen/qwen1.5-7b-chat-awq',
40:
- '@cf/fblgit/una-cybertron-7b-v2-bf16',
41:+ '@cf/defog/sqlcoder-7b-2',
42: ]
43:
44: model_aliases = {
- "german-7b-v1": "@cf/thebloke/discolm-german-7b-v1-awq",
-
-
- "gemma-7b": "@hf/google/gemma-7b-it",
-
-
45: "llama-2-7b": "@cf/meta/llama-2-7b-chat-fp16",
46: "llama-2-7b": "@cf/meta/llama-2-7b-chat-int8",
47:
48: "llama-3-8b": "@cf/meta/llama-3-8b-instruct",
49: "llama-3-8b": "@cf/meta/llama-3-8b-instruct-awq",
- "llama-3-8b": "@cf/meta/llama-3.1-8b-instruct",
50: "llama-3-8b": "@hf/meta-llama/meta-llama-3-8b-instruct",
51:
52: "llama-3.1-8b": "@cf/meta/llama-3.1-8b-instruct-awq",
53: "llama-3.1-8b": "@cf/meta/llama-3.1-8b-instruct-fp8",
- "llama-3.1-8b": "@cf/meta/llama-3.1-8b-instruct-fp8",
54:
- "llama-3.2-11b": "@cf/meta/llama-3.2-11b-vision-instruct",
55: "llama-3.2-1b": "@cf/meta/llama-3.2-1b-instruct",
- "llama-3.2-3b": "@cf/meta/llama-3.2-3b-instruct",
56:
-
- "mistral-7b": "@cf/mistral/mistral-7b-instruct-v0.1",
- "mistral-7b": "@hf/mistral/mistral-7b-instruct-v0.2",
-
-
- "openchat-3.5": "@cf/openchat/openchat-3.5-0106",
-
-
- "phi-2": "@cf/microsoft/phi-2",
-
-
- "qwen-1.5-0.5b": "@cf/qwen/qwen1.5-0.5b-chat",
- "qwen-1.5-1.8b": "@cf/qwen/qwen1.5-1.8b-chat",
- "qwen-1.5-14b": "@cf/qwen/qwen1.5-14b-chat-awq",
57: "qwen-1.5-7b": "@cf/qwen/qwen1.5-7b-chat-awq",
58:
-
- "tinyllama-1.1b": "@cf/tinyllama/tinyllama-1.1b-chat-v1.0",
-
-
- "cybertron-7b": "@cf/fblgit/una-cybertron-7b-v2-bf16",
59:+ #"sqlcoder-7b": "@cf/defog/sqlcoder-7b-2",
60: }
61:
62: @classmethod
74: model: str,
75: messages: Messages,
76: proxy: str = None,
- max_tokens: str = 2048,
- stream: bool = True,
77:+ max_tokens: int = 2048,
78: **kwargs
79: ) -> AsyncResult:
80: model = cls.get_model(model)
102:
103: scraper = cloudscraper.create_scraper()
104:
- prompt = format_prompt(messages)
105: data = {
106: "messages": [
- {"role": "system", "content": "You are a helpful assistant"},
- {"role": "user", "content": prompt}
107:+ {"role": "user", "content": format_prompt(messages)}
108: ],
109: "lora": None,
110: "model": model,
111: "max_tokens": max_tokens,
- "stream": stream
112:+ "stream": True
113: }
114:
115: max_retries = 3
116:+ full_response = ""
117:+
118: for attempt in range(max_retries):
119: try:
120: response = scraper.post(
129: if response.status_code == 403:
130: await asyncio.sleep(2 ** attempt)
131: continue
-
132:+
133: response.raise_for_status()
134:
135: for line in response.iter_lines():
136: if line.startswith(b'data: '):
137: if line == b'data: [DONE]':
138:+ if full_response:
139:+ yield full_response
140: break
141: try:
- content = json.loads(line[6:].decode('utf-8'))['response']
- yield content
142:+ content = json.loads(line[6:].decode('utf-8'))
143:+ if 'response' in content and content['response'] != '</s>':
144:+ yield content['response']
145: except Exception:
146: continue
147: break
148: except Exception as e:
149: if attempt == max_retries - 1:
150: raise
-
- @classmethod
- async def create_async(
- cls,
- model: str,
- messages: Messages,
- proxy: str = None,
- **kwargs
- ) -> str:
- full_response = ""
- async for response in cls.create_async_generator(model, messages, proxy, **kwargs):
- full_response += response
- return full_response