Class: I18nContextGenerator::LLM::Anthropic

Inherits:
Client
  • Object
show all
Defined in:
lib/i18n_context_generator/llm/anthropic.rb

Overview

Claude API implementation of the LLM client.

Constant Summary collapse

API_URL =
'https://api.anthropic.com/v1/messages'
ANTHROPIC_VERSION =
'2023-06-01'
DEFAULT_MODEL =
'claude-sonnet-4-6'

Constants inherited from Client

Client::CONFIDENCE_LEVELS, Client::DEFAULT_MAX_PROMPT_CHARS, Client::MAX_AMBIGUITY_REASON_LENGTH, Client::MAX_DESCRIPTION_LENGTH, Client::MAX_OUTPUT_TOKENS, Client::MAX_TRANSLATION_LENGTH, Client::MIN_MAX_PROMPT_CHARS, Client::RESPONSE_FIELDS, Client::RESPONSE_SCHEMA, Client::SYSTEM_PROMPT, Client::TONES, Client::UI_ELEMENTS

Constants included from RequestPolicy

RequestPolicy::MAX_RETRIES, RequestPolicy::MAX_RETRY_DELAY, RequestPolicy::RETRYABLE_STATUS_CODES, RequestPolicy::TRANSIENT_NETWORK_ERRORS

Instance Method Summary collapse

Methods inherited from Client

default_model_for, for, provider_class, #resolved_model, #validate_supplemental_context!

Constructor Details

#initializeAnthropic

Returns a new instance of Anthropic.

Raises:



11
12
13
14
15
16
17
# File 'lib/i18n_context_generator/llm/anthropic.rb', line 11

def initialize
  super
  @api_key = ENV.fetch('ANTHROPIC_API_KEY', nil)
  raise Error, 'ANTHROPIC_API_KEY environment variable is required' unless @api_key

  @uri = URI(API_URL)
end

Instance Method Details

#generate_context(key:, text:, matches:, model: nil, comment: nil, include_file_paths: false, redact_prompts: true, max_prompt_chars: nil, supplemental_context: []) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/i18n_context_generator/llm/anthropic.rb', line 19

def generate_context(key:, text:, matches:, model: nil, comment: nil,
                     include_file_paths: false, redact_prompts: true,
                     max_prompt_chars: nil, supplemental_context: [])
  outcome = nil
  model = resolved_model(model)
  prompt = build_prompt(
    key: key,
    text: text,
    matches: matches,
    comment: comment,
    include_file_paths: include_file_paths,
    redact_prompts: redact_prompts,
    max_prompt_chars: max_prompt_chars,
    supplemental_context: supplemental_context
  )
  outcome = request_with_retries(uri: @uri) { post_request(model: model, prompt: prompt) }
  handle_response(outcome.response, retries: outcome.retries)
rescue PromptPreparationError => e
  ContextResult.new(description: 'Prompt preparation failed', error: e.message)
rescue StandardError => e
  ContextResult.new(
    description: 'API request failed',
    error: e.message,
    **failure_request_telemetry(e, outcome: outcome)
  )
end