Spaces:
Sleeping
Sleeping
Commit ·
5896d2e
1
Parent(s): 0d83c90
Fix: Add debugging to find the missing API Key
Browse files- src/llm.py +26 -7
src/llm.py
CHANGED
|
@@ -1,24 +1,43 @@
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
from dotenv import load_dotenv
|
| 3 |
from langchain_openai import ChatOpenAI
|
| 4 |
|
|
|
|
| 5 |
load_dotenv()
|
| 6 |
|
| 7 |
def get_llm():
|
| 8 |
"""
|
| 9 |
The Brain.
|
| 10 |
-
Connects to OpenRouter
|
| 11 |
"""
|
|
|
|
| 12 |
api_key = os.getenv("OPENROUTER_API_KEY")
|
|
|
|
|
|
|
| 13 |
if not api_key:
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
#
|
| 17 |
llm = ChatOpenAI(
|
| 18 |
-
model="
|
| 19 |
-
|
| 20 |
-
|
| 21 |
temperature=0
|
| 22 |
)
|
| 23 |
|
| 24 |
-
return llm
|
|
|
|
|
|
| 1 |
+
#model="z-ai/glm-4.5-air:free",
|
| 2 |
+
|
| 3 |
import os
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
from langchain_openai import ChatOpenAI
|
| 6 |
|
| 7 |
+
# Load .env locally (This line does nothing on the cloud if .env is missing, which is fine)
|
| 8 |
load_dotenv()
|
| 9 |
|
| 10 |
def get_llm():
|
| 11 |
"""
|
| 12 |
The Brain.
|
| 13 |
+
Connects to OpenRouter.
|
| 14 |
"""
|
| 15 |
+
# 1. Try to get the key
|
| 16 |
api_key = os.getenv("OPENROUTER_API_KEY")
|
| 17 |
+
|
| 18 |
+
# 2. Debugging Block (This prints to Hugging Face Logs)
|
| 19 |
if not api_key:
|
| 20 |
+
print("⚠️ CRITICAL ERROR: API Key is missing!")
|
| 21 |
+
print("---------------------------------------------------")
|
| 22 |
+
print("I looked for: 'OPENROUTER_API_KEY'")
|
| 23 |
+
print("But I only found these keys in the environment:")
|
| 24 |
+
# Print only the NAMES of the keys (safe), not the values
|
| 25 |
+
for key in os.environ.keys():
|
| 26 |
+
if "API" in key or "KEY" in key:
|
| 27 |
+
print(f" - {key}")
|
| 28 |
+
print("---------------------------------------------------")
|
| 29 |
+
raise ValueError("❌ OPENROUTER_API_KEY not found. Please check your Hugging Face Secrets.")
|
| 30 |
+
else:
|
| 31 |
+
# If it works, print a masked version to confirm
|
| 32 |
+
print(f"✅ API Key loaded successfully! (Starts with: {api_key[:8]}...)")
|
| 33 |
|
| 34 |
+
# 3. Connect to the LLM
|
| 35 |
llm = ChatOpenAI(
|
| 36 |
+
model="google/gemini-2.0-flash-lite-preview-02-05:free", # Using the faster/newer model
|
| 37 |
+
api_key=api_key,
|
| 38 |
+
base_url="https://openrouter.ai/api/v1",
|
| 39 |
temperature=0
|
| 40 |
)
|
| 41 |
|
| 42 |
+
return llm
|
| 43 |
+
|