Skip to content

Commit 3a3e729

Browse files
committed
✨ add optional features
1 parent 625d428 commit 3a3e729

File tree

4 files changed

+111
-29
lines changed

4 files changed

+111
-29
lines changed

lib/mindee/http/mindee_api_v2.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,13 @@ def enqueue(input_source, params)
124124
[['file', file_data, file_metadata]] # : Array[untyped]
125125
end
126126
form_data.push ['model_id', params.model_id]
127-
form_data.push ['rag', 'true'] if params.rag
127+
128+
# deal with optional features
129+
form_data.push ['rag', params.rag.to_s.downcase] unless params.rag.nil?
130+
form_data.push ['raw_text', params.rag.to_s.downcase] unless params.raw_text.nil?
131+
form_data.push ['polygon', params.rag.to_s.downcase] unless params.polygon.nil?
132+
form_data.push ['confidence', params.rag.to_s.downcase] unless params.confidence.nil?
133+
128134
form_data.push ['file_alias', params.file_alias] if params.file_alias
129135
unless params.webhook_ids.nil? || params.webhook_ids.empty?
130136
form_data.push ['webhook_ids', params.webhook_ids.join(',')]

lib/mindee/input/inference_parameters.rb

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,19 @@ class InferenceParameters
77
# @return [String] ID of the model (required).
88
attr_reader :model_id
99

10-
# @return [Boolean, nil] Enable Retrieval-Augmented Generation.
10+
# @return [Boolean, nil] Enhance extraction accuracy with Retrieval-Augmented Generation.
1111
attr_reader :rag
1212

13+
# @return [Boolean, nil] Extract the full text content from the document as strings, and fill the raw_text` attribute.
14+
attr_reader :raw_text
15+
16+
# @return [Boolean, nil] Calculate bounding box polygons for all fields, and fill their `locations` attribute.
17+
attr_reader :polygon
18+
19+
# @return [Boolean, nil] Boost the precision and accuracy of all extractions.
20+
# Calculate confidence scores for all fields, and fill their confidence attribute.
21+
attr_reader :confidence
22+
1323
# @return [String, nil] Optional alias for the file.
1424
attr_reader :file_alias
1525

@@ -23,16 +33,32 @@ class InferenceParameters
2333
attr_reader :close_file
2434

2535
# @param [String] model_id ID of the model
26-
# @param [FalseClass] rag Whether to enable rag.
36+
# @param [nil] rag Whether to enable RAG.
37+
# @param [nil] raw_text Whether to enable rax text.
38+
# @param [nil] polygon Whether to enable polygons.
39+
# @param [nil] confidence Whether to enable confidence scores.
2740
# @param [nil] file_alias File alias, if applicable.
2841
# @param [nil] webhook_ids
2942
# @param [nil] polling_options
3043
# @param [TrueClass] close_file
31-
def initialize(model_id, rag: false, file_alias: nil, webhook_ids: nil, polling_options: nil, close_file: true)
44+
def initialize(
45+
model_id,
46+
rag: nil,
47+
raw_text: nil,
48+
polygon: nil,
49+
confidence: nil,
50+
file_alias: nil,
51+
webhook_ids: nil,
52+
polling_options: nil,
53+
close_file: true
54+
)
3255
raise Errors::MindeeInputError, 'Model ID is required.' if model_id.empty? || model_id.nil?
3356

3457
@model_id = model_id
35-
@rag = rag || false
58+
@rag = rag
59+
@raw_text = raw_text
60+
@polygon = polygon
61+
@confidence = confidence
3662
@file_alias = file_alias
3763
@webhook_ids = webhook_ids || []
3864
@polling_options = get_clean_polling_options(polling_options)
@@ -70,7 +96,10 @@ def self.from_hash(params: {})
7096
end
7197

7298
model_id = params.fetch(:model_id)
73-
rag = params.fetch(:rag, false)
99+
rag = params.fetch(:rag, nil)
100+
raw_text = params.fetch(:raw_text, nil)
101+
polygon = params.fetch(:polygon, nil)
102+
confidence = params.fetch(:confidence, nil)
74103
file_alias = params.fetch(:file_alias, nil)
75104
webhook_ids = params.fetch(:webhook_ids, [])
76105
polling_options_input = params.fetch(:page_options, PollingOptions.new)
@@ -83,8 +112,8 @@ def self.from_hash(params: {})
83112
)
84113
end
85114
close_file = params.fetch(:close_file, true)
86-
InferenceParameters.new(model_id, rag: rag, file_alias: file_alias, webhook_ids: webhook_ids,
87-
close_file: close_file)
115+
InferenceParameters.new(model_id, rag: rag, raw_text: raw_text, polygon: polygon, confidence: confidence,
116+
file_alias: file_alias, webhook_ids: webhook_ids, close_file: close_file)
88117
end
89118

90119
private

sig/mindee/input/inference_parameters.rbs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,21 @@ module Mindee
33
module Input
44
class InferenceParameters
55
attr_reader close_file: bool
6+
attr_reader confidence: bool?
67
attr_reader file_alias: String?
78
attr_reader model_id: String
89
attr_reader polling_options: PollingOptions
10+
attr_reader polygon: bool?
911
attr_reader rag: bool?
12+
attr_reader raw_text: bool?
1013
attr_reader webhook_ids: Array[String]?
1114

