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
# File 'lib/alt_text/client.rb', line 17

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

  encoded_image = Base64.strict_encode64(File.binread(tmp_image))
  tmp_image.close! if tmp_image.is_a?(Tempfile)

  payload = {
    messages: [
      { role: 'user',
        content: [
          { type: 'image',
            source:
              { type: 'base64',
                media_type: 'image/jpeg',
                data: encoded_image } },
          { type: 'text',
            text: prompt }
        ] }
    ],
    max_tokens: 10_000,
    anthropic_version: 'bedrock-2023-05-31'
  }

  response = @client.invoke_model(model_id: model_id,
                                  content_type: 'application/json',
                                  body: payload.to_json)
  JSON.parse(response.body.read)['content'][0]['text']
end