Skip to content

Commit 9d296c7

Browse files
authored
✨ add text_context option to v2 inference (#217)
1 parent 043dc5f commit 9d296c7

File tree

6 files changed

+65
-30
lines changed

6 files changed

+65
-30
lines changed

lib/mindee/http/mindee_api_v2.rb

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,25 @@ def inference_result_req_get(queue_id)
111111
poll("#{@settings.base_url}/inferences/#{queue_id}")
112112
end
113113

114+
# Handle parameters for the enqueue form
115+
# @param form_data [Array] Array of form fields
116+
# @param params [Input::InferenceParameters] Inference options.
117+
def enqueue_form_options(form_data, params)
118+
# deal with optional features
119+
form_data.push(['rag', params.rag.to_s]) unless params.rag.nil?
120+
form_data.push(['raw_text', params.raw_text.to_s]) unless params.raw_text.nil?
121+
form_data.push(['polygon', params.polygon.to_s]) unless params.polygon.nil?
122+
form_data.push(['confidence', params.confidence.to_s]) unless params.confidence.nil?
123+
form_data.push ['file_alias', params.file_alias] if params.file_alias
124+
form_data.push ['text_context', params.text_context] if params.text_context
125+
unless params.webhook_ids.nil? || params.webhook_ids.empty?
126+
form_data.push ['webhook_ids', params.webhook_ids.join(',')]
127+
end
128+
form_data
129+
end
130+
114131
# @param input_source [Mindee::Input::Source::LocalInputSource, Mindee::Input::Source::URLInputSource]
115-
# @param params [Input::InferenceParameters] Parse options.
132+
# @param params [Input::InferenceParameters] Inference options.
116133
# @return [Net::HTTPResponse, nil]
117134
def enqueue(input_source, params)
118135
uri = URI("#{@settings.base_url}/inferences/enqueue")
@@ -125,16 +142,9 @@ def enqueue(input_source, params)
125142
end
126143
form_data.push(['model_id', params.model_id])
127144

128-
# deal with optional features
129-
form_data.push(['rag', params.rag.to_s]) unless params.rag.nil?
130-
form_data.push(['raw_text', params.raw_text.to_s]) unless params.raw_text.nil?
131-
form_data.push(['polygon', params.polygon.to_s]) unless params.polygon.nil?
132-
form_data.push(['confidence', params.confidence.to_s]) unless params.confidence.nil?
145+
# deal with other parameters
146+
form_data = enqueue_form_options(form_data, params)
133147

134-
form_data.push ['file_alias', params.file_alias] if params.file_alias
135-
unless params.webhook_ids.nil? || params.webhook_ids.empty?
136-
form_data.push ['webhook_ids', params.webhook_ids.join(',')]
137-
end
138148
headers = {
139149
'Authorization' => @settings.api_key,
140150
'User-Agent' => @settings.user_agent,

lib/mindee/input/inference_parameters.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ class InferenceParameters
2525
# @return [String, nil] Optional alias for the file.
2626
attr_reader :file_alias
2727

28+
# @return [String, nil] Additional text context used by the model during inference.
29+
# Not recommended, for specific use only.
30+
attr_reader :text_context
31+
2832
# @return [Array<String>, nil] Optional list of Webhooks IDs to propagate the API response to.
2933
attr_reader :webhook_ids
3034

@@ -52,6 +56,7 @@ def initialize(
5256
confidence: nil,
5357
file_alias: nil,
5458
webhook_ids: nil,
59+
text_context: nil,
5560
polling_options: nil,
5661
close_file: true
5762
)
@@ -64,6 +69,7 @@ def initialize(
6469
@confidence = confidence
6570
@file_alias = file_alias
6671
@webhook_ids = webhook_ids || []
72+
@text_context = text_context
6773
@polling_options = get_clean_polling_options(polling_options)
6874
@close_file = close_file.nil? || close_file
6975
# rubocop:enable Metrics/ParameterLists

sig/mindee/http/mindee_api_v2.rbs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ module Mindee
1313
def inference_job_req_get: (String) -> Net::HTTPResponse
1414
def inference_result_req_get: (String) -> Net::HTTPResponse
1515
def enqueue: (Input::Source::LocalInputSource | Input::Source::URLInputSource, Input::InferenceParameters) -> Net::HTTPResponse?
16+
17+
private
18+
19+
def enqueue_form_options: (Array[untyped], Input::InferenceParameters) -> Array[untyped]
1620
end
1721
end
1822
end

sig/mindee/input/inference_parameters.rbs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module Mindee
1010
attr_reader polygon: bool?
1111
attr_reader rag: bool?
1212
attr_reader raw_text: bool?
13+
attr_reader text_context: String?
1314
attr_reader webhook_ids: Array[String]?
1415

1516
def initialize: (
@@ -19,6 +20,7 @@ module Mindee
1920
?polygon: bool?,
2021
?confidence: bool?,
2122
?file_alias: String?,
23+
?text_context: String?,
2224
?webhook_ids: Array[String]?,
2325
?polling_options: Hash[Symbol | String, untyped] | PollingOptions?,
2426
?close_file: bool?

spec/v2/client_v2_integration.rb

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
max_retries: 80
1818
)
1919

20-
params = Mindee::Input::InferenceParameters.new(
20+
inference_params = Mindee::Input::InferenceParameters.new(
2121
model_id,
2222
rag: false,
2323
raw_text: true,
@@ -27,7 +27,7 @@
2727
polling_options: polling
2828
)
2929

30-
response = client.enqueue_and_get_inference(input, params)
30+
response = client.enqueue_and_get_inference(input, inference_params)
3131

3232
expect(response).not_to be_nil
3333
expect(response.inference).not_to be_nil
@@ -64,7 +64,7 @@
6464
src_path = File.join(V1_PRODUCT_DATA_DIR, 'financial_document', 'default_sample.jpg')
6565
input = Mindee::Input::Source::FileInputSource.new(File.open(src_path, 'rb'), 'default_sample.jpg')
6666

67-
params = Mindee::Input::InferenceParameters.new(
67+
inference_params = Mindee::Input::InferenceParameters.new(
6868
model_id,
6969
raw_text: false,
7070
polygon: false,
@@ -73,7 +73,7 @@
7373
file_alias: 'ruby-integration-test'
7474
)
7575

76-
response = client.enqueue_and_get_inference(input, params)
76+
response = client.enqueue_and_get_inference(input, inference_params)
7777
expect(response).not_to be_nil
7878

7979
file = response.inference.file
@@ -111,19 +111,21 @@
111111
src_path = File.join(FILE_TYPES_DIR, 'pdf', 'blank_1.pdf')
112112
input = Mindee::Input::Source::FileInputSource.new(File.open(src_path, 'rb'), 'blank_1.pdf')
113113

114-
params = Mindee::Input::InferenceParameters.new('INVALID_MODEL_ID')
114+
inference_params = Mindee::Input::InferenceParameters.new('INVALID_MODEL_ID')
115115

116116
expect do
117-
client.enqueue_inference(input, params)
117+
client.enqueue_inference(input, inference_params)
118118
end.to raise_error(Mindee::Errors::MindeeHTTPErrorV2) { |e| expect(e.status).to eq(422) }
119119
end
120120

121121
it 'raises MindeeHTTPErrorV2 (422) on invalid webhook id' do
122122
src_path = File.join(FILE_TYPES_DIR, 'pdf', 'blank_1.pdf')
123123
input = Mindee::Input::Source::FileInputSource.new(File.open(src_path, 'rb'), 'blank_1.pdf')
124124

125-
params = Mindee::Input::InferenceParameters.new(model_id,
126-
webhook_ids: ['INVALID_WEBHOOK_ID'])
125+
params = Mindee::Input::InferenceParameters.new(
126+
model_id,
127+
webhook_ids: ['INVALID_WEBHOOK_ID']
128+
)
127129

128130
expect do
129131
client.enqueue_inference(input, params)
@@ -141,12 +143,13 @@
141143
src_path = File.join(FILE_TYPES_DIR, 'pdf', 'blank_1.pdf')
142144
input = Mindee::Input::Source::FileInputSource.new(File.open(src_path, 'rb'), 'blank_1.pdf')
143145

144-
params = Mindee::Input::InferenceParameters.new(model_id,
145-
webhook_ids: ['fc405e37-4ba4-4d03-aeba-533a8d1f0f21',
146-
'fc405e37-4ba4-4d03-aeba-533a8d1f0f21'])
146+
inference_params = Mindee::Input::InferenceParameters.new(
147+
model_id,
148+
webhook_ids: ['fc405e37-4ba4-4d03-aeba-533a8d1f0f21', 'fc405e37-4ba4-4d03-aeba-533a8d1f0f21']
149+
)
147150

148151
expect do
149-
client.enqueue_inference(input, params)
152+
client.enqueue_inference(input, inference_params)
150153
end.to raise_error(Mindee::Errors::MindeeHTTPErrorV2) { |e|
151154
expect(e.status).to eq(422)
152155
expect(e.code).to start_with('422-')
@@ -174,17 +177,20 @@
174177
it 'raises on invalid model ID' do
175178
expect do
176179
src_path = File.join(V1_PRODUCT_DATA_DIR, 'financial_document', 'default_sample.jpg')
177-
input = Mindee::Input::Source::FileInputSource.new(File.open(src_path, 'rb'), 'default_sample.jpg')
180+
input = Mindee::Input::Source::FileInputSource.new(
181+
File.open(src_path, 'rb'),
182+
'default_sample.jpg'
183+
)
178184

179-
params = Mindee::Input::InferenceParameters.new(
185+
inference_params = Mindee::Input::InferenceParameters.new(
180186
'fc405e37-4ba4-4d03-aeba-533a8d1f0f21',
181187
raw_text: false,
182188
polygon: false,
183189
confidence: false,
184190
rag: false,
185191
file_alias: 'ruby-integration-test'
186192
)
187-
client.enqueue_and_get_inference(input, params)
193+
client.enqueue_and_get_inference(input, inference_params)
188194
end.to raise_error(Mindee::Errors::MindeeHTTPErrorV2) { |e|
189195
expect(e.status).to eq(404)
190196
expect(e.code).to start_with('404-')
@@ -199,9 +205,9 @@
199205
it 'parses an URL input source without errors' do
200206
url_input = Mindee::Input::Source::URLInputSource.new(blank_pdf_url)
201207

202-
params = Mindee::Input::InferenceParameters.new(model_id)
208+
inference_params = Mindee::Input::InferenceParameters.new(model_id)
203209

204-
response = client.enqueue_and_get_inference(url_input, params)
210+
response = client.enqueue_and_get_inference(url_input, inference_params)
205211

206212
expect(response).not_to be_nil
207213
expect(response.inference).not_to be_nil

spec/v2/client_v2_spec.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,12 @@ def stub_next_request_with(method, hash:, status_code: 0)
4545
it 'enqueue(path) raises MindeeHTTPErrorV2 on 4xx' do
4646
expect do
4747
stub_next_request_with(:enqueue, hash: JSON.generate(json400))
48-
client.enqueue_inference(input_doc, model_id: 'dummy-model')
48+
inference_params = Mindee::Input::InferenceParameters.new(
49+
'dummy-model',
50+
raw_text: false,
51+
text_context: 'Hello my name is mud.'
52+
)
53+
client.enqueue_inference(input_doc, inference_params)
4954
end.to raise_error(Mindee::Errors::MindeeHTTPErrorV2) { |e|
5055
expect(e.status).to eq(400)
5156
expect(e.detail).to eq('Unsupported content.')
@@ -55,7 +60,8 @@ def stub_next_request_with(method, hash:, status_code: 0)
5560
it 'enqueue_and_get_inference(path) raises MindeeHTTPErrorV2 on 4xx' do
5661
expect do
5762
stub_next_request_with(:enqueue, hash: JSON.generate(json400))
58-
client.enqueue_and_get_inference(input_doc, model_id: 'dummy-model')
63+
inference_params = Mindee::Input::InferenceParameters.new('dummy-model')
64+
client.enqueue_and_get_inference(input_doc, inference_params)
5965
end.to raise_error(Mindee::Errors::MindeeHTTPErrorV2) { |e|
6066
expect(e.status).to eq(400)
6167
expect(e.detail).to eq('Unsupported content.')
@@ -67,7 +73,8 @@ def stub_next_request_with(method, hash:, status_code: 0)
6773

6874
expect do
6975
stub_next_request_with(:enqueue, hash: JSON.generate(error_hash))
70-
client.enqueue_inference(input_doc, model_id: 'dummy-model')
76+
inference_params = Mindee::Input::InferenceParameters.new('dummy-model')
77+
client.enqueue_inference(input_doc, inference_params)
7178
end.to raise_error(Mindee::Errors::MindeeHTTPErrorV2) { |e|
7279
expect(e.status).to eq(413)
7380
expect(e.detail).to include('File exceeds size limit')

0 commit comments

Comments
 (0)