Spaces:
Sleeping
Sleeping
Graham Paasch commited on
Commit ·
703134e
1
Parent(s): 32f0387
Disable OpenRouter; only Anthropic/OpenAI providers remain
Browse files- agent/llm_client.py +4 -111
agent/llm_client.py
CHANGED
|
@@ -42,7 +42,6 @@ class LLMClient:
|
|
| 42 |
# Support both standard and MCP hackathon naming conventions
|
| 43 |
self.openai_key = _get_env(["OPENAI_API_KEY", "OPENAI_MCP_1ST_BDAY"])
|
| 44 |
self.anthropic_key = _get_env(["ANTHROPIC_API_KEY", "ANTHROPIC_MCP_1ST_BDAY"])
|
| 45 |
-
self.openrouter_key = _get_env(["OPENROUTER_API_KEY"])
|
| 46 |
|
| 47 |
# Determine which provider to use
|
| 48 |
self.provider = self._detect_provider()
|
|
@@ -68,15 +67,12 @@ class LLMClient:
|
|
| 68 |
status = {
|
| 69 |
"openai_key": "present" if _present(["OPENAI_API_KEY", "OPENAI_MCP_1ST_BDAY"]) else "missing",
|
| 70 |
"anthropic_key": "present" if _present(["ANTHROPIC_API_KEY", "ANTHROPIC_MCP_1ST_BDAY"]) else "missing",
|
| 71 |
-
"openrouter_key": "present" if _present(["OPENROUTER_API_KEY"]) else "missing",
|
| 72 |
"provider": "unknown",
|
| 73 |
}
|
| 74 |
if status["anthropic_key"] == "present":
|
| 75 |
status["provider"] = "anthropic"
|
| 76 |
elif status["openai_key"] == "present":
|
| 77 |
status["provider"] = "openai"
|
| 78 |
-
elif status["openrouter_key"] == "present":
|
| 79 |
-
status["provider"] = "openrouter"
|
| 80 |
return status
|
| 81 |
|
| 82 |
def _detect_provider(self) -> Optional[str]:
|
|
@@ -85,8 +81,6 @@ class LLMClient:
|
|
| 85 |
return "anthropic"
|
| 86 |
elif self.openai_key:
|
| 87 |
return "openai"
|
| 88 |
-
elif self.openrouter_key:
|
| 89 |
-
return "openrouter"
|
| 90 |
return None
|
| 91 |
|
| 92 |
def chat(
|
|
@@ -103,9 +97,7 @@ class LLMClient:
|
|
| 103 |
if not self.provider:
|
| 104 |
raise RuntimeError("No LLM provider configured. Set ANTHROPIC_MCP_1ST_BDAY or OPENAI_MCP_1ST_BDAY.")
|
| 105 |
|
| 106 |
-
if self.provider == "
|
| 107 |
-
return self._call_openrouter(messages, temperature, max_tokens, stream)
|
| 108 |
-
elif self.provider == "openai":
|
| 109 |
return self._call_openai(messages, temperature, max_tokens, stream)
|
| 110 |
elif self.provider == "anthropic":
|
| 111 |
return self._call_anthropic(messages, temperature, max_tokens, stream)
|
|
@@ -120,117 +112,18 @@ class LLMClient:
|
|
| 120 |
if not self.provider:
|
| 121 |
raise RuntimeError("No LLM provider configured. Set ANTHROPIC_MCP_1ST_BDAY or OPENAI_MCP_1ST_BDAY.")
|
| 122 |
|
| 123 |
-
if self.provider == "
|
| 124 |
-
yield from self._stream_openrouter(messages, temperature, max_tokens)
|
| 125 |
-
elif self.provider == "openai":
|
| 126 |
yield from self._stream_openai(messages, temperature, max_tokens)
|
| 127 |
elif self.provider == "anthropic":
|
| 128 |
yield from self._stream_anthropic(messages, temperature, max_tokens)
|
| 129 |
|
| 130 |
def _call_openrouter(self, messages, temperature, max_tokens, stream):
|
| 131 |
"""Call OpenRouter API"""
|
| 132 |
-
|
| 133 |
-
model = "anthropic/claude-3.5-sonnet"
|
| 134 |
-
|
| 135 |
-
if monitor:
|
| 136 |
-
monitor.start_call(call_id, "llm", "openrouter", model, temperature=temperature)
|
| 137 |
-
|
| 138 |
-
try:
|
| 139 |
-
import requests
|
| 140 |
-
|
| 141 |
-
url = "https://openrouter.ai/api/v1/chat/completions"
|
| 142 |
-
headers = {
|
| 143 |
-
"Authorization": f"Bearer {self.openrouter_key}",
|
| 144 |
-
"Content-Type": "application/json"
|
| 145 |
-
}
|
| 146 |
-
|
| 147 |
-
data = {
|
| 148 |
-
"model": model, # Good balance of cost/quality
|
| 149 |
-
"messages": [{"role": m.role, "content": m.content} for m in messages],
|
| 150 |
-
"temperature": temperature,
|
| 151 |
-
"max_tokens": max_tokens
|
| 152 |
-
}
|
| 153 |
-
|
| 154 |
-
response = requests.post(url, headers=headers, json=data, timeout=60)
|
| 155 |
-
response.raise_for_status()
|
| 156 |
-
|
| 157 |
-
result = response.json()
|
| 158 |
-
content = result['choices'][0]['message']['content']
|
| 159 |
-
usage = result.get('usage', {})
|
| 160 |
-
|
| 161 |
-
if monitor:
|
| 162 |
-
monitor.complete_call(
|
| 163 |
-
call_id,
|
| 164 |
-
success=True,
|
| 165 |
-
input_tokens=usage.get('prompt_tokens', 0),
|
| 166 |
-
output_tokens=usage.get('completion_tokens', 0)
|
| 167 |
-
)
|
| 168 |
-
|
| 169 |
-
return content
|
| 170 |
-
|
| 171 |
-
except Exception as e:
|
| 172 |
-
logger.error(f"OpenRouter API error: {e}")
|
| 173 |
-
if monitor:
|
| 174 |
-
monitor.complete_call(call_id, success=False, error_message=str(e))
|
| 175 |
-
raise
|
| 176 |
|
| 177 |
def _stream_openrouter(self, messages, temperature, max_tokens):
|
| 178 |
"""Stream from OpenRouter"""
|
| 179 |
-
|
| 180 |
-
model = "anthropic/claude-3.5-sonnet"
|
| 181 |
-
|
| 182 |
-
if monitor:
|
| 183 |
-
monitor.start_call(call_id, "llm", "openrouter", model, temperature=temperature)
|
| 184 |
-
|
| 185 |
-
total_tokens_est = 0
|
| 186 |
-
|
| 187 |
-
try:
|
| 188 |
-
import requests
|
| 189 |
-
|
| 190 |
-
url = "https://openrouter.ai/api/v1/chat/completions"
|
| 191 |
-
headers = {
|
| 192 |
-
"Authorization": f"Bearer {self.openrouter_key}",
|
| 193 |
-
"Content-Type": "application/json"
|
| 194 |
-
}
|
| 195 |
-
|
| 196 |
-
data = {
|
| 197 |
-
"model": model,
|
| 198 |
-
"messages": [{"role": m.role, "content": m.content} for m in messages],
|
| 199 |
-
"temperature": temperature,
|
| 200 |
-
"max_tokens": max_tokens,
|
| 201 |
-
"stream": True
|
| 202 |
-
}
|
| 203 |
-
|
| 204 |
-
with requests.post(url, headers=headers, json=data, stream=True, timeout=60) as response:
|
| 205 |
-
response.raise_for_status()
|
| 206 |
-
for line in response.iter_lines():
|
| 207 |
-
if line:
|
| 208 |
-
line = line.decode('utf-8')
|
| 209 |
-
if line.startswith('data: '):
|
| 210 |
-
line = line[6:]
|
| 211 |
-
if line == '[DONE]':
|
| 212 |
-
# Estimate tokens (rough approximation: 4 chars = 1 token)
|
| 213 |
-
if monitor:
|
| 214 |
-
monitor.complete_call(call_id, success=True,
|
| 215 |
-
input_tokens=total_tokens_est // 2,
|
| 216 |
-
output_tokens=total_tokens_est // 2)
|
| 217 |
-
break
|
| 218 |
-
try:
|
| 219 |
-
chunk = json.loads(line)
|
| 220 |
-
if 'choices' in chunk and len(chunk['choices']) > 0:
|
| 221 |
-
delta = chunk['choices'][0].get('delta', {})
|
| 222 |
-
if 'content' in delta:
|
| 223 |
-
content = delta['content']
|
| 224 |
-
total_tokens_est += len(content) // 4
|
| 225 |
-
yield content
|
| 226 |
-
except json.JSONDecodeError:
|
| 227 |
-
continue
|
| 228 |
-
|
| 229 |
-
except Exception as e:
|
| 230 |
-
logger.error(f"OpenRouter streaming error: {e}")
|
| 231 |
-
if monitor:
|
| 232 |
-
monitor.complete_call(call_id, success=False, error_message=str(e))
|
| 233 |
-
raise
|
| 234 |
|
| 235 |
def _call_openai(self, messages, temperature, max_tokens, stream):
|
| 236 |
"""Call OpenAI API"""
|
|
|
|
| 42 |
# Support both standard and MCP hackathon naming conventions
|
| 43 |
self.openai_key = _get_env(["OPENAI_API_KEY", "OPENAI_MCP_1ST_BDAY"])
|
| 44 |
self.anthropic_key = _get_env(["ANTHROPIC_API_KEY", "ANTHROPIC_MCP_1ST_BDAY"])
|
|
|
|
| 45 |
|
| 46 |
# Determine which provider to use
|
| 47 |
self.provider = self._detect_provider()
|
|
|
|
| 67 |
status = {
|
| 68 |
"openai_key": "present" if _present(["OPENAI_API_KEY", "OPENAI_MCP_1ST_BDAY"]) else "missing",
|
| 69 |
"anthropic_key": "present" if _present(["ANTHROPIC_API_KEY", "ANTHROPIC_MCP_1ST_BDAY"]) else "missing",
|
|
|
|
| 70 |
"provider": "unknown",
|
| 71 |
}
|
| 72 |
if status["anthropic_key"] == "present":
|
| 73 |
status["provider"] = "anthropic"
|
| 74 |
elif status["openai_key"] == "present":
|
| 75 |
status["provider"] = "openai"
|
|
|
|
|
|
|
| 76 |
return status
|
| 77 |
|
| 78 |
def _detect_provider(self) -> Optional[str]:
|
|
|
|
| 81 |
return "anthropic"
|
| 82 |
elif self.openai_key:
|
| 83 |
return "openai"
|
|
|
|
|
|
|
| 84 |
return None
|
| 85 |
|
| 86 |
def chat(
|
|
|
|
| 97 |
if not self.provider:
|
| 98 |
raise RuntimeError("No LLM provider configured. Set ANTHROPIC_MCP_1ST_BDAY or OPENAI_MCP_1ST_BDAY.")
|
| 99 |
|
| 100 |
+
if self.provider == "openai":
|
|
|
|
|
|
|
| 101 |
return self._call_openai(messages, temperature, max_tokens, stream)
|
| 102 |
elif self.provider == "anthropic":
|
| 103 |
return self._call_anthropic(messages, temperature, max_tokens, stream)
|
|
|
|
| 112 |
if not self.provider:
|
| 113 |
raise RuntimeError("No LLM provider configured. Set ANTHROPIC_MCP_1ST_BDAY or OPENAI_MCP_1ST_BDAY.")
|
| 114 |
|
| 115 |
+
if self.provider == "openai":
|
|
|
|
|
|
|
| 116 |
yield from self._stream_openai(messages, temperature, max_tokens)
|
| 117 |
elif self.provider == "anthropic":
|
| 118 |
yield from self._stream_anthropic(messages, temperature, max_tokens)
|
| 119 |
|
| 120 |
def _call_openrouter(self, messages, temperature, max_tokens, stream):
|
| 121 |
"""Call OpenRouter API"""
|
| 122 |
+
raise RuntimeError("OpenRouter is disabled for this Space.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
def _stream_openrouter(self, messages, temperature, max_tokens):
|
| 125 |
"""Stream from OpenRouter"""
|
| 126 |
+
raise RuntimeError("OpenRouter is disabled for this Space.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
def _call_openai(self, messages, temperature, max_tokens, stream):
|
| 129 |
"""Call OpenAI API"""
|