Cachinde commited on
Commit ·
ce9dd03
1
Parent(s): 9fdf3e0
feat: provider Cerebras (free tier generoso) na hierarquia apos o local
Browse files- .env.example +1 -0
- README.md +4 -1
- ai_engine.py +7 -1
- config.py +4 -0
.env.example
CHANGED
|
@@ -5,6 +5,7 @@ AI_MODEL=
|
|
| 5 |
|
| 6 |
OPENROUTER_API_KEY=
|
| 7 |
GROQ_API_KEY=
|
|
|
|
| 8 |
DEEPSEEK_API_KEY=
|
| 9 |
MISTRAL_API_KEY=
|
| 10 |
GEMINI_API_KEY=
|
|
|
|
| 5 |
|
| 6 |
OPENROUTER_API_KEY=
|
| 7 |
GROQ_API_KEY=
|
| 8 |
+
CEREBRAS_API_KEY=
|
| 9 |
DEEPSEEK_API_KEY=
|
| 10 |
MISTRAL_API_KEY=
|
| 11 |
GEMINI_API_KEY=
|
README.md
CHANGED
|
@@ -22,8 +22,11 @@ A anotação @spaces.GPU está configurada para suportar inferência rápida.
|
|
| 22 |
|
| 23 |
## Hierarquia de provedores (fallback)
|
| 24 |
|
| 25 |
-
1. Llama local (ZeroGPU) → 2.
|
| 26 |
Cada nível que falha cai para o seguinte; `AI_PROVIDER` força um específico primeiro.
|
|
|
|
|
|
|
|
|
|
| 27 |
O Llama local (`local_llm.py`, `NousResearch/Meta-Llama-3-8B-Instruct` por defeito,
|
| 28 |
LoRA opcional via `LORA_ADAPTER_PATH` — pasta local ou id do Hub, ex. `user/meu-lora`,
|
| 29 |
com download automático e confirmação de adaptadores ativos no log) é carregado
|
|
|
|
| 22 |
|
| 23 |
## Hierarquia de provedores (fallback)
|
| 24 |
|
| 25 |
+
1. Llama local (ZeroGPU) → 2. Cerebras → 3. OpenRouter → 4. Groq → 5. DeepSeek → 6. Mistral → 7. Gemini.
|
| 26 |
Cada nível que falha cai para o seguinte; `AI_PROVIDER` força um específico primeiro.
|
| 27 |
+
Provedores sem chave são saltados. Cerebras tem free tier generoso (1M tokens/dia,
|
| 28 |
+
sem cartão; contexto 8K no free): cria a chave em cloud.cerebras.ai e define
|
| 29 |
+
`CEREBRAS_API_KEY`. Modelos OpenRouter com `:free` também valem como degrau gratuito.
|
| 30 |
O Llama local (`local_llm.py`, `NousResearch/Meta-Llama-3-8B-Instruct` por defeito,
|
| 31 |
LoRA opcional via `LORA_ADAPTER_PATH` — pasta local ou id do Hub, ex. `user/meu-lora`,
|
| 32 |
com download automático e confirmação de adaptadores ativos no log) é carregado
|
ai_engine.py
CHANGED
|
@@ -163,7 +163,7 @@ class AIEngine:
|
|
| 163 |
|
| 164 |
# Nivel de Hierarquia (Fallback)
|
| 165 |
# 1. Llama Local (ZeroGPU) -> 2. OpenRouter -> 3. Groq -> 4. DeepSeek -> 5. Gemini
|
| 166 |
-
ordem_provedores = ["local_llama", "openrouter", "groq", "deepseek", "mistral", "gemini"]
|
| 167 |
|
| 168 |
forced_provider = Config.AI_PROVIDER
|
| 169 |
if forced_provider and forced_provider != "auto":
|
|
@@ -179,6 +179,7 @@ class AIEngine:
|
|
| 179 |
# Saltar provedores se a respectiva chave de seguranca nao existir
|
| 180 |
if provider == "openrouter" and not Config.OPENROUTER_API_KEY: continue
|
| 181 |
if provider == "groq" and not Config.GROQ_API_KEY: continue
|
|
|
|
| 182 |
if provider == "deepseek" and not Config.DEEPSEEK_API_KEY: continue
|
| 183 |
if provider == "mistral" and not Config.MISTRAL_API_KEY: continue
|
| 184 |
if provider == "gemini" and not Config.GEMINI_API_KEY: continue
|
|
@@ -273,6 +274,11 @@ class AIEngine:
|
|
| 273 |
"Authorization": f"Bearer {Config.GROQ_API_KEY}",
|
| 274 |
"Content-Type": "application/json",
|
| 275 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 276 |
if provider == "deepseek":
|
| 277 |
return "https://api.deepseek.com/chat/completions", {
|
| 278 |
"Authorization": f"Bearer {Config.DEEPSEEK_API_KEY}",
|
|
|
|
| 163 |
|
| 164 |
# Nivel de Hierarquia (Fallback)
|
| 165 |
# 1. Llama Local (ZeroGPU) -> 2. OpenRouter -> 3. Groq -> 4. DeepSeek -> 5. Gemini
|
| 166 |
+
ordem_provedores = ["local_llama", "cerebras", "openrouter", "groq", "deepseek", "mistral", "gemini"]
|
| 167 |
|
| 168 |
forced_provider = Config.AI_PROVIDER
|
| 169 |
if forced_provider and forced_provider != "auto":
|
|
|
|
| 179 |
# Saltar provedores se a respectiva chave de seguranca nao existir
|
| 180 |
if provider == "openrouter" and not Config.OPENROUTER_API_KEY: continue
|
| 181 |
if provider == "groq" and not Config.GROQ_API_KEY: continue
|
| 182 |
+
if provider == "cerebras" and not Config.CEREBRAS_API_KEY: continue
|
| 183 |
if provider == "deepseek" and not Config.DEEPSEEK_API_KEY: continue
|
| 184 |
if provider == "mistral" and not Config.MISTRAL_API_KEY: continue
|
| 185 |
if provider == "gemini" and not Config.GEMINI_API_KEY: continue
|
|
|
|
| 274 |
"Authorization": f"Bearer {Config.GROQ_API_KEY}",
|
| 275 |
"Content-Type": "application/json",
|
| 276 |
}
|
| 277 |
+
if provider == "cerebras":
|
| 278 |
+
return "https://api.cerebras.ai/v1/chat/completions", {
|
| 279 |
+
"Authorization": f"Bearer {Config.CEREBRAS_API_KEY}",
|
| 280 |
+
"Content-Type": "application/json",
|
| 281 |
+
}
|
| 282 |
if provider == "deepseek":
|
| 283 |
return "https://api.deepseek.com/chat/completions", {
|
| 284 |
"Authorization": f"Bearer {Config.DEEPSEEK_API_KEY}",
|
config.py
CHANGED
|
@@ -12,6 +12,7 @@ class Config:
|
|
| 12 |
MISTRAL_API_KEY = os.getenv("MISTRAL_API_KEY", "").strip()
|
| 13 |
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY", "").strip()
|
| 14 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY", "").strip()
|
|
|
|
| 15 |
|
| 16 |
API_AUTH_TOKEN = os.getenv("API_AUTH_TOKEN", "").strip()
|
| 17 |
|
|
@@ -134,6 +135,7 @@ Notas de preço
|
|
| 134 |
"deepseek": "deepseek-chat",
|
| 135 |
"mistral": "mistral-small-latest",
|
| 136 |
"groq": "llama-3.3-70b-versatile",
|
|
|
|
| 137 |
"gemini": "gemini-2.0-flash",
|
| 138 |
}
|
| 139 |
|
|
@@ -145,6 +147,8 @@ Notas de preço
|
|
| 145 |
return "openrouter"
|
| 146 |
if cls.GROQ_API_KEY:
|
| 147 |
return "groq"
|
|
|
|
|
|
|
| 148 |
if cls.DEEPSEEK_API_KEY:
|
| 149 |
return "deepseek"
|
| 150 |
if cls.MISTRAL_API_KEY:
|
|
|
|
| 12 |
MISTRAL_API_KEY = os.getenv("MISTRAL_API_KEY", "").strip()
|
| 13 |
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY", "").strip()
|
| 14 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY", "").strip()
|
| 15 |
+
CEREBRAS_API_KEY = os.getenv("CEREBRAS_API_KEY", "").strip()
|
| 16 |
|
| 17 |
API_AUTH_TOKEN = os.getenv("API_AUTH_TOKEN", "").strip()
|
| 18 |
|
|
|
|
| 135 |
"deepseek": "deepseek-chat",
|
| 136 |
"mistral": "mistral-small-latest",
|
| 137 |
"groq": "llama-3.3-70b-versatile",
|
| 138 |
+
"cerebras": "llama-3.3-70b",
|
| 139 |
"gemini": "gemini-2.0-flash",
|
| 140 |
}
|
| 141 |
|
|
|
|
| 147 |
return "openrouter"
|
| 148 |
if cls.GROQ_API_KEY:
|
| 149 |
return "groq"
|
| 150 |
+
if cls.CEREBRAS_API_KEY:
|
| 151 |
+
return "cerebras"
|
| 152 |
if cls.DEEPSEEK_API_KEY:
|
| 153 |
return "deepseek"
|
| 154 |
if cls.MISTRAL_API_KEY:
|