Spaces:
Sleeping
Sleeping
Update model support:
Browse files- add meta-llama provider
- add support for llama models
- add support for HF inference providers
- setup.py +4 -1
- src/llm/__init__.py +2 -0
- src/llm/meta.py +47 -0
setup.py
CHANGED
|
@@ -14,7 +14,7 @@ _ENV_KEY_MAP = {
|
|
| 14 |
"gemini": "GEMINI_API_KEY",
|
| 15 |
}
|
| 16 |
|
| 17 |
-
_PROVIDER_CHOICES = {"1": "openai", "2": "anthropic", "3": "gemini"}
|
| 18 |
|
| 19 |
|
| 20 |
def generate_config(
|
|
@@ -40,6 +40,7 @@ def generate_config(
|
|
| 40 |
"openai": "",
|
| 41 |
"anthropic": "",
|
| 42 |
"gemini": "",
|
|
|
|
| 43 |
},
|
| 44 |
"embeddings": {
|
| 45 |
"provider": "local",
|
|
@@ -136,6 +137,7 @@ def run_wizard():
|
|
| 136 |
print(" 1) OpenAI")
|
| 137 |
print(" 2) Anthropic")
|
| 138 |
print(" 3) Google Gemini")
|
|
|
|
| 139 |
provider_choice = input("Choose [1]: ").strip() or "1"
|
| 140 |
provider = _PROVIDER_CHOICES.get(provider_choice, "openai")
|
| 141 |
|
|
@@ -156,6 +158,7 @@ def run_wizard():
|
|
| 156 |
"openai": ["gpt-4.1", "gpt-4.1-mini", "gpt-4.1-nano"],
|
| 157 |
"anthropic": ["claude-sonnet-4-6", "claude-haiku-4-5", "claude-opus-4-6"],
|
| 158 |
"gemini": ["gemini-2.5-flash", "gemini-2.5-pro", "gemini-2.0-flash"],
|
|
|
|
| 159 |
}
|
| 160 |
models = fallback.get(provider, ["default-model"])
|
| 161 |
|
|
|
|
| 14 |
"gemini": "GEMINI_API_KEY",
|
| 15 |
}
|
| 16 |
|
| 17 |
+
_PROVIDER_CHOICES = {"1": "openai", "2": "anthropic", "3": "gemini", "4": "meta-llama"}
|
| 18 |
|
| 19 |
|
| 20 |
def generate_config(
|
|
|
|
| 40 |
"openai": "",
|
| 41 |
"anthropic": "",
|
| 42 |
"gemini": "",
|
| 43 |
+
"hugging face": "",
|
| 44 |
},
|
| 45 |
"embeddings": {
|
| 46 |
"provider": "local",
|
|
|
|
| 137 |
print(" 1) OpenAI")
|
| 138 |
print(" 2) Anthropic")
|
| 139 |
print(" 3) Google Gemini")
|
| 140 |
+
print(" 4) Meta Llama")
|
| 141 |
provider_choice = input("Choose [1]: ").strip() or "1"
|
| 142 |
provider = _PROVIDER_CHOICES.get(provider_choice, "openai")
|
| 143 |
|
|
|
|
| 158 |
"openai": ["gpt-4.1", "gpt-4.1-mini", "gpt-4.1-nano"],
|
| 159 |
"anthropic": ["claude-sonnet-4-6", "claude-haiku-4-5", "claude-opus-4-6"],
|
| 160 |
"gemini": ["gemini-2.5-flash", "gemini-2.5-pro", "gemini-2.0-flash"],
|
| 161 |
+
"meta": ["Llama-3.3-70B-Instruct"],
|
| 162 |
}
|
| 163 |
models = fallback.get(provider, ["default-model"])
|
| 164 |
|
src/llm/__init__.py
CHANGED
|
@@ -2,12 +2,14 @@
|
|
| 2 |
from src.llm import openai as _openai
|
| 3 |
from src.llm import anthropic as _anthropic
|
| 4 |
from src.llm import gemini as _gemini
|
|
|
|
| 5 |
from src.config_loader import get_api_key
|
| 6 |
|
| 7 |
PROVIDERS = {
|
| 8 |
"openai": _openai,
|
| 9 |
"anthropic": _anthropic,
|
| 10 |
"gemini": _gemini,
|
|
|
|
| 11 |
}
|
| 12 |
|
| 13 |
def generate(system_prompt: str, user_message: str, cfg: dict,
|
|
|
|
| 2 |
from src.llm import openai as _openai
|
| 3 |
from src.llm import anthropic as _anthropic
|
| 4 |
from src.llm import gemini as _gemini
|
| 5 |
+
from src.llm import meta as _llama
|
| 6 |
from src.config_loader import get_api_key
|
| 7 |
|
| 8 |
PROVIDERS = {
|
| 9 |
"openai": _openai,
|
| 10 |
"anthropic": _anthropic,
|
| 11 |
"gemini": _gemini,
|
| 12 |
+
"meta-llama": _llama,
|
| 13 |
}
|
| 14 |
|
| 15 |
def generate(system_prompt: str, user_message: str, cfg: dict,
|
src/llm/meta.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
SUPPORTED_MODELS = ["Llama-3.3-70B-Instruct"]
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
def generate(system_prompt: str, user_message: str, api_key: str,
|
| 6 |
+
model: str = None, temperature: float = 0.0,
|
| 7 |
+
max_tokens: int = 8192) -> str:
|
| 8 |
+
from openai import OpenAI
|
| 9 |
+
|
| 10 |
+
model = model or SUPPORTED_MODELS[0]
|
| 11 |
+
client = OpenAI(base_url="https://router.huggingface.co/v1",
|
| 12 |
+
api_key=api_key,
|
| 13 |
+
default_headers={"X-HF-Bill-To": "cgiar"})
|
| 14 |
+
|
| 15 |
+
try:
|
| 16 |
+
messages = [
|
| 17 |
+
{"role": "system", "content": system_prompt},
|
| 18 |
+
{"role": "user", "content": user_message},
|
| 19 |
+
]
|
| 20 |
+
response = client.chat.completions.create(
|
| 21 |
+
model=model,
|
| 22 |
+
messages=messages,
|
| 23 |
+
temperature=temperature,
|
| 24 |
+
max_completion_tokens=max_tokens,
|
| 25 |
+
)
|
| 26 |
+
# Check for blocked responses
|
| 27 |
+
if not response.choices:
|
| 28 |
+
return ""
|
| 29 |
+
return response.choices[0].message.content or ""
|
| 30 |
+
except ValueError as e:
|
| 31 |
+
# Safety filter block — return empty so verifier treats it as generation failure
|
| 32 |
+
return ""
|
| 33 |
+
except Exception as e:
|
| 34 |
+
error_type = type(e).__name__
|
| 35 |
+
error_msg = str(e)
|
| 36 |
+
if "auth" in error_type.lower() or "authentication" in error_type.lower() or "api_key" in error_msg.lower():
|
| 37 |
+
raise RuntimeError(f"Hugging Face authentication failed. Check your API key.") from e
|
| 38 |
+
elif "rate" in error_type.lower() or "429" in error_msg:
|
| 39 |
+
raise RuntimeError(f"Hugging Face rate limit exceeded. Wait a moment and try again.") from e
|
| 40 |
+
elif "model" in error_msg.lower() or "not found" in error_msg.lower():
|
| 41 |
+
raise RuntimeError(f"Hugging Face model error: {error_msg}") from e
|
| 42 |
+
else:
|
| 43 |
+
raise RuntimeError(f"Hugging Face API error ({error_type}): {error_msg}") from e
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def list_models(api_key: str) -> list[str]:
|
| 47 |
+
return list(SUPPORTED_MODELS)
|