Add NVIDIA NIM provider (DeepSeek V4 Pro, GLM) — free tier
Browse files- scripts/llm_solver_cloud.py +54 -16
scripts/llm_solver_cloud.py
CHANGED
|
@@ -3,26 +3,29 @@ PEMF ARC-AGI — LLM Program Synthesis (Multi-Provider)
|
|
| 3 |
=====================================================
|
| 4 |
|
| 5 |
Supports:
|
| 6 |
-
-
|
| 7 |
-
-
|
| 8 |
-
-
|
|
|
|
| 9 |
- Ollama local (any model)
|
| 10 |
-
- Any OpenAI-compatible API
|
| 11 |
|
| 12 |
Usage:
|
| 13 |
-
#
|
| 14 |
-
export LLM_PROVIDER=
|
| 15 |
-
export
|
| 16 |
python llm_solver_cloud.py
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
export LLM_PROVIDER=
|
| 20 |
-
export
|
|
|
|
| 21 |
python llm_solver_cloud.py
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
export LLM_PROVIDER=
|
| 25 |
-
export
|
| 26 |
python llm_solver_cloud.py
|
| 27 |
|
| 28 |
# Ollama local
|
|
@@ -48,6 +51,14 @@ import urllib.request
|
|
| 48 |
# =============================================================================
|
| 49 |
|
| 50 |
PROVIDERS = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
"gemini": {
|
| 52 |
"name": "Google Gemini",
|
| 53 |
"base_url": "https://generativelanguage.googleapis.com/v1beta/models/{model}:generateContent",
|
|
@@ -57,7 +68,7 @@ PROVIDERS = {
|
|
| 57 |
"get_key_url": "https://aistudio.google.com/apikey",
|
| 58 |
},
|
| 59 |
"deepseek": {
|
| 60 |
-
"name": "DeepSeek",
|
| 61 |
"base_url": "https://api.deepseek.com/v1/chat/completions",
|
| 62 |
"default_model": "deepseek-chat",
|
| 63 |
"env_key": "DEEPSEEK_API_KEY",
|
|
@@ -65,7 +76,7 @@ PROVIDERS = {
|
|
| 65 |
"get_key_url": "https://platform.deepseek.com/api_keys",
|
| 66 |
},
|
| 67 |
"glm": {
|
| 68 |
-
"name": "GLM (Zhipu AI)",
|
| 69 |
"base_url": "https://open.bigmodel.cn/api/paas/v4/chat/completions",
|
| 70 |
"default_model": "glm-4-flash",
|
| 71 |
"env_key": "GLM_API_KEY",
|
|
@@ -85,6 +96,29 @@ PROVIDERS = {
|
|
| 85 |
# API CALLERS
|
| 86 |
# =============================================================================
|
| 87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
def call_gemini(prompt: str, api_key: str, model: str = "gemini-2.0-flash",
|
| 89 |
temperature: float = 0.7) -> str:
|
| 90 |
"""Call Google Gemini API."""
|
|
@@ -184,7 +218,9 @@ def call_ollama(prompt: str, model: str = "qwen2.5-coder:32b",
|
|
| 184 |
def call_llm(prompt: str, provider: str, api_key: str = "",
|
| 185 |
model: str = "", temperature: float = 0.7) -> str:
|
| 186 |
"""Unified LLM caller."""
|
| 187 |
-
if provider == "
|
|
|
|
|
|
|
| 188 |
return call_gemini(prompt, api_key, model or "gemini-2.0-flash", temperature)
|
| 189 |
elif provider == "deepseek":
|
| 190 |
return call_deepseek(prompt, api_key, model or "deepseek-chat", temperature)
|
|
@@ -402,6 +438,8 @@ def main():
|
|
| 402 |
# Rate limit respect
|
| 403 |
if PROVIDER == "gemini":
|
| 404 |
time.sleep(4) # 15 RPM = 1 every 4s
|
|
|
|
|
|
|
| 405 |
elif PROVIDER in ("deepseek", "glm"):
|
| 406 |
time.sleep(1)
|
| 407 |
|
|
|
|
| 3 |
=====================================================
|
| 4 |
|
| 5 |
Supports:
|
| 6 |
+
- NVIDIA NIM (free — DeepSeek V4 Pro, GLM-4, Qwen, Llama)
|
| 7 |
+
- Google Gemini (free tier: 15 RPM)
|
| 8 |
+
- DeepSeek direct API (very cheap)
|
| 9 |
+
- GLM/Zhipu direct API (free tier)
|
| 10 |
- Ollama local (any model)
|
|
|
|
| 11 |
|
| 12 |
Usage:
|
| 13 |
+
# NVIDIA NIM — FREE, best option (DeepSeek V4 Pro)
|
| 14 |
+
export LLM_PROVIDER=nvidia
|
| 15 |
+
export NVIDIA_API_KEY=nvapi-xxxxx
|
| 16 |
python llm_solver_cloud.py
|
| 17 |
+
# Get key: https://build.nvidia.com/settings/api-keys
|
| 18 |
+
# Models: deepseek-ai/deepseek-v4-pro, thudm/glm-4-9b-chat, etc.
|
| 19 |
|
| 20 |
+
# NVIDIA NIM with GLM
|
| 21 |
+
export LLM_PROVIDER=nvidia
|
| 22 |
+
export NVIDIA_API_KEY=nvapi-xxxxx
|
| 23 |
+
export LLM_MODEL=thudm/glm-4-9b-chat
|
| 24 |
python llm_solver_cloud.py
|
| 25 |
|
| 26 |
+
# Gemini (free)
|
| 27 |
+
export LLM_PROVIDER=gemini
|
| 28 |
+
export GEMINI_API_KEY=your_key
|
| 29 |
python llm_solver_cloud.py
|
| 30 |
|
| 31 |
# Ollama local
|
|
|
|
| 51 |
# =============================================================================
|
| 52 |
|
| 53 |
PROVIDERS = {
|
| 54 |
+
"nvidia": {
|
| 55 |
+
"name": "NVIDIA NIM (free tier — DeepSeek V4, GLM, Qwen, Llama)",
|
| 56 |
+
"base_url": "https://integrate.api.nvidia.com/v1/chat/completions",
|
| 57 |
+
"default_model": "deepseek-ai/deepseek-v4-pro",
|
| 58 |
+
"env_key": "NVIDIA_API_KEY",
|
| 59 |
+
"free_tier": "Free for NVIDIA Developer Program members (1000 credits)",
|
| 60 |
+
"get_key_url": "https://build.nvidia.com/settings/api-keys",
|
| 61 |
+
},
|
| 62 |
"gemini": {
|
| 63 |
"name": "Google Gemini",
|
| 64 |
"base_url": "https://generativelanguage.googleapis.com/v1beta/models/{model}:generateContent",
|
|
|
|
| 68 |
"get_key_url": "https://aistudio.google.com/apikey",
|
| 69 |
},
|
| 70 |
"deepseek": {
|
| 71 |
+
"name": "DeepSeek (direct API)",
|
| 72 |
"base_url": "https://api.deepseek.com/v1/chat/completions",
|
| 73 |
"default_model": "deepseek-chat",
|
| 74 |
"env_key": "DEEPSEEK_API_KEY",
|
|
|
|
| 76 |
"get_key_url": "https://platform.deepseek.com/api_keys",
|
| 77 |
},
|
| 78 |
"glm": {
|
| 79 |
+
"name": "GLM (Zhipu AI direct)",
|
| 80 |
"base_url": "https://open.bigmodel.cn/api/paas/v4/chat/completions",
|
| 81 |
"default_model": "glm-4-flash",
|
| 82 |
"env_key": "GLM_API_KEY",
|
|
|
|
| 96 |
# API CALLERS
|
| 97 |
# =============================================================================
|
| 98 |
|
| 99 |
+
def call_nvidia(prompt: str, api_key: str, model: str = "deepseek-ai/deepseek-v4-pro",
|
| 100 |
+
temperature: float = 0.7) -> str:
|
| 101 |
+
"""Call NVIDIA NIM API (OpenAI-compatible). Hosts DeepSeek V4, GLM, Qwen, Llama."""
|
| 102 |
+
url = "https://integrate.api.nvidia.com/v1/chat/completions"
|
| 103 |
+
payload = {
|
| 104 |
+
"model": model,
|
| 105 |
+
"messages": [{"role": "user", "content": prompt}],
|
| 106 |
+
"max_tokens": 2048,
|
| 107 |
+
"temperature": temperature,
|
| 108 |
+
}
|
| 109 |
+
data = json.dumps(payload).encode('utf-8')
|
| 110 |
+
req = urllib.request.Request(url, data=data,
|
| 111 |
+
headers={"Content-Type": "application/json",
|
| 112 |
+
"Authorization": f"Bearer {api_key}"},
|
| 113 |
+
method='POST')
|
| 114 |
+
try:
|
| 115 |
+
with urllib.request.urlopen(req, timeout=120) as resp:
|
| 116 |
+
result = json.loads(resp.read().decode())
|
| 117 |
+
return result['choices'][0]['message']['content']
|
| 118 |
+
except Exception as e:
|
| 119 |
+
return f"ERROR: {e}"
|
| 120 |
+
|
| 121 |
+
|
| 122 |
def call_gemini(prompt: str, api_key: str, model: str = "gemini-2.0-flash",
|
| 123 |
temperature: float = 0.7) -> str:
|
| 124 |
"""Call Google Gemini API."""
|
|
|
|
| 218 |
def call_llm(prompt: str, provider: str, api_key: str = "",
|
| 219 |
model: str = "", temperature: float = 0.7) -> str:
|
| 220 |
"""Unified LLM caller."""
|
| 221 |
+
if provider == "nvidia":
|
| 222 |
+
return call_nvidia(prompt, api_key, model or "deepseek-ai/deepseek-v4-pro", temperature)
|
| 223 |
+
elif provider == "gemini":
|
| 224 |
return call_gemini(prompt, api_key, model or "gemini-2.0-flash", temperature)
|
| 225 |
elif provider == "deepseek":
|
| 226 |
return call_deepseek(prompt, api_key, model or "deepseek-chat", temperature)
|
|
|
|
| 438 |
# Rate limit respect
|
| 439 |
if PROVIDER == "gemini":
|
| 440 |
time.sleep(4) # 15 RPM = 1 every 4s
|
| 441 |
+
elif PROVIDER == "nvidia":
|
| 442 |
+
time.sleep(2) # NIM free tier: ~30 RPM
|
| 443 |
elif PROVIDER in ("deepseek", "glm"):
|
| 444 |
time.sleep(1)
|
| 445 |
|