1215
def initialize: (
1316
String,
1417
?rag: bool?,
18+
?raw_text: bool?,
19+
?polygon: bool?,
20+
?confidence: bool?,
1521
?file_alias: String?,
1622
?webhook_ids: Array[String]?,
1723
?polling_options: Hash[Symbol | String, untyped] | PollingOptions?,

spec/client_v2_integration.rb

Lines changed: 62 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,49 +17,90 @@
1717
max_retries: 80
1818
)
1919

20-
params = Mindee::Input::InferenceParameters.new(model_id,
21-
rag: false,
22-
file_alias: 'ruby-integration-test',
23-
polling_options: polling)
20+
params = Mindee::Input::InferenceParameters.new(
21+
model_id,
22+
rag: false,
23+
raw_text: true,
24+
polygon: false,
25+
confidence: false,
26+
file_alias: 'ruby-integration-test',
27+
polling_options: polling
28+
)
2429

2530
response = client.enqueue_and_get_inference(input, params)
2631

2732
expect(response).not_to be_nil
2833
expect(response.inference).not_to be_nil
2934

30-
expect(response.inference.file).not_to be_nil
31-
expect(response.inference.file.name).to eq('multipage_cut-2.pdf')
35+
file = response.inference.file
36+
expect(file).not_to be_nil
37+
expect(file).to be_a(Mindee::Parsing::V2::InferenceFile)
38+
expect(file.name).to eq('multipage_cut-2.pdf')
39+
expect(file.page_count).to eq(2)
40+
41+
model = response.inference.model
42+
expect(model).not_to be_nil
43+
expect(model).to be_a(Mindee::Parsing::V2::InferenceModel)
44+
expect(model.id).to eq(model_id)
3245

33-
expect(response.inference.model).not_to be_nil
34-
expect(response.inference.model.id).to eq(model_id)
46+
active_options = response.inference.active_options
47+
expect(active_options).not_to be_nil
48+
expect(active_options).to be_a(Mindee::Parsing::V2::InferenceActiveOptions)
49+
expect(active_options.raw_text).to eq(true)
50+
expect(active_options.polygon).to eq(false)
51+
expect(active_options.confidence).to eq(false)
52+
expect(active_options.rag).to eq(false)
3553

36-
expect(response.inference.active_options).not_to be_nil
54+
result = response.inference.result
55+
expect(result).not_to be_nil
3756

38-
expect(response.inference.result).not_to be_nil
39-
expect(response.inference.result.raw_text).to be_nil
40-
expect(response.inference.result.fields).not_to be_nil
57+
expect(result.raw_text).not_to be_nil
58+
expect(result.raw_text.pages.length).to eq(2)
59+
60+
expect(result.fields).not_to be_nil
4161
end
4262

4363
it 'parses a filled single-page image successfully' do
4464
src_path = File.join(__dir__ || './', 'data', 'products', 'financial_document', 'default_sample.jpg')
4565
input = Mindee::Input::Source::FileInputSource.new(File.open(src_path, 'rb'), 'default_sample.jpg')
4666

47-
params = Mindee::Input::InferenceParameters.new(model_id,
48-
rag: false,
49-
file_alias: 'ruby-integration-test')
67+
params = Mindee::Input::InferenceParameters.new(
68+
model_id,
69+
raw_text: false,
70+
polygon: false,
71+
confidence: false,
72+
rag: false,
73+
file_alias: 'ruby-integration-test'
74+
)
5075

5176
response = client.enqueue_and_get_inference(input, params)
5277
expect(response).not_to be_nil
5378

54-
expect(response.inference).not_to be_nil
55-
expect(response.inference.file.name).to eq('default_sample.jpg')
79+
file = response.inference.file
80+
expect(file).not_to be_nil
81+
expect(file).to be_a(Mindee::Parsing::V2::InferenceFile)
82+
expect(file.name).to eq('default_sample.jpg')
83+
expect(file.page_count).to eq(1)
84+
85+
model = response.inference.model
86+
expect(model).not_to be_nil
87+
expect(model).to be_a(Mindee::Parsing::V2::InferenceModel)
88+
expect(model.id).to eq(model_id)
89+
90+
active_options = response.inference.active_options
91+
expect(active_options).not_to be_nil
92+
expect(active_options).to be_a(Mindee::Parsing::V2::InferenceActiveOptions)
93+
expect(active_options.raw_text).to eq(true)
94+
expect(active_options.polygon).to eq(false)
95+
expect(active_options.confidence).to eq(false)
96+
expect(active_options.rag).to eq(false)
5697

57-
expect(response.inference.model).not_to be_nil
58-
expect(response.inference.model.id).to eq(model_id)
98+
result = response.inference.result
99+
expect(result).not_to be_nil
59100

60-
expect(response.inference.active_options).not_to be_nil
101+
expect(result.raw_text).to be_nil
61102

62-
fields = response.inference.result.fields
103+
fields = result.fields
63104
expect(fields).not_to be_nil
64105
expect(fields['supplier_name']).not_to be_nil
65106
expect(fields['supplier_name'].value).to eq('John Smith')

0 commit comments

Comments
 (0)