remove anthropic sdk to remove dependencies to apache licences

This commit is contained in:
2025-12-16 16:44:06 +02:00
parent 8ca921715e
commit 3a419645e3

View File

@@ -1,9 +1,10 @@
"""Anthropic/Claude LLM provider.""" """Anthropic/Claude LLM provider."""
import anthropic # import anthropic
from typing import Dict, List from typing import Dict, List
from .base import LLMProvider from .base import LLMProvider
from .prompts import COMPRESS_COMMIT_PROMPT, GENERATE_DAILY_PROMPT from .prompts import COMPRESS_COMMIT_PROMPT, GENERATE_DAILY_PROMPT
import requests
class AnthropicProvider(LLMProvider): class AnthropicProvider(LLMProvider):
@@ -11,15 +12,38 @@ class AnthropicProvider(LLMProvider):
def __init__(self, config: Dict): def __init__(self, config: Dict):
super().__init__(config) super().__init__(config)
api_key = config.get('api_key', '') self.api_key = config.get('api_key', '')
if not api_key: if not self.api_key:
raise ValueError("Anthropic API key not configured. Run: jrnl config set anthropic api_key YOUR_KEY") raise ValueError("Anthropic API key not configured. Run: jrnl config set anthropic api_key YOUR_KEY")
self.client = anthropic.Anthropic(api_key=api_key) # self.client = anthropic.Anthropic(api_key=api_key)
self.model = config.get('model', 'claude-sonnet-4-5-20250929') self.model = config.get('model', 'claude-sonnet-4-5-20250929')
self.max_tokens_commit = config.get('max_tokens_commit', 200) self.max_tokens_commit = config.get('max_tokens_commit', 200)
self.max_tokens_daily = config.get('max_tokens_daily', 500) self.max_tokens_daily = config.get('max_tokens_daily', 500)
def _send_message(self, prompt: dict, max_tokens=200) -> dict:
res = requests.post(
"https://api.anthropic.com/v1/messages",
headers={
'x-api-key': self.api_key,
'anthropic-version': '2023-06-01',
'Content-Type': 'application/json'
},
data={
'model': self.model,
'max_tokens': max_tokens,
'temperature': 0.3,
'messages': [
{
'role': 'user',
'text': prompt
}
]
}
)
return res.json()
def compress_commit(self, commit_message: str, commit_diff: str) -> str: def compress_commit(self, commit_message: str, commit_diff: str) -> str:
"""Compress commit using Claude.""" """Compress commit using Claude."""
prompt = COMPRESS_COMMIT_PROMPT.format( prompt = COMPRESS_COMMIT_PROMPT.format(
@@ -28,21 +52,14 @@ class AnthropicProvider(LLMProvider):
) )
try: try:
message = self.client.messages.create( message = self._send_message(
model=self.model, message=prompt,
max_tokens=self.max_tokens_commit, max_tokens=self.max_tokens_commit,
temperature=0.3,
messages=[
{"role": "user", "content": prompt}
]
) )
return message.content[0].text.strip()
except anthropic.AuthenticationError: return message.get('content', [])[0].get('text').strip()
return f"[Auth Error] {commit_message}" except IndexError as e:
except anthropic.RateLimitError: return f"[Anthropic Error] {commit_message}"
return f"[Rate Limit] {commit_message}"
except anthropic.APIError as e:
return f"[API Error] {commit_message}"
except Exception as e: except Exception as e:
return f"[LLM Error] {commit_message}" return f"[LLM Error] {commit_message}"
@@ -60,21 +77,15 @@ class AnthropicProvider(LLMProvider):
) )
try: try:
message = self.client.messages.create( message = self._send_message(
model=self.model, message=prompt,
max_tokens=self.max_tokens_daily, max_tokens=self.max_tokens_daily,
temperature=0.5,
messages=[
{"role": "user", "content": prompt}
]
) )
return message.content[0].text.strip()
except anthropic.AuthenticationError: return message.get('content', [])[0].get('text').strip()
raise RuntimeError("Invalid Anthropic API key. Run: jrnl config set anthropic api_key YOUR_KEY")
except anthropic.RateLimitError: except IndexError as e:
raise RuntimeError("Anthropic API rate limit exceeded. Try again later or use ollama.") return f"[Anthropic Error] Failed to generate daily."
except anthropic.APIError as e:
raise RuntimeError(f"Anthropic API error: {e}")
except Exception as e: except Exception as e:
raise RuntimeError(f"Failed to generate daily: {type(e).__name__}: {e}") raise RuntimeError(f"Failed to generate daily: {type(e).__name__}: {e}")