Class: PromptCanary::Adapters::Anthropic

Inherits:
Base
  • Object
show all
Defined in:
lib/prompt_canary/adapters/anthropic.rb

Constant Summary collapse

DEFAULT_MAX_TOKENS =
4096

Instance Method Summary collapse

Constructor Details

#initialize(client: ::Anthropic::Client.new) ⇒ Anthropic

Returns a new instance of Anthropic.



10
11
12
13
# File 'lib/prompt_canary/adapters/anthropic.rb', line 10

def initialize(client: ::Anthropic::Client.new)
  super()
  @client = client
end

Instance Method Details

#call(version:, args:) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/prompt_canary/adapters/anthropic.rb', line 15

def call(version:, args:)
  start = Process.clock_gettime(Process::CLOCK_MONOTONIC)

  response = @client.messages.create(
    model: version.model,
    system_: version.system_for(args),
    max_tokens: DEFAULT_MAX_TOKENS,
    messages: [{ role: "user", content: args.fetch(:user_message, "Generate.") }]
  )

  latency_ms = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - start) * 1000).round

  {
    text: response.content.first.text,
    latency_ms: latency_ms,
    tokens: { input: response.usage.input_tokens, output: response.usage.output_tokens },
    error: nil
  }
rescue ::Anthropic::Errors::APIError => e
  latency_ms = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - start) * 1000).round
  { text: nil, latency_ms: latency_ms, tokens: nil, error: e }
end