official.ghost.logic commited on
Commit
ed1e0bc
·
1 Parent(s): b86858a

Add lazy initialization for Anthropic client

Browse files

- Re-initialize client from env if not set during first call
- Provides better error message pointing to HF Spaces Secrets
- Fixes 'Anthropic client not initialized' error

Files changed (1) hide show
  1. src/utils/ai_client.py +11 -1
src/utils/ai_client.py CHANGED
@@ -116,7 +116,17 @@ class AIClient:
116
  ) -> str:
117
  """Generate using Anthropic Claude"""
118
  if not self.anthropic_client:
119
- raise RuntimeError("Anthropic client not initialized")
 
 
 
 
 
 
 
 
 
 
120
 
121
  messages = [{"role": "user", "content": prompt}]
122
 
 
116
  ) -> str:
117
  """Generate using Anthropic Claude"""
118
  if not self.anthropic_client:
119
+ # Try to re-initialize from environment
120
+ anthropic_key = os.getenv("ANTHROPIC_API_KEY")
121
+ if anthropic_key:
122
+ print(f"Re-initializing Anthropic client with key: {anthropic_key[:10]}...")
123
+ try:
124
+ self.anthropic_client = anthropic.Anthropic(api_key=anthropic_key)
125
+ print("✅ Anthropic client re-initialized successfully")
126
+ except Exception as e:
127
+ raise RuntimeError(f"Failed to re-initialize Anthropic client: {e}")
128
+ else:
129
+ raise RuntimeError("Anthropic client not initialized. Set ANTHROPIC_API_KEY in HF Spaces Secrets.")
130
 
131
  messages = [{"role": "user", "content": prompt}]
132