mirror of
https://github.com/MacRimi/ProxMenux.git
synced 2026-11-02 10:52:22 +00:00
fix: read text block and handle refusals in Anthropic provider
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
"""Anthropic (Claude) provider implementation.
|
"""Anthropic (Claude) provider implementation.
|
||||||
|
|
||||||
Anthropic's Claude models are excellent for text generation and translation.
|
Anthropic's Claude models are excellent for text generation and translation.
|
||||||
Models use "-latest" aliases that auto-update to newest versions.
|
Models are addressed by their pinned IDs (the "-latest" aliases were retired).
|
||||||
"""
|
"""
|
||||||
from typing import Optional, List
|
from typing import Optional, List
|
||||||
from .base import AIProvider, AIProviderError
|
from .base import AIProvider, AIProviderError
|
||||||
@@ -15,29 +15,35 @@ class AnthropicProvider(AIProvider):
|
|||||||
API_URL = "https://api.anthropic.com/v1/messages"
|
API_URL = "https://api.anthropic.com/v1/messages"
|
||||||
API_VERSION = "2023-06-01"
|
API_VERSION = "2023-06-01"
|
||||||
|
|
||||||
# Anthropic model aliases that resolve to pinned snapshots (per docs
|
# Current Claude API model IDs (per docs at
|
||||||
# at platform.claude.com/docs/en/docs/about-claude/models). Kept as a
|
# platform.claude.com/docs/en/docs/about-claude/models). Kept as a
|
||||||
# runtime fallback for the `/api/notifications/provider-models`
|
# runtime fallback for `/api/notifications/provider-models` when
|
||||||
# intersection with `verified_ai_models.json`; the JSON is the
|
# `verified_ai_models.json` has no Anthropic entry; the JSON is the
|
||||||
# authoritative UI-facing list. Refreshed 2026-07-13 to drop the
|
# authoritative UI-facing list. Refreshed 2026-10-26 against the live
|
||||||
# deprecated claude-3-5-* aliases (retired upstream, no longer in the
|
# Models API. Ordered cheapest → most capable so the first entry is a
|
||||||
# Models API response) and add the current-generation IDs. Ordered
|
# safe default.
|
||||||
# cheapest → most capable so the first entry is a safe default.
|
|
||||||
KNOWN_MODELS = [
|
KNOWN_MODELS = [
|
||||||
"claude-haiku-4-5",
|
"claude-haiku-5-5",
|
||||||
|
"claude-haiku-4-5-20251001",
|
||||||
|
"claude-sonnet-5-5",
|
||||||
"claude-sonnet-5",
|
"claude-sonnet-5",
|
||||||
"claude-sonnet-4-6",
|
"claude-sonnet-4-6",
|
||||||
|
"claude-opus-5-5",
|
||||||
"claude-opus-4-8",
|
"claude-opus-4-8",
|
||||||
|
"claude-opus-4-7",
|
||||||
"claude-opus-4-6",
|
"claude-opus-4-6",
|
||||||
|
"claude-opus-4-5-20251101",
|
||||||
"claude-fable-5",
|
"claude-fable-5",
|
||||||
|
"claude-fable-5-1",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
# Newer models (Opus 5.x, Fable) may think before answering and the
|
||||||
|
# thinking counts against max_tokens, so a small cap can end the turn
|
||||||
|
# with no text at all. max_tokens is a ceiling, not a cost.
|
||||||
|
MIN_MAX_TOKENS = 1024
|
||||||
|
|
||||||
def list_models(self) -> List[str]:
|
def list_models(self) -> List[str]:
|
||||||
"""Return known Anthropic model aliases.
|
"""Return known Anthropic model IDs."""
|
||||||
|
|
||||||
Anthropic doesn't have a public models list API, but their "-latest"
|
|
||||||
aliases auto-update to the newest versions, making them reliable choices.
|
|
||||||
"""
|
|
||||||
return self.KNOWN_MODELS
|
return self.KNOWN_MODELS
|
||||||
|
|
||||||
def generate(self, system_prompt: str, user_message: str,
|
def generate(self, system_prompt: str, user_message: str,
|
||||||
@@ -68,7 +74,7 @@ class AnthropicProvider(AIProvider):
|
|||||||
'messages': [
|
'messages': [
|
||||||
{'role': 'user', 'content': user_message},
|
{'role': 'user', 'content': user_message},
|
||||||
],
|
],
|
||||||
'max_tokens': max_tokens,
|
'max_tokens': max(max_tokens, self.MIN_MAX_TOKENS),
|
||||||
}
|
}
|
||||||
|
|
||||||
headers = {
|
headers = {
|
||||||
@@ -79,11 +85,20 @@ class AnthropicProvider(AIProvider):
|
|||||||
|
|
||||||
result = self._make_request(self.API_URL, payload, headers)
|
result = self._make_request(self.API_URL, payload, headers)
|
||||||
|
|
||||||
try:
|
# A safety classifier can decline even benign notifications
|
||||||
# Anthropic returns content as array of content blocks
|
# (HTTP 200, stop_reason "refusal", empty content). Raising lets the
|
||||||
content = result['content']
|
# caller fall back to the original, untranslated notification.
|
||||||
if isinstance(content, list) and len(content) > 0:
|
if result.get('stop_reason') == 'refusal':
|
||||||
return content[0].get('text', '').strip()
|
category = (result.get('stop_details') or {}).get('category')
|
||||||
return str(content).strip()
|
raise AIProviderError(f"Request declined by Anthropic (category: {category})")
|
||||||
except (KeyError, IndexError) as e:
|
|
||||||
raise AIProviderError(f"Unexpected response format: {e}")
|
# Content is a list of blocks; thinking blocks may come before the
|
||||||
|
# text, so take the first text block rather than content[0].
|
||||||
|
content = result.get('content')
|
||||||
|
if not isinstance(content, list):
|
||||||
|
raise AIProviderError("Unexpected response format: missing content")
|
||||||
|
for block in content:
|
||||||
|
if isinstance(block, dict) and block.get('type') == 'text':
|
||||||
|
return block.get('text', '').strip()
|
||||||
|
raise AIProviderError(
|
||||||
|
f"No text in response (stop_reason: {result.get('stop_reason')})")
|
||||||
|
|||||||
@@ -0,0 +1,65 @@
|
|||||||
|
import sys
|
||||||
|
import unittest
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
|
SCRIPTS_DIR = Path(__file__).resolve().parents[1]
|
||||||
|
if str(SCRIPTS_DIR) not in sys.path:
|
||||||
|
sys.path.insert(0, str(SCRIPTS_DIR))
|
||||||
|
|
||||||
|
from ai_providers.anthropic_provider import AnthropicProvider
|
||||||
|
from ai_providers.base import AIProviderError
|
||||||
|
|
||||||
|
|
||||||
|
class CannedAnthropicProvider(AnthropicProvider):
|
||||||
|
def __init__(self, response):
|
||||||
|
super().__init__(api_key="test-key", model="claude-opus-5-5")
|
||||||
|
self.response = response
|
||||||
|
self.captured_payload = None
|
||||||
|
|
||||||
|
def _make_request(self, url, payload, headers):
|
||||||
|
self.captured_payload = payload
|
||||||
|
return self.response
|
||||||
|
|
||||||
|
|
||||||
|
class AnthropicProviderTests(unittest.TestCase):
|
||||||
|
def test_skips_thinking_block_before_text(self):
|
||||||
|
provider = CannedAnthropicProvider({
|
||||||
|
"stop_reason": "end_turn",
|
||||||
|
"content": [
|
||||||
|
{"type": "thinking", "thinking": "", "signature": "x"},
|
||||||
|
{"type": "text", "text": " Hola "},
|
||||||
|
],
|
||||||
|
})
|
||||||
|
self.assertEqual(provider.generate("system", "user"), "Hola")
|
||||||
|
|
||||||
|
def test_thinking_only_response_raises(self):
|
||||||
|
provider = CannedAnthropicProvider({
|
||||||
|
"stop_reason": "max_tokens",
|
||||||
|
"content": [{"type": "thinking", "thinking": "", "signature": "x"}],
|
||||||
|
})
|
||||||
|
with self.assertRaises(AIProviderError):
|
||||||
|
provider.generate("system", "user")
|
||||||
|
|
||||||
|
def test_refusal_with_empty_content_raises(self):
|
||||||
|
provider = CannedAnthropicProvider({
|
||||||
|
"stop_reason": "refusal",
|
||||||
|
"stop_details": {"type": "refusal", "category": "cyber"},
|
||||||
|
"content": [],
|
||||||
|
})
|
||||||
|
with self.assertRaisesRegex(AIProviderError, "cyber"):
|
||||||
|
provider.generate("system", "user")
|
||||||
|
|
||||||
|
def test_small_max_tokens_is_raised_to_floor(self):
|
||||||
|
provider = CannedAnthropicProvider({
|
||||||
|
"stop_reason": "end_turn",
|
||||||
|
"content": [{"type": "text", "text": "CONNECTION_OK"}],
|
||||||
|
})
|
||||||
|
provider.generate("system", "user", max_tokens=50)
|
||||||
|
self.assertEqual(provider.captured_payload["max_tokens"], AnthropicProvider.MIN_MAX_TOKENS)
|
||||||
|
provider.generate("system", "user", max_tokens=4000)
|
||||||
|
self.assertEqual(provider.captured_payload["max_tokens"], 4000)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
Reference in New Issue
Block a user