Class: AltText::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/alt_text/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(access_key:, secret_key:, region:) ⇒ Client

Returns a new instance of Client.



9
10
11
12
13
14
15
# File 'lib/alt_text/client.rb', line 9

def initialize(access_key:, secret_key:, region:)
  @client = Aws::BedrockRuntime::Client.new(
    access_key_id: access_key,
    secret_access_key: secret_key,
    region: region
  )
end

Instance Method Details

#process_image(image_path, prompt:, model_id:) ⇒ Object



17
18
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/alt_text/client.rb', line 17

def process_image(image_path, prompt:, model_id:)
  model_id = AltText::LLMRegistry.resolve(model_id)
  image_format = image_format_for(image_path)
  tmp_image = resize_if_needed(image_path)

  image_bytes = File.binread(tmp_image)
  tmp_image.close! if tmp_image.is_a?(Tempfile)

  messages = [
    {
      role: 'user',
      content: [
        {
          image: {
            format: image_format,
            source: {
              bytes: image_bytes
            }
          }
        },
        {
          text: prompt
        }
      ]
    }
  ]

  # The `converse` method of the Bedrock Ruby SDK is used to interact with
  # LLM models in a standardized way, using a "messages" schema that supports
  # text, images, and tool calls. Unlike `invoke_model`, which requires
  # model-specific payloads.  Note that this prevents fine-grained control
  # of image processing parameters that some models may support.
  #
  # Examples of supported models:
  #   - Amazon Nova Pro (supports text and images)
  #   - Amazon Nova Lite (supports text and images)
  #   - Anthropic Claude / Opus (supports text and images)
  response = @client.converse(model_id: model_id,
                              messages: messages)

  response.output.message.content.first.text
end