diff --git a/src/Parsing/V2/InferenceActiveOptions.php b/src/Parsing/V2/InferenceActiveOptions.php index a8adefae..e3a4c406 100644 --- a/src/Parsing/V2/InferenceActiveOptions.php +++ b/src/Parsing/V2/InferenceActiveOptions.php @@ -15,6 +15,7 @@ class InferenceActiveOptions { /** * @var boolean Whether the Retrieval-Augmented Generation feature was activated. + * When this feature is activated, the RAG pipeline is used to increase result accuracy. */ public bool $rag; @@ -24,14 +25,21 @@ class InferenceActiveOptions public bool $rawText; /** - * @var boolean Whether the polygon feature was activated. + * @var boolean Whether the Raw Text feature was activated. + * When this feature is activated, the raw text extracted from the document is returned in the result. */ public bool $polygon; /** * @var boolean Whether the confidence feature was activated. + * When this feature is activated, a confidence score for each field is returned in the result. */ public bool $confidence; + /** + * @var boolean Whether the text context feature was activated. + * When this feature is activated, the provided context is used to improve the accuracy of the inference. + */ + public bool $textContext; /** * @param array $serverResponse Raw server response array. @@ -42,6 +50,7 @@ public function __construct(array $serverResponse) $this->rawText = $serverResponse['raw_text']; $this->polygon = $serverResponse['polygon']; $this->confidence = $serverResponse['confidence']; + $this->textContext = $serverResponse['text_context']; } /** diff --git a/tests/Input/LocalResponseTest.php b/tests/V1/Input/LocalResponseV1Test.php similarity index 98% rename from tests/Input/LocalResponseTest.php rename to tests/V1/Input/LocalResponseV1Test.php index b89a76da..a0abae30 100644 --- a/tests/Input/LocalResponseTest.php +++ b/tests/V1/Input/LocalResponseV1Test.php @@ -1,11 +1,11 @@ filePath = \TestingUtilities::getV2DataDir() . '/inference/standard_field_types.json'; + } + + protected function assertLocalResponse(LocalResponse $localResponse): void + { + $fakeHMACSigning = "ogNjY44MhvKPGTtVsI8zG82JqWQa68woYQH"; + $signature = "b82a515c832fd2c4f4ce3a7e6f53c12e8d10e19223f6cf0e3a9809a7a3da26be"; + $reflectedLocalResponse = new \ReflectionClass($localResponse); + $reflectedFile = $reflectedLocalResponse->getProperty('file'); + $reflectedFile->setAccessible(true); + $this->assertNotNull($reflectedFile); + $this->assertFalse($localResponse->isValidHMACSignature($fakeHMACSigning, "fake HMAC signature")); + $this->assertEquals($signature, $localResponse->getHmacSignature($fakeHMACSigning)); + $this->assertTrue($localResponse->isValidHMACSignature($fakeHMACSigning, $signature)); + $response = $localResponse->deserializeResponse(InferenceResponse::class); + $this->assertInstanceOf(InferenceResponse::class, $response); + $this->assertNotNull($response->inference); + $this->assertNotNull($response->inference->result); + $this->assertNotNull($response->inference->result->fields); + } + + public function testValidFileLocalResponse(){ + $file = fopen($this->filePath, 'rb'); + $localResponse = new LocalResponse($file); + fclose($file); + $this->assertLocalResponse($localResponse); + } + + public function testValidPathLocalResponse(){ + $localResponse = new LocalResponse($this->filePath); + $this->assertLocalResponse($localResponse); + } + + public function testValidBytesLocalResponse(){ + $raw = file_get_contents($this->filePath); + $encoding = mb_detect_encoding($raw, ['UTF-8','UTF-16','UTF-32','ISO-8859-1','Windows-1252'], true) ?: 'UTF-8'; + $utf8 = ($encoding === 'UTF-8') ? $raw : mb_convert_encoding($raw, 'UTF-8', $encoding); + $utf8 = preg_replace('/^\xEF\xBB\xBF/', '', $utf8); + $localResponse = new LocalResponse($utf8); + $this->assertLocalResponse($localResponse); + } +} diff --git a/tests/V2/Parsing/InferenceResponseTest.php b/tests/V2/Parsing/InferenceResponseTest.php index f0d9abeb..cd5af67e 100644 --- a/tests/V2/Parsing/InferenceResponseTest.php +++ b/tests/V2/Parsing/InferenceResponseTest.php @@ -365,6 +365,13 @@ public function testCoordinatesAndLocationDataMustBeAccessible(): void $this->assertTrue(FieldConfidence::Low->lessThanOrEqual($dateField->confidence)); $this->assertTrue(FieldConfidence::Medium->lessThanOrEqual($dateField->confidence)); $this->assertEquals('Medium', $dateField->confidence->value); + + $activeOptions = $inference->activeOptions; + $this->assertTrue($activeOptions->polygon); + $this->assertFalse($activeOptions->confidence); + $this->assertFalse($activeOptions->rag); + $this->assertFalse($activeOptions->rawText); + $this->assertFalse($activeOptions->textContext); } public function testRagMetadataWhenMatched() @@ -394,4 +401,30 @@ public function testShouldLoadWith422Error() $this->assertEquals(1, count($response->job->error->errors)); $this->assertInstanceOf(ErrorItem::class, $response->job->error->errors[0]); } + + public function testTextContextIsTrue(): void + { + $response = $this->loadFromResource('v2/inference/text_context_enabled.json'); + $inference = $response->inference; + $this->assertNotNull($inference); + $activeOptions = $inference->activeOptions; + $this->assertFalse($activeOptions->polygon); + $this->assertFalse($activeOptions->confidence); + $this->assertFalse($activeOptions->rag); + $this->assertFalse($activeOptions->rawText); + $this->assertTrue($activeOptions->textContext); + } + + public function testTextContextIsFalse(): void + { + $response = $this->loadFromResource('v2/products/financial_document/complete.json'); + $inference = $response->inference; + $this->assertNotNull($inference); + $activeOptions = $inference->activeOptions; + $this->assertFalse($activeOptions->polygon); + $this->assertFalse($activeOptions->confidence); + $this->assertFalse($activeOptions->rag); + $this->assertFalse($activeOptions->rawText); + $this->assertFalse($activeOptions->textContext); + } } diff --git a/tests/resources b/tests/resources index b0d725b7..f86f3eaf 160000 --- a/tests/resources +++ b/tests/resources @@ -1 +1 @@ -Subproject commit b0d725b71784a45db611c325739320b6c192b7e5 +Subproject commit f86f3eaf540f0babeb3d4f1a458d764856a2170b