Class: Mindee::Input::InferenceParameters

Inherits:
Object
  • Object
show all
Defined in:
lib/mindee/input/inference_parameters.rb

Overview

Parameters to set when sending a file for inference.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_id, rag: nil, raw_text: nil, polygon: nil, confidence: nil, file_alias: nil, webhook_ids: nil, text_context: nil, polling_options: nil, close_file: true, data_schema: nil) ⇒ InferenceParameters

rubocop:disable Metrics/ParameterLists

Parameters:

  • model_id (String)

    ID of the model

  • rag (nil) (defaults to: nil)

    Whether to enable RAG.

  • raw_text (nil) (defaults to: nil)

    Whether to enable rax text.

  • polygon (nil) (defaults to: nil)

    Whether to enable polygons.

  • confidence (nil) (defaults to: nil)

    Whether to enable confidence scores.

  • file_alias (nil) (defaults to: nil)

    File alias, if applicable.

  • webhook_ids (nil) (defaults to: nil)
  • polling_options (nil) (defaults to: nil)
  • close_file (TrueClass) (defaults to: true)

Raises:



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/mindee/input/inference_parameters.rb', line 56

def initialize(
  model_id,
  rag: nil,
  raw_text: nil,
  polygon: nil,
  confidence: nil,
  file_alias: nil,
  webhook_ids: nil,
  text_context: nil,
  polling_options: nil,
  close_file: true,
  data_schema: nil
)
  raise Errors::MindeeInputError, 'Model ID is required.' if model_id.empty? || model_id.nil?

  @model_id = model_id
  @rag = rag
  @raw_text = raw_text
  @polygon = polygon
  @confidence = confidence
  @file_alias = file_alias
  @webhook_ids = webhook_ids || []
  @text_context = text_context
  @polling_options = get_clean_polling_options(polling_options)
  @close_file = close_file.nil? || close_file
  @data_schema = DataSchema.new(data_schema) unless data_schema.nil?
  # rubocop:enable Metrics/ParameterLists
end

Instance Attribute Details

#close_fileBoolean? (readonly)

Returns Whether to close the file after parsing.

Returns:

  • (Boolean, nil)

    Whether to close the file after parsing.



44
45
46
# File 'lib/mindee/input/inference_parameters.rb', line 44

def close_file
  @close_file
end

#confidenceBoolean? (readonly)

Returns Boost the precision and accuracy of all extractions. Calculate confidence scores for all fields, and fill their confidence attribute.

Returns:

  • (Boolean, nil)

    Boost the precision and accuracy of all extractions. Calculate confidence scores for all fields, and fill their confidence attribute.



25
26
27
# File 'lib/mindee/input/inference_parameters.rb', line 25

def confidence
  @confidence
end

#data_schemaDataSchemaField (readonly)

Returns:



41
42
43
# File 'lib/mindee/input/inference_parameters.rb', line 41

def data_schema
  @data_schema
end

#file_aliasString? (readonly)

Returns Optional alias for the file.

Returns:

  • (String, nil)

    Optional alias for the file.



28
29
30
# File 'lib/mindee/input/inference_parameters.rb', line 28

def file_alias
  @file_alias
end

#model_idString (readonly)

Returns ID of the model (required).

Returns:

  • (String)

    ID of the model (required).



10
11
12
# File 'lib/mindee/input/inference_parameters.rb', line 10

def model_id
  @model_id
end

#polling_optionsPollingOptions (readonly)

Returns Options for polling. Set only if having timeout issues.

Returns:

  • (PollingOptions)

    Options for polling. Set only if having timeout issues.



38
39
40
# File 'lib/mindee/input/inference_parameters.rb', line 38

def polling_options
  @polling_options
end

#polygonBoolean? (readonly)

Returns Calculate bounding box polygons for all fields, and fill their locations attribute.

Returns:

  • (Boolean, nil)

    Calculate bounding box polygons for all fields, and fill their locations attribute.



