Skip to content

Commit a1185f4

Browse files
✨ add text context option return param (#218)
1 parent 9d296c7 commit a1185f4

File tree

8 files changed

+83
-3
lines changed

8 files changed

+83
-3
lines changed

lib/mindee/parsing/v2/inference_active_options.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@ class InferenceActiveOptions
1313
attr_reader :confidence
1414
# @return [Boolean] Whether the Retrieval-Augmented Generation feature was activated.
1515
attr_reader :rag
16+
# @return [Boolean] Whether the text context feature was activated.
17+
attr_reader :text_context
1618

1719
# @param server_response [Hash] Raw JSON parsed into a Hash.
1820
def initialize(server_response)
1921
@raw_text = server_response['raw_text']
2022
@polygon = server_response['polygon']
2123
@confidence = server_response['confidence']
2224
@rag = server_response['rag']
25+
@text_context = server_response['text_context']
2326
end
2427

2528
# String representation.

sig/mindee/input/local_response.rbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module Mindee
33
module Input
44
class LocalResponse
55
def file: -> StringIO
6-
def initialize: (File | IO | StringIO | String | Pathname) -> void
6+
def initialize: (File | IO | StringIO | String | Pathname | Tempfile) -> void
77
def as_hash: -> Hash[String | Symbol, untyped]
88
def self.process_secret_key: (String) -> String
99
def get_hmac_signature: (String) -> String

sig/mindee/parsing/v2/inference_active_options.rbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module Mindee
66
attr_reader polygon: bool
77
attr_reader rag: bool
88
attr_reader raw_text: bool
9+
attr_reader text_context: bool
910

1011
def initialize: (Hash[String | Symbol, untyped]) -> void
1112
end
File renamed without changes.

spec/v2/client_v2_integration.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
polygon: false,
2525
confidence: false,
2626
file_alias: 'ruby-integration-test',
27-
polling_options: polling
27+
polling_options: polling,
28+
text_context: 'this is a test'
2829
)
2930

3031
response = client.enqueue_and_get_inference(input, inference_params)
@@ -50,6 +51,7 @@
5051
expect(active_options.polygon).to eq(false)
5152
expect(active_options.confidence).to eq(false)
5253
expect(active_options.rag).to eq(false)
54+
expect(active_options.text_context).to eq(true)
5355

5456
result = response.inference.result
5557
expect(result).not_to be_nil
@@ -94,6 +96,7 @@
9496
expect(active_options.polygon).to eq(false)
9597
expect(active_options.confidence).to eq(false)
9698
expect(active_options.rag).to eq(false)
99+
expect(active_options.text_context).to eq(false)
97100

98101
result = response.inference.result
99102
expect(result).not_to be_nil
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# frozen_string_literal: true
2+
3+
require 'mindee/input/local_response'
4+
require_relative '../../data'
5+
6+
def assert_local_response(local_response)
7+
dummy_secret_key = 'ogNjY44MhvKPGTtVsI8zG82JqWQa68woYQH'
8+
signature = 'b82a515c832fd2c4f4ce3a7e6f53c12e8d10e19223f6cf0e3a9809a7a3da26be'
9+
expect(local_response.file).to_not be(nil)
10+
expect(local_response.valid_hmac_signature?(
11+
dummy_secret_key, 'invalid signature'
12+
)).to be(false)
13+
expect(local_response.get_hmac_signature(dummy_secret_key)).to eq(signature)
14+
inference_response = local_response.deserialize_response(Mindee::Parsing::V2::InferenceResponse)
15+
expect(inference_response).to be_a(Mindee::Parsing::V2::InferenceResponse)
16+
expect(inference_response).not_to be_nil
17+
expect(inference_response.inference).not_to be_nil
18+
end
19+
20+
describe Mindee::Input::LocalResponse do
21+
let(:file_path) { File.join(V2_DATA_DIR, 'inference', 'standard_field_types.json') }
22+
context 'A V2 local response' do
23+
it 'should load from a path' do
24+
response = Mindee::Input::LocalResponse.new(file_path)
25+
assert_local_response(response)
26+
end
27+
28+
it 'should load from a string' do
29+
str_file = File.read(file_path)
30+
response = Mindee::Input::LocalResponse.new(str_file)
31+
assert_local_response(response)
32+
end
33+
34+
it 'should load from a StringIO' do
35+
strio_file = StringIO.new(File.read(file_path))
36+
response = Mindee::Input::LocalResponse.new(strio_file)
37+
assert_local_response(response)
38+
end
39+
40+
it 'should load from a file-like object' do
41+
str_file = File.read(file_path)
42+
Tempfile.open do |tempfile|
43+
tempfile.write(str_file)
44+
tempfile.rewind
45+
response = Mindee::Input::LocalResponse.new(tempfile)
46+
assert_local_response(response)
47+
end
48+
end
49+
50+
it 'should trigger an error when something invalid is passed' do
51+
expect do
52+
Mindee::Input::LocalResponse.new(123)
53+
end.to raise_error Mindee::Errors::MindeeInputError
54+
end
55+
56+
it 'should trigger an error when the payload is not hashable' do
57+
local_response = Mindee::Input::LocalResponse.new('Your mother was a hamster.')
58+
expect do
59+
local_response.as_hash
60+
end.to raise_error Mindee::Errors::MindeeInputError
61+
end
62+
end
63+
end

spec/v2/parsing/inference_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
let(:complete_path) { File.join(findoc_path, 'complete.json') }
1616
let(:rag_matched_path) { File.join(inference_path, 'rag_matched.json') }
1717
let(:rag_not_matched_path) { File.join(inference_path, 'rag_not_matched.json') }
18+
let(:text_context_path) { File.join(inference_path, 'text_context_enabled.json') }
1819

1920
def load_v2_inference(resource_path)
2021
local_response = Mindee::Input::LocalResponse.new(resource_path)
@@ -81,6 +82,7 @@ def load_v2_inference(resource_path)
8182
expect(active_options.raw_text).to eq(false)
8283
expect(active_options.polygon).to eq(false)
8384
expect(active_options.confidence).to eq(false)
85+
expect(active_options.text_context).to eq(false)
8486
expect(active_options.rag).to eq(false)
8587

8688
fields = inference.result.fields
@@ -363,4 +365,12 @@ def load_standard_fields
363365
expect(response.inference.result.rag.retrieved_document_id).to be_nil
364366
end
365367
end
368+
369+
describe 'text context' do
370+
it 'when enabled' do
371+
response = load_v2_inference(text_context_path)
372+
expect(response.inference).not_to be_nil
373+
expect(response.inference.active_options.text_context).to be_truthy
374+
end
375+
end
366376
end

0 commit comments

Comments
 (0)