1+ # frozen_string_literal: true
12require 'aws-sdk-bedrockruntime'
23require 'json'
34require 'base64' # Required for Base64.strict_encode64
45
56module BedrockDocumentScreener
6- MODEL_ID = 'us.anthropic.claude-haiku-4-5-20251001-v1:0' # routes request to specific Bedrock model
7+ MODEL_ID = 'us.anthropic.claude-haiku-4-5-20251001-v1:0'
78 REGION = 'us-east-1'
89 MIME_TYPES = {
910 '.pdf' => 'application/pdf' ,
@@ -12,36 +13,40 @@ module BedrockDocumentScreener
1213 '.jpeg' => 'image/jpeg'
1314 } . freeze
1415 DEFAULT_FILE_PATH = Rails . root . join ( "spec" , "fixtures" , "files" , "picture_id.jpg" ) . freeze
15- DEFAULT_PROMPT = "Is this a photo id?" # change this for different file types
16-
17- # matches the Anthropic Messages API JSON shape that Bedrock expects for Anthropic models
16+ DEFAULT_PROMPT = "Is this a photo id?"
1817 CLAUDE_MESSAGES_API_TEMPLATE = {
19- anthropic_version : "bedrock-2023-05-31" , # Interpret this request body using the Anthropic Messages API schema from May 31, 2023.”
20- max_tokens : 100 , # limits the output of the output of the model, token is a unit the model uses to read and write text
21- messages : [ { role : "user" ,
22- content : [
23- { type : "image" ,
24- source : {
25- type : "base64" ,
26- media_type : nil , # populated later
27- data : nil # populated later
28- } } ,
29- { type : "text" , text : DEFAULT_PROMPT }
30- ] } ]
18+ anthropic_version : "bedrock-2023-05-31" ,
19+ max_tokens : 100 ,
20+ messages : [
21+ {
22+ role : "user" ,
23+ content : [
24+ {
25+ type : "image" ,
26+ source : {
27+ type : "base64" ,
28+ media_type : nil , # populated later
29+ data : nil # populated later
30+ }
31+ } ,
32+ {
33+ type : "text" ,
34+ text : DEFAULT_PROMPT
35+ }
36+ ]
37+ }
38+ ]
3139 } . freeze
3240
33- # gets the file extension from the file path and checks if its a accepted type
3441 def self . file_media_type ( file_path )
3542 media_type = MIME_TYPES [ File . extname ( file_path ) . downcase ]
36- # skip instead of raise? capture sentry
3743 raise "Unsupported file type: #{ File . extname ( file_path ) } . Supported types: #{ MIME_TYPES . keys . join ( ', ' ) } ." unless media_type
3844 media_type
3945 end
4046
41- # reads the file as bytes, encodes it
4247 def self . file_data ( file_path )
4348 base64_data = Base64 . strict_encode64 ( File . binread ( file_path ) )
44- # skip if can't encode file?
49+ raise "Could not read and encode file: #{ file_path } " unless base64_data
4550 base64_data
4651 end
4752
0 commit comments