Update coding_priority_pipe.py
Browse files- coding_priority_pipe.py +93 -41
coding_priority_pipe.py
CHANGED
|
@@ -2,33 +2,36 @@
|
|
| 2 |
title: Coding Fallback Pipe
|
| 3 |
author: nerdur
|
| 4 |
author_url: https://nerdur-webui.hf.space
|
| 5 |
-
version:
|
| 6 |
-
description:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
"""
|
| 8 |
|
| 9 |
from pydantic import BaseModel
|
| 10 |
-
from typing import Optional, List, Union,
|
| 11 |
import requests
|
| 12 |
-
import
|
| 13 |
|
| 14 |
|
| 15 |
class Pipe:
|
| 16 |
class Valves(BaseModel):
|
| 17 |
-
# NVIDIA NIM Free Endpoint coding modeli — prioritet od najboljeg ka rezervnom
|
| 18 |
-
model_priority: List[str] = [
|
| 19 |
-
"mistral-ai/devstral-2-123b-instruct-2512",
|
| 20 |
-
"deepseek-ai/deepseek-v3.2",
|
| 21 |
-
"qwen/qwen3-coder-480b-a35b-instruct",
|
| 22 |
-
"zhipuai/glm-4.7",
|
| 23 |
-
]
|
| 24 |
nvidia_api_key: str = ""
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
| 26 |
enabled: bool = True
|
|
|
|
| 27 |
|
| 28 |
def __init__(self):
|
| 29 |
self.type = "pipe"
|
| 30 |
self.id = "coding_fallback"
|
| 31 |
-
self.name = "Smart Coding Router (
|
| 32 |
self.valves = self.Valves()
|
| 33 |
|
| 34 |
def pipes(self):
|
|
@@ -38,65 +41,114 @@ class Pipe:
|
|
| 38 |
if not self.valves.enabled:
|
| 39 |
return "Router je isključen."
|
| 40 |
|
| 41 |
-
import os
|
| 42 |
-
api_key = self.valves.nvidia_api_key or os.getenv("NVIDIA_API_KEY") or os.getenv("NVIDIA_ID_API_KEY", "")
|
| 43 |
-
|
| 44 |
-
if not api_key:
|
| 45 |
-
return "❌ NVIDIA_API_KEY nije postavljen."
|
| 46 |
-
|
| 47 |
messages = body.get("messages", [])
|
| 48 |
stream = body.get("stream", False)
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
last_error = None
|
| 56 |
-
for model_id in
|
| 57 |
payload = {
|
| 58 |
"model": model_id,
|
| 59 |
"messages": messages,
|
| 60 |
"stream": stream,
|
| 61 |
-
"max_tokens":
|
| 62 |
-
"temperature":
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
}
|
| 64 |
|
| 65 |
try:
|
| 66 |
-
print(f"Coding Router: Pokušavam {model_id}...")
|
| 67 |
response = requests.post(
|
| 68 |
-
f"{
|
| 69 |
headers=headers,
|
| 70 |
json=payload,
|
| 71 |
stream=stream,
|
| 72 |
-
timeout=
|
| 73 |
)
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
continue
|
| 80 |
|
| 81 |
response.raise_for_status()
|
| 82 |
|
| 83 |
if stream:
|
| 84 |
-
def generate():
|
| 85 |
-
|
|
|
|
| 86 |
if line:
|
| 87 |
yield line.decode("utf-8") + "\n"
|
| 88 |
return generate()
|
| 89 |
else:
|
| 90 |
data = response.json()
|
| 91 |
-
|
|
|
|
|
|
|
| 92 |
|
| 93 |
except requests.exceptions.Timeout:
|
| 94 |
-
print(f"⚠️ {model_id} timeout, prelazim...")
|
| 95 |
-
last_error = f"{model_id}: timeout"
|
| 96 |
continue
|
| 97 |
except Exception as e:
|
| 98 |
-
print(f"⚠️ {model_id} greška: {e}")
|
| 99 |
last_error = str(e)
|
| 100 |
continue
|
| 101 |
|
| 102 |
-
return f"❌ Svi
|
|
|
|
| 2 |
title: Coding Fallback Pipe
|
| 3 |
author: nerdur
|
| 4 |
author_url: https://nerdur-webui.hf.space
|
| 5 |
+
version: 3.0
|
| 6 |
+
description: |
|
| 7 |
+
Fallback redoslijed za kodiranje:
|
| 8 |
+
1. NVIDIA NIM (4 modela)
|
| 9 |
+
2. Gemini 2.5 Flash
|
| 10 |
+
3. Groq (besplatni coding modeli)
|
| 11 |
+
4. Cerebras
|
| 12 |
+
5. SambaNova
|
| 13 |
"""
|
| 14 |
|
| 15 |
from pydantic import BaseModel
|
| 16 |
+
from typing import Optional, List, Union, Iterator
|
| 17 |
import requests
|
| 18 |
+
import os
|
| 19 |
|
| 20 |
|
| 21 |
class Pipe:
|
| 22 |
class Valves(BaseModel):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
nvidia_api_key: str = ""
|
| 24 |
+
google_api_key: str = ""
|
| 25 |
+
groq_api_key: str = ""
|
| 26 |
+
cerebras_api_key: str = ""
|
| 27 |
+
sambanova_api_key: str = ""
|
| 28 |
enabled: bool = True
|
| 29 |
+
request_timeout: int = 45
|
| 30 |
|
| 31 |
def __init__(self):
|
| 32 |
self.type = "pipe"
|
| 33 |
self.id = "coding_fallback"
|
| 34 |
+
self.name = "🔀 Smart Coding Router (Multi-Provider)"
|
| 35 |
self.valves = self.Valves()
|
| 36 |
|
| 37 |
def pipes(self):
|
|
|
|
| 41 |
if not self.valves.enabled:
|
| 42 |
return "Router je isključen."
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
messages = body.get("messages", [])
|
| 45 |
stream = body.get("stream", False)
|
| 46 |
+
max_tokens = body.get("max_tokens", 4096)
|
| 47 |
+
temperature = body.get("temperature", 0.2)
|
| 48 |
+
|
| 49 |
+
# --- Redoslijed providera ---
|
| 50 |
+
# Svaki entry: (provider_name, base_url, model_id, api_key)
|
| 51 |
+
nvidia_key = self.valves.nvidia_api_key or os.getenv("NVIDIA_ID_API_KEY") or os.getenv("NVIDIA_API_KEY", "")
|
| 52 |
+
google_key = self.valves.google_api_key or os.getenv("GOOGLE_API_KEY", "")
|
| 53 |
+
groq_key = self.valves.groq_api_key or os.getenv("GROQ_API_KEY", "")
|
| 54 |
+
cerebras_key = self.valves.cerebras_api_key or os.getenv("CEREBRAS_API_KEY", "")
|
| 55 |
+
sambanova_key = self.valves.sambanova_api_key or os.getenv("SAMBANOVA_API_KEY", "")
|
| 56 |
+
|
| 57 |
+
providers = []
|
| 58 |
+
|
| 59 |
+
# 1. NVIDIA NIM — 4 coding modela
|
| 60 |
+
if nvidia_key:
|
| 61 |
+
for model in [
|
| 62 |
+
"mistral-ai/devstral-2-123b-instruct-2512",
|
| 63 |
+
"deepseek-ai/deepseek-v3.2",
|
| 64 |
+
"qwen/qwen3-coder-480b-a35b-instruct",
|
| 65 |
+
"zhipuai/glm-4.7",
|
| 66 |
+
]:
|
| 67 |
+
providers.append(("NVIDIA", "https://integrate.api.nvidia.com/v1", model, nvidia_key))
|
| 68 |
+
|
| 69 |
+
# 2. Gemini 2.5 Flash
|
| 70 |
+
if google_key:
|
| 71 |
+
providers.append(("Gemini", "https://generativelanguage.googleapis.com/v1beta/openai", "gemini-2.5-flash", google_key))
|
| 72 |
+
|
| 73 |
+
# 3. Groq — besplatni coding modeli
|
| 74 |
+
if groq_key:
|
| 75 |
+
for model in [
|
| 76 |
+
"moonshotai/kimi-k2-instruct",
|
| 77 |
+
"deepseek-r1-distill-llama-70b",
|
| 78 |
+
"llama-3.3-70b-versatile",
|
| 79 |
+
]:
|
| 80 |
+
providers.append(("Groq", "https://api.groq.com/openai/v1", model, groq_key))
|
| 81 |
+
|
| 82 |
+
# 4. Cerebras
|
| 83 |
+
if cerebras_key:
|
| 84 |
+
providers.append(("Cerebras", "https://api.cerebras.ai/v1", "llama-3.3-70b", cerebras_key))
|
| 85 |
+
|
| 86 |
+
# 5. SambaNova
|
| 87 |
+
if sambanova_key:
|
| 88 |
+
providers.append(("SambaNova", "https://api.sambanova.ai/v1", "Meta-Llama-3.3-70B-Instruct", sambanova_key))
|
| 89 |
+
|
| 90 |
+
if not providers:
|
| 91 |
+
return "❌ Nijedan API ključ nije postavljen. Provjeri Space Secrets."
|
| 92 |
|
| 93 |
last_error = None
|
| 94 |
+
for provider_name, base_url, model_id, api_key in providers:
|
| 95 |
payload = {
|
| 96 |
"model": model_id,
|
| 97 |
"messages": messages,
|
| 98 |
"stream": stream,
|
| 99 |
+
"max_tokens": max_tokens,
|
| 100 |
+
"temperature": temperature,
|
| 101 |
+
}
|
| 102 |
+
headers = {
|
| 103 |
+
"Authorization": f"Bearer {api_key}",
|
| 104 |
+
"Content-Type": "application/json",
|
| 105 |
}
|
| 106 |
|
| 107 |
try:
|
| 108 |
+
print(f"🔀 Coding Router [{provider_name}]: Pokušavam {model_id}...")
|
| 109 |
response = requests.post(
|
| 110 |
+
f"{base_url}/chat/completions",
|
| 111 |
headers=headers,
|
| 112 |
json=payload,
|
| 113 |
stream=stream,
|
| 114 |
+
timeout=self.valves.request_timeout,
|
| 115 |
)
|
| 116 |
|
| 117 |
+
if response.status_code in (429, 500, 502, 503, 529):
|
| 118 |
+
print(f"⚠️ [{provider_name}] {model_id} → HTTP {response.status_code}, prelazim...")
|
| 119 |
+
last_error = f"[{provider_name}] {model_id}: HTTP {response.status_code}"
|
| 120 |
+
continue
|
| 121 |
+
|
| 122 |
+
if response.status_code == 401:
|
| 123 |
+
print(f"⚠️ [{provider_name}] Neispravan API ključ, preskačem providera...")
|
| 124 |
+
last_error = f"[{provider_name}]: 401 Unauthorized"
|
| 125 |
+
# Preskoči sve modele ovog providera
|
| 126 |
+
current_provider = provider_name
|
| 127 |
+
providers_iter = [(p, b, m, k) for p, b, m, k in providers if p != current_provider]
|
| 128 |
continue
|
| 129 |
|
| 130 |
response.raise_for_status()
|
| 131 |
|
| 132 |
if stream:
|
| 133 |
+
def generate(resp=response, pname=provider_name, mid=model_id):
|
| 134 |
+
print(f"✅ [{pname}] Streaming: {mid}")
|
| 135 |
+
for line in resp.iter_lines():
|
| 136 |
if line:
|
| 137 |
yield line.decode("utf-8") + "\n"
|
| 138 |
return generate()
|
| 139 |
else:
|
| 140 |
data = response.json()
|
| 141 |
+
content = data["choices"][0]["message"]["content"]
|
| 142 |
+
print(f"✅ [{provider_name}] Odgovor od: {model_id}")
|
| 143 |
+
return content
|
| 144 |
|
| 145 |
except requests.exceptions.Timeout:
|
| 146 |
+
print(f"⚠️ [{provider_name}] {model_id} timeout ({self.valves.request_timeout}s), prelazim...")
|
| 147 |
+
last_error = f"[{provider_name}] {model_id}: timeout"
|
| 148 |
continue
|
| 149 |
except Exception as e:
|
| 150 |
+
print(f"⚠️ [{provider_name}] {model_id} greška: {e}")
|
| 151 |
last_error = str(e)
|
| 152 |
continue
|
| 153 |
|
| 154 |
+
return f"❌ Svi provideri neuspješni.\nPosljednja greška: {last_error}"
|