21
22
23
# File 'lib/mindee/input/inference_parameters.rb', line 21

def polygon
  @polygon
end

#ragBoolean? (readonly)

Returns Enhance extraction accuracy with Retrieval-Augmented Generation.

Returns:

  • (Boolean, nil)

    Enhance extraction accuracy with Retrieval-Augmented Generation.



13
14
15
# File 'lib/mindee/input/inference_parameters.rb', line 13

def rag
  @rag
end

#raw_textBoolean? (readonly)

Returns Extract the full text content from the document as strings, and fill the raw_text` attribute.

Returns:

  • (Boolean, nil)

    Extract the full text content from the document as strings, and fill the raw_text` attribute.



17
18
19
# File 'lib/mindee/input/inference_parameters.rb', line 17

def raw_text
  @raw_text
end

#text_contextString? (readonly)

Returns Additional text context used by the model during inference. Not recommended, for specific use only.

Returns:

  • (String, nil)

    Additional text context used by the model during inference. Not recommended, for specific use only.



32
33
34
# File 'lib/mindee/input/inference_parameters.rb', line 32

def text_context
  @text_context
end

#webhook_idsArray<String>? (readonly)

Returns Optional list of Webhooks IDs to propagate the API response to.

Returns:

  • (Array<String>, nil)

    Optional list of Webhooks IDs to propagate the API response to.



35
36
37
# File 'lib/mindee/input/inference_parameters.rb', line 35

def webhook_ids
  @webhook_ids
end

Class Method Details

.from_hash(params: {}) ⇒ InferenceParameters

Loads a prediction from a Hash.

Parameters:

  • params (Hash) (defaults to: {})

    Parameters to provide as a hash.

Returns:



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/mindee/input/inference_parameters.rb', line 108

def self.from_hash(params: {})
  params.transform_keys!(&:to_sym)

  if params.empty? || params[:model_id].nil? || params[:model_id].empty?
    raise Errors::MindeeInputError, 'Model ID is required.'
  end

  model_id = params.fetch(:model_id)
  rag = params.fetch(:rag, nil)
  raw_text = params.fetch(:raw_text, nil)
  polygon = params.fetch(:polygon, nil)
  confidence = params.fetch(:confidence, nil)
  file_alias = params.fetch(:file_alias, nil)
  webhook_ids = params.fetch(:webhook_ids, [])
  polling_options_input = params.fetch(:page_options, PollingOptions.new)
  if polling_options_input.is_a?(Hash)
    polling_options_input = polling_options_input.transform_keys(&:to_sym)
    PollingOptions.new(
      initial_delay_sec: polling_options_input.fetch(:initial_delay_sec, 2.0),
      delay_sec: polling_options_input.fetch(:delay_sec, 1.5),
      max_retries: polling_options_input.fetch(:max_retries, 80)
    )
  end
  close_file = params.fetch(:close_file, true)
  InferenceParameters.new(model_id, rag: rag, raw_text: raw_text, polygon: polygon, confidence: confidence,
                                    file_alias: file_alias, webhook_ids: webhook_ids, close_file: close_file)
end

Instance Method Details

#validate_async_paramsObject

Validates the parameters for async auto-polling

Raises:

  • (ArgumentError)


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/mindee/input/inference_parameters.rb', line 86

def validate_async_params
  min_delay_sec = 1
  min_initial_delay_sec = 1
  min_retries = 2

  if @polling_options.delay_sec < min_delay_sec
    raise ArgumentError,
          "Cannot set auto-poll delay to less than #{min_delay_sec} second(s)"
  end
  if @polling_options.initial_delay_sec < min_initial_delay_sec
    raise ArgumentError,
          "Cannot set initial parsing delay to less than #{min_initial_delay_sec} second(s)"
  end
  return unless @polling_options.max_retries < min_retries

  raise ArgumentError,
        "Cannot set auto-poll retries to less than #{min_retries}"
end