Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/mindee/parsing/v2/inference_active_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ class InferenceActiveOptions
attr_reader :confidence
# @return [Boolean] Whether the Retrieval-Augmented Generation feature was activated.
attr_reader :rag
# @return [Boolean] Whether the text context feature was activated.
attr_reader :text_context

# @param server_response [Hash] Raw JSON parsed into a Hash.
def initialize(server_response)
@raw_text = server_response['raw_text']
@polygon = server_response['polygon']
@confidence = server_response['confidence']
@rag = server_response['rag']
@text_context = server_response['text_context']
end

# String representation.
Expand Down
2 changes: 1 addition & 1 deletion sig/mindee/input/local_response.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Mindee
module Input
class LocalResponse
def file: -> StringIO
def initialize: (File | IO | StringIO | String | Pathname) -> void
def initialize: (File | IO | StringIO | String | Pathname | Tempfile) -> void
def as_hash: -> Hash[String | Symbol, untyped]
def self.process_secret_key: (String) -> String
def get_hmac_signature: (String) -> String
Expand Down
1 change: 1 addition & 0 deletions sig/mindee/parsing/v2/inference_active_options.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Mindee
attr_reader polygon: bool
attr_reader rag: bool
attr_reader raw_text: bool
attr_reader text_context: bool

def initialize: (Hash[String | Symbol, untyped]) -> void
end
Expand Down
5 changes: 4 additions & 1 deletion spec/v2/client_v2_integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
polygon: false,
confidence: false,
file_alias: 'ruby-integration-test',
polling_options: polling
polling_options: polling,
text_context: 'this is a test'
)

response = client.enqueue_and_get_inference(input, inference_params)
Expand All @@ -50,6 +51,7 @@
expect(active_options.polygon).to eq(false)
expect(active_options.confidence).to eq(false)
expect(active_options.rag).to eq(false)
expect(active_options.text_context).to eq(true)

result = response.inference.result
expect(result).not_to be_nil
Expand Down Expand Up @@ -94,6 +96,7 @@
expect(active_options.polygon).to eq(false)
expect(active_options.confidence).to eq(false)
expect(active_options.rag).to eq(false)
expect(active_options.text_context).to eq(false)

result = response.inference.result
expect(result).not_to be_nil
Expand Down
63 changes: 63 additions & 0 deletions spec/v2/input/local_response_v2_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# frozen_string_literal: true

require 'mindee/input/local_response'
require_relative '../../data'

def assert_local_response(local_response)
dummy_secret_key = 'ogNjY44MhvKPGTtVsI8zG82JqWQa68woYQH'
signature = 'b82a515c832fd2c4f4ce3a7e6f53c12e8d10e19223f6cf0e3a9809a7a3da26be'
expect(local_response.file).to_not be(nil)
expect(local_response.valid_hmac_signature?(
dummy_secret_key, 'invalid signature'
)).to be(false)
expect(local_response.get_hmac_signature(dummy_secret_key)).to eq(signature)
inference_response = local_response.deserialize_response(Mindee::Parsing::V2::InferenceResponse)
expect(inference_response).to be_a(Mindee::Parsing::V2::InferenceResponse)
expect(inference_response).not_to be_nil
expect(inference_response.inference).not_to be_nil
end

describe Mindee::Input::LocalResponse do
let(:file_path) { File.join(V2_DATA_DIR, 'inference', 'standard_field_types.json') }
context 'A V2 local response' do
it 'should load from a path' do
response = Mindee::Input::LocalResponse.new(file_path)
assert_local_response(response)
end

it 'should load from a string' do
str_file = File.read(file_path)
response = Mindee::Input::LocalResponse.new(str_file)
assert_local_response(response)
end

it 'should load from a StringIO' do
strio_file = StringIO.new(File.read(file_path))
response = Mindee::Input::LocalResponse.new(strio_file)
assert_local_response(response)
end

it 'should load from a file-like object' do
str_file = File.read(file_path)
Tempfile.open do |tempfile|
tempfile.write(str_file)
tempfile.rewind
response = Mindee::Input::LocalResponse.new(tempfile)
assert_local_response(response)
end
end

it 'should trigger an error when something invalid is passed' do
expect do
Mindee::Input::LocalResponse.new(123)
end.to raise_error Mindee::Errors::MindeeInputError
end

it 'should trigger an error when the payload is not hashable' do
local_response = Mindee::Input::LocalResponse.new('Your mother was a hamster.')
expect do
local_response.as_hash
end.to raise_error Mindee::Errors::MindeeInputError
end
end
end
10 changes: 10 additions & 0 deletions spec/v2/parsing/inference_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
let(:complete_path) { File.join(findoc_path, 'complete.json') }
let(:rag_matched_path) { File.join(inference_path, 'rag_matched.json') }
let(:rag_not_matched_path) { File.join(inference_path, 'rag_not_matched.json') }
let(:text_context_path) { File.join(inference_path, 'text_context_enabled.json') }

def load_v2_inference(resource_path)
local_response = Mindee::Input::LocalResponse.new(resource_path)
Expand Down Expand Up @@ -81,6 +82,7 @@ def load_v2_inference(resource_path)
expect(active_options.raw_text).to eq(false)
expect(active_options.polygon).to eq(false)
expect(active_options.confidence).to eq(false)
expect(active_options.text_context).to eq(false)
expect(active_options.rag).to eq(false)

fields = inference.result.fields
Expand Down Expand Up @@ -363,4 +365,12 @@ def load_standard_fields
expect(response.inference.result.rag.retrieved_document_id).to be_nil
end
end

describe 'text context' do
it 'when enabled' do
response = load_v2_inference(text_context_path)
expect(response.inference).not_to be_nil
expect(response.inference.active_options.text_context).to be_truthy
end
end
end