Class: BetterTranslate::Providers::AnthropicProvider
- Inherits:
-
BaseHttpProvider
- Object
- BaseHttpProvider
- BetterTranslate::Providers::AnthropicProvider
- Defined in:
- lib/better_translate/providers/anthropic_provider.rb
Overview
Anthropic Claude translation provider
Uses claude-haiku-4-5 model for fast, efficient translations.
Constant Summary collapse
- API_URL =
Anthropic API endpoint
"https://api.anthropic.com/v1/messages"- MODEL =
Model to use for translations
"claude-haiku-4-5"- API_VERSION =
API version
"2023-06-01"
Instance Attribute Summary
Attributes inherited from BaseHttpProvider
#cache, #config, #rate_limiter
Instance Method Summary collapse
-
#build_messages(text, target_lang_name) ⇒ Hash
private
private
Build messages for Anthropic API.
-
#build_system_message(target_lang_name) ⇒ String
private
private
Build system message with optional context.
-
#extract_translation(response) ⇒ String
private
private
Extract translation from API response.
-
#make_messages_request(message_data) ⇒ Faraday::Response
private
private
Make messages request to Anthropic API.
-
#translate_batch(texts, target_lang_code, target_lang_name) ⇒ Array<String>
Translate multiple texts in a batch.
-
#translate_text(text, target_lang_code, target_lang_name) ⇒ String
Translate a single text.
Methods inherited from BaseHttpProvider
#build_cache_key, #calculate_backoff, #handle_response, #http_client, #initialize, #log_retry, #make_request, #with_cache
Constructor Details
This class inherits a constructor from BetterTranslate::Providers::BaseHttpProvider
Instance Method Details
#build_messages(text, target_lang_name) ⇒ Hash (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Build messages for Anthropic API
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/better_translate/providers/anthropic_provider.rb', line 81 def (text, target_lang_name) = (target_lang_name) { system: , messages: [ { role: "user", content: text } ] } end |
#build_system_message(target_lang_name) ⇒ String (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Build system message with optional context
98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/better_translate/providers/anthropic_provider.rb', line 98 def (target_lang_name) = "You are a professional translator. Translate the following text to #{target_lang_name}. " \ "Return ONLY the translated text, without any explanations or additional text. " \ "Words like VARIABLE_0, VARIABLE_1, etc. are placeholders and must be kept unchanged in the translation." if config.translation_context && !config.translation_context.empty? += "\n\nContext: #{config.translation_context}" end end |
#extract_translation(response) ⇒ String (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Extract translation from API response
140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/better_translate/providers/anthropic_provider.rb', line 140 def extract_translation(response) parsed = JSON.parse(response.body) translation = parsed.dig("content", 0, "text") raise TranslationError, "No translation in response" if translation.nil? || translation.empty? translation.strip rescue JSON::ParserError => e raise TranslationError.new( "Failed to parse Anthropic response", context: { error: e., body: response.body } ) end |
#make_messages_request(message_data) ⇒ Faraday::Response (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Make messages request to Anthropic API
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/better_translate/providers/anthropic_provider.rb', line 116 def () body = { model: MODEL, max_tokens: 1024, system: [:system], messages: [:messages] } headers = { "Content-Type" => "application/json", "x-api-key" => config.anthropic_key || "", "anthropic-version" => API_VERSION } make_request(:post, API_URL, body: body, headers: headers) end |
#translate_batch(texts, target_lang_code, target_lang_name) ⇒ Array<String>
Translate multiple texts in a batch
68 69 70 |
# File 'lib/better_translate/providers/anthropic_provider.rb', line 68 def translate_batch(texts, target_lang_code, target_lang_name) texts.map { |text| translate_text(text, target_lang_code, target_lang_name) } end |
#translate_text(text, target_lang_code, target_lang_name) ⇒ String
Translate a single text
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/better_translate/providers/anthropic_provider.rb', line 39 def translate_text(text, target_lang_code, target_lang_name) Validator.validate_text!(text) Validator.validate_language_code!(target_lang_code) cache_key = build_cache_key(text, target_lang_code) with_cache(cache_key) do = (text, target_lang_name) response = () extract_translation(response) end rescue ApiError => e raise TranslationError.new( "Failed to translate text with Anthropic: #{e.}", context: { text: text, target_lang: target_lang_code, original_error: e } ) end |