Spaces:
Paused
Paused
Update backend/model_router.py
Browse files- backend/model_router.py +22 -11
backend/model_router.py
CHANGED
|
@@ -6,7 +6,7 @@ import httpx
|
|
| 6 |
groq_keys = os.environ.get("GROQ_API_KEYS", "").split(",")
|
| 7 |
openrouter_keys = os.environ.get("OPENROUTER_KEYS", "").split(",")
|
| 8 |
|
| 9 |
-
POLLINATIONS_URL = "https://text.pollinations.ai/"
|
| 10 |
|
| 11 |
# Helper that creates an AsyncOpenAI client ignoring environment proxies
|
| 12 |
def _make_client(base_url: str, api_key: str) -> AsyncOpenAI:
|
|
@@ -49,12 +49,20 @@ async def route_chat(model: str, messages: List[Dict]) -> AsyncGenerator[str, No
|
|
| 49 |
if chunk.choices[0].delta.content:
|
| 50 |
yield chunk.choices[0].delta.content
|
| 51 |
elif model == "free-pollinations":
|
| 52 |
-
|
| 53 |
async with aiohttp.ClientSession() as session:
|
| 54 |
-
async with session.post(
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
else:
|
| 59 |
raise ValueError(f"Unknown model: {model}")
|
| 60 |
|
|
@@ -73,12 +81,15 @@ async def route_autocomplete(model: str, prefix: str, suffix: str, max_tokens: i
|
|
| 73 |
|
| 74 |
def get_available_models():
|
| 75 |
return [
|
| 76 |
-
# Groq models (currently supported)
|
| 77 |
{"id": "groq-llama-3.3-70b-versatile", "name": "Groq Llama 3.3 70B", "free": False},
|
| 78 |
{"id": "groq-llama-3.1-8b-instant", "name": "Groq Llama 3.1 8B (Instant)", "free": False},
|
| 79 |
-
{"id": "groq-
|
| 80 |
-
# OpenRouter models
|
| 81 |
-
{"id": "openrouter-anthropic/claude-3-
|
| 82 |
-
{"id": "openrouter-meta-llama/llama-3-
|
|
|
|
|
|
|
|
|
|
| 83 |
{"id": "free-pollinations", "name": "Free Pollinations (GPT-4o-mini)", "free": True},
|
| 84 |
]
|
|
|
|
| 6 |
groq_keys = os.environ.get("GROQ_API_KEYS", "").split(",")
|
| 7 |
openrouter_keys = os.environ.get("OPENROUTER_KEYS", "").split(",")
|
| 8 |
|
| 9 |
+
POLLINATIONS_URL = "https://text.pollinations.ai/openai" # correct endpoint
|
| 10 |
|
| 11 |
# Helper that creates an AsyncOpenAI client ignoring environment proxies
|
| 12 |
def _make_client(base_url: str, api_key: str) -> AsyncOpenAI:
|
|
|
|
| 49 |
if chunk.choices[0].delta.content:
|
| 50 |
yield chunk.choices[0].delta.content
|
| 51 |
elif model == "free-pollinations":
|
| 52 |
+
# Pollinations expects OpenAI-format chat messages
|
| 53 |
async with aiohttp.ClientSession() as session:
|
| 54 |
+
async with session.post(
|
| 55 |
+
POLLINATIONS_URL,
|
| 56 |
+
json={"messages": messages, "model": "openai"},
|
| 57 |
+
headers={"Content-Type": "application/json"},
|
| 58 |
+
) as resp:
|
| 59 |
+
if resp.status != 200:
|
| 60 |
+
text = await resp.text()
|
| 61 |
+
raise Exception(f"Pollinations error {resp.status}: {text}")
|
| 62 |
+
# Non-streaming response (simple)
|
| 63 |
+
data = await resp.json()
|
| 64 |
+
content = data.get("choices", [{}])[0].get("message", {}).get("content", "")
|
| 65 |
+
yield content
|
| 66 |
else:
|
| 67 |
raise ValueError(f"Unknown model: {model}")
|
| 68 |
|
|
|
|
| 81 |
|
| 82 |
def get_available_models():
|
| 83 |
return [
|
| 84 |
+
# Groq models (currently supported as of 2025)
|
| 85 |
{"id": "groq-llama-3.3-70b-versatile", "name": "Groq Llama 3.3 70B", "free": False},
|
| 86 |
{"id": "groq-llama-3.1-8b-instant", "name": "Groq Llama 3.1 8B (Instant)", "free": False},
|
| 87 |
+
{"id": "groq-qwen-2.5-32b", "name": "Groq Qwen 2.5 32B", "free": False},
|
| 88 |
+
# OpenRouter models (verified working IDs)
|
| 89 |
+
{"id": "openrouter-anthropic/claude-3-sonnet", "name": "Claude 3 Sonnet (OpenRouter)", "free": False},
|
| 90 |
+
{"id": "openrouter-meta-llama/llama-3-8b-instruct", "name": "Llama 3 8B (OpenRouter)", "free": False},
|
| 91 |
+
{"id": "openrouter-openai/gpt-3.5-turbo", "name": "GPT-3.5 Turbo (OpenRouter)", "free": False},
|
| 92 |
+
{"id": "openrouter-google/gemma-2-9b-it", "name": "Gemma 2 9B (OpenRouter)", "free": False},
|
| 93 |
+
# Free Pollinations
|
| 94 |
{"id": "free-pollinations", "name": "Free Pollinations (GPT-4o-mini)", "free": True},
|
| 95 |
]
|