Spaces:
Running on Zero
Running on Zero
official.ghost.logic commited on
Commit ·
b86858a
1
Parent(s): 67393de
Fix: Read API keys directly from environment variables
Browse files- Use os.getenv() directly instead of config object
- Add debug logging to show which clients initialize
- Show first 10 chars of key for verification
- This ensures HF Spaces secrets are properly read
- src/utils/ai_client.py +25 -14
src/utils/ai_client.py
CHANGED
|
@@ -3,6 +3,7 @@ AI Client Manager - Unified interface for multiple LLM providers
|
|
| 3 |
Supports Anthropic Claude, Google Gemini, and OpenAI
|
| 4 |
"""
|
| 5 |
|
|
|
|
| 6 |
from typing import Optional, Dict, Any, List
|
| 7 |
from enum import Enum
|
| 8 |
import anthropic
|
|
@@ -32,29 +33,39 @@ class AIClient:
|
|
| 32 |
|
| 33 |
def _initialize_clients(self):
|
| 34 |
"""Initialize available AI clients based on API keys"""
|
| 35 |
-
# Anthropic Claude
|
| 36 |
-
|
|
|
|
| 37 |
try:
|
| 38 |
-
self.anthropic_client = anthropic.Anthropic(
|
| 39 |
-
|
| 40 |
-
)
|
| 41 |
except Exception as e:
|
| 42 |
-
print(f"Failed to initialize Anthropic client: {e}")
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
# Google Gemini
|
| 45 |
-
|
|
|
|
| 46 |
try:
|
| 47 |
-
genai.configure(api_key=
|
| 48 |
self.google_client = genai
|
|
|
|
| 49 |
except Exception as e:
|
| 50 |
-
print(f"Failed to initialize Google client: {e}")
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
# OpenAI
|
| 53 |
-
|
|
|
|
| 54 |
try:
|
| 55 |
-
self.openai_client = OpenAI(api_key=
|
|
|
|
| 56 |
except Exception as e:
|
| 57 |
-
print(f"Failed to initialize OpenAI client: {e}")
|
|
|
|
|
|
|
| 58 |
|
| 59 |
def generate(
|
| 60 |
self,
|
|
|
|
| 3 |
Supports Anthropic Claude, Google Gemini, and OpenAI
|
| 4 |
"""
|
| 5 |
|
| 6 |
+
import os
|
| 7 |
from typing import Optional, Dict, Any, List
|
| 8 |
from enum import Enum
|
| 9 |
import anthropic
|
|
|
|
| 33 |
|
| 34 |
def _initialize_clients(self):
|
| 35 |
"""Initialize available AI clients based on API keys"""
|
| 36 |
+
# Anthropic Claude - read directly from environment
|
| 37 |
+
anthropic_key = os.getenv("ANTHROPIC_API_KEY")
|
| 38 |
+
if anthropic_key:
|
| 39 |
try:
|
| 40 |
+
self.anthropic_client = anthropic.Anthropic(api_key=anthropic_key)
|
| 41 |
+
print(f"✅ Anthropic client initialized (key: {anthropic_key[:10]}...)")
|
|
|
|
| 42 |
except Exception as e:
|
| 43 |
+
print(f"❌ Failed to initialize Anthropic client: {e}")
|
| 44 |
+
else:
|
| 45 |
+
print("⚠️ ANTHROPIC_API_KEY not found in environment")
|
| 46 |
|
| 47 |
+
# Google Gemini - read directly from environment
|
| 48 |
+
google_key = os.getenv("GOOGLE_API_KEY")
|
| 49 |
+
if google_key:
|
| 50 |
try:
|
| 51 |
+
genai.configure(api_key=google_key)
|
| 52 |
self.google_client = genai
|
| 53 |
+
print(f"✅ Google client initialized (key: {google_key[:10]}...)")
|
| 54 |
except Exception as e:
|
| 55 |
+
print(f"❌ Failed to initialize Google client: {e}")
|
| 56 |
+
else:
|
| 57 |
+
print("⚠️ GOOGLE_API_KEY not found in environment")
|
| 58 |
|
| 59 |
+
# OpenAI - read directly from environment
|
| 60 |
+
openai_key = os.getenv("OPENAI_API_KEY")
|
| 61 |
+
if openai_key:
|
| 62 |
try:
|
| 63 |
+
self.openai_client = OpenAI(api_key=openai_key)
|
| 64 |
+
print(f"✅ OpenAI client initialized (key: {openai_key[:10]}...)")
|
| 65 |
except Exception as e:
|
| 66 |
+
print(f"❌ Failed to initialize OpenAI client: {e}")
|
| 67 |
+
else:
|
| 68 |
+
print("⚠️ OPENAI_API_KEY not found in environment")
|
| 69 |
|
| 70 |
def generate(
|
| 71 |
self,
|