Skip to content

Commit e025221

Browse files
committed
:sprakles: add inference options
1 parent 7c4a2a3 commit e025221

5 files changed

Lines changed: 161 additions & 49 deletions

File tree

src/Http/MindeeApiV2.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,17 @@ private function documentEnqueuePost(
311311
$postFields['file'] = $inputSource->fileObject;
312312
}
313313

314-
if ($params->rag) {
315-
$postFields['rag'] = 'true';
314+
if ($params->rawText != null) {
315+
$postFields['raw_text'] = strtolower($params->rawText);
316+
}
317+
if ($params->polygon != null) {
318+
$postFields['polygon'] = strtolower($params->polygon);
319+
}
320+
if ($params->confidence != null) {
321+
$postFields['confidence'] = strtolower($params->confidence);
322+
}
323+
if ($params->rag != null) {
324+
$postFields['rag'] = strtolower($params->rag);
316325
}
317326

318327
$url = $this->baseUrl . '/inferences/enqueue';

src/Input/InferenceParameters.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,25 @@ class InferenceParameters
1313
public string $modelId;
1414

1515
/**
16-
* @var boolean Whether to enable Retrieval-Augmented Generation.
16+
* @var boolean|null Enhance extraction accuracy with Retrieval-Augmented Generation..
1717
*/
18-
public bool $rag;
18+
public ?bool $rag;
19+
20+
/**
21+
* @var boolean|null Extract the full text content from the document as strings.
22+
*/
23+
public ?bool $rawText;
24+
25+
/**
26+
* @var boolean|null Calculate bounding box polygons for all fields.
27+
*/
28+
public ?bool $polygon;
29+
30+
/**
31+
* @var boolean|null Boost the precision and accuracy of all extractions.
32+
* Calculate confidence scores for all fields.
33+
*/
34+
public ?bool $confidence;
1935

2036
/**
2137
* @var string|null Optional file alias.
@@ -40,6 +56,9 @@ class InferenceParameters
4056
/**
4157
* @param string $modelId ID of the model.
4258
* @param boolean|null $rag Whether to enable Retrieval-Augmented Generation.
59+
* @param boolean|null $rawText Whether to extract the full text content from the document as strings.
60+
* @param boolean|null $polygon Whether to calculate bounding box polygons for all fields.
61+
* @param boolean|null $confidence Whether to calculate confidence scores for all fields.
4362
* @param string|null $alias Optional file alias.
4463
* @param array<string>|null $webhooksIds List of webhook IDs.
4564
* @param PollingOptions|null $pollingOptions Polling options.
@@ -48,6 +67,9 @@ class InferenceParameters
4867
public function __construct(
4968
string $modelId,
5069
?bool $rag = null,
70+
?bool $rawText = null,
71+
?bool $polygon = null,
72+
?bool $confidence = null,
5173
?string $alias = null,
5274
?array $webhooksIds = null,
5375
?PollingOptions $pollingOptions = null,
@@ -58,7 +80,11 @@ public function __construct(
5880
$pollingOptions = new PollingOptions();
5981
}
6082
$this->pollingOptions = $pollingOptions;
61-
$this->rag = (bool) $rag;
83+
$this->rag = $rag;
84+
$this->rawText = $rawText;
85+
$this->polygon = $polygon;
86+
$this->confidence = $confidence;
87+
6288
$this->closeFile = (bool) $closeFile;
6389
if (isset($alias)) {
6490
$this->alias = $alias;

src/Parsing/V2/Field/InferenceFields.php

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Mindee\Parsing\V2\Field;
44

55
use ArrayObject;
6+
use InvalidArgumentException;
67

78
/**
89
* Collection of inference fields.
@@ -36,12 +37,65 @@ public function __construct(array $serverResponse, int $indentLevel = 0)
3637
/**
3738
* Get a field by key.
3839
*
39-
* @param string $key Field key to retrieve.
40-
* @return SimpleField|ObjectField|ListField|null
40+
* @param string $fieldName Field key to retrieve.
41+
* @return SimpleField|ObjectField|ListField
42+
* @throws InvalidArgumentException When the field does not exist.
4143
*/
42-
public function get(string $key)
44+
public function get(string $fieldName)
4345
{
44-
return $this->fields[$key] ?? null;
46+
$field = $this->fields[$fieldName];
47+
if ($field == null) {
48+
throw new InvalidArgumentException("Field $fieldName does not exist.");
49+
}
50+
return $field;
51+
}
52+
53+
/**
54+
* Get a simple field by key.
55+
*
56+
* @param string $fieldName Field key to retrieve.
57+
* @return SimpleField
58+
* @throws InvalidArgumentException When the field does not exist or is not a simple field.
59+
*/
60+
public function getSimpleField(string $fieldName)
61+
{
62+
$field = $this->get($fieldName);
63+
if ($field instanceof SimpleField) {
64+
return $field;
65+
}
66+
throw new InvalidArgumentException("Field $fieldName is not a simple field.");
67+
}
68+
69+
/**
70+
* Get a list field by key.
71+
*
72+
* @param string $fieldName Field key to retrieve.
73+
* @return ListField
74+
* @throws InvalidArgumentException When the field does not exist or is not a list field.
75+
*/
76+
public function getListField(string $fieldName)
77+
{
78+
$field = $this->get($fieldName);
79+
if ($field instanceof ListField) {
80+
return $field;
81+
}
82+
throw new InvalidArgumentException("Field $fieldName is not a list field.");
83+
}
84+
85+
/**
86+
* Get a simple field by key.
87+
*
88+
* @param string $fieldName Field key to retrieve.
89+
* @return ObjectField
90+
* @throws InvalidArgumentException When the field does not exist or is not an object field.
91+
*/
92+
public function getObjectField(string $fieldName)
93+
{
94+
$field = $this->get($fieldName);
95+
if ($field instanceof ObjectField) {
96+
return $field;
97+
}
98+
throw new InvalidArgumentException("Field $fieldName is not an object field.");
4599
}
46100

47101
/**

src/Parsing/V2/InferenceActiveOptions.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@
55
use Mindee\Parsing\Common\SummaryHelper;
66

77
/**
8-
* Inference result options class.
8+
* Options which were activated during the inference.
9+
*
10+
* Options can be activated or deactivated:
11+
* - By setting their default values on the Platform UI
12+
* - By explicitly setting them in the inference request
913
*/
1014
class InferenceActiveOptions
1115
{
1216
/**
13-
* @var boolean Whether the RAG feature was activated.
17+
* @var boolean Whether the Retrieval-Augmented Generation feature was activated.
1418
*/
1519
public bool $rag;
1620

tests/Parsing/V2/InferenceTest.php

Lines changed: 57 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,44 @@ public function testAsyncPredictWhenEmptyMustHaveValidProperties(): void
4040

4141
$this->assertCount(21, $fields, 'Expected 21 fields');
4242

43-
$taxes = $fields->get('taxes');
44-
$this->assertNotNull($taxes, "'taxes' field must exist");
45-
$this->assertInstanceOf(ListField::class, $taxes, "'taxes' must be a ListField");
46-
$this->assertEmpty($taxes->items, "'taxes' list must be empty");
43+
$this->assertInstanceOf(
44+
SimpleField::class,
45+
$fields['total_amount'],
46+
"Field 'total_amount' must be a SimpleField"
47+
);
48+
$totalAmount = $fields->getSimpleField('total_amount');
49+
$this->assertEmpty($totalAmount->value);
4750

48-
$supplierAddress = $fields->get('supplier_address');
49-
$this->assertNotNull($supplierAddress, "'supplier_address' field must exist");
50-
$this->assertInstanceOf(ObjectField::class, $supplierAddress, "'supplier_address' must be an ObjectField");
51+
$this->assertInstanceOf(
52+
ListField::class,
53+
$fields['taxes'],
54+
"Field 'taxes' must be a ListField"
55+
);
56+
$taxes = $fields->getListField('taxes');
57+
$this->assertEmpty($taxes->items);
58+
59+
$this->assertInstanceOf(
60+
ObjectField::class,
61+
$fields['supplier_address'],
62+
"Field 'supplier_address' must be an ObjectField"
63+
);
64+
$supplierAddress = $fields->getObjectField('supplier_address');
65+
$this->assertCount(9, $supplierAddress->fields);
5166

52-
foreach ($fields as $key => $value) {
53-
if ($value === null) {
67+
foreach ($fields as $fieldName => $field) {
68+
if ($field === null) {
5469
continue;
5570
}
56-
57-
if ($value instanceof ListField) {
58-
$this->assertInstanceOf(ListField::class, $value, "$key – ListField expected");
59-
} elseif ($value instanceof ObjectField) {
60-
$this->assertInstanceOf(ObjectField::class, $value, "$key – ObjectField expected");
71+
if ($field instanceof ListField) {
72+
$this->assertEmpty($field->items, "Field $fieldName.items must be empty");
73+
} elseif ($field instanceof ObjectField) {
74+
foreach ($field->fields as $subFieldName => $subField) {
75+
$this->assertEmpty($subField->value, "Field $fieldName.$subFieldName must be empty");
76+
}
77+
} elseif ($field instanceof SimpleField) {
78+
$this->assertIsNotObject($field->value, "Field $fieldName must be a scalar value");
6179
} else {
62-
$this->assertInstanceOf(SimpleField::class, $value, "$key – SimpleField expected");
80+
$this->fail("Unknown field type: $fieldName");
6381
}
6482
}
6583
}
@@ -93,7 +111,7 @@ public function testAsyncPredictWhenCompleteMustExposeAllProperties(): void
93111
$this->assertInstanceOf(SimpleField::class, $date);
94112
$this->assertEquals('2019-11-02', $date->value, "'date' value mismatch");
95113

96-
$taxes = $fields->get('taxes');
114+
$taxes = $fields->getListField('taxes');
97115
$this->assertNotNull($taxes, "'taxes' field must exist");
98116
$this->assertInstanceOf(ListField::class, $taxes, "'taxes' must be a ListField");
99117
$this->assertCount(1, $taxes->items, "'taxes' list must contain exactly one item");
@@ -107,7 +125,7 @@ public function testAsyncPredictWhenCompleteMustExposeAllProperties(): void
107125
$this->assertEquals(31.5, $baseTax->value, "'taxes.base' value mismatch");
108126
$this->assertNotNull(strval($taxes), "'taxes'.__toString() must not be null");
109127

110-
$supplierAddress = $fields->get('supplier_address');
128+
$supplierAddress = $fields->getObjectField('supplier_address');
111129
$this->assertNotNull($supplierAddress, "'supplier_address' field must exist");
112130
$this->assertInstanceOf(ObjectField::class, $supplierAddress, "'supplier_address' must be an ObjectField");
113131

@@ -270,45 +288,46 @@ public function testRawTextsMustBeAccessible(): void
270288
*/
271289
public function testRstDisplayMustBeAccessible(): void
272290
{
273-
$resp = $this->loadFromResource('v2/inference/standard_field_types.json');
274-
$rstRef = $this->readFileAsString('v2/inference/standard_field_types.rst');
275-
$inf = $resp->inference;
276-
$this->assertNotNull($inf);
277-
$this->assertEquals($rstRef, strval($resp->inference));
291+
$response = $this->loadFromResource('v2/inference/standard_field_types.json');
292+
$expectedRst = $this->readFileAsString('v2/inference/standard_field_types.rst');
293+
$inference = $response->inference;
294+
$this->assertNotNull($inference);
295+
$this->assertEquals($expectedRst, strval($response->inference));
278296
}
279297

280298
/**
281299
* Coordinates & location data must be parsed and exposed.
282300
*/
283301
public function testCoordinatesAndLocationDataMustBeAccessible(): void
284302
{
285-
$resp = $this->loadFromResource('v2/products/financial_document/complete_with_coordinates.json');
286-
$inf = $resp->inference;
287-
$this->assertNotNull($inf);
288-
$this->assertNotNull($inf->result->fields->get('date')->locations);
289-
$this->assertNotNull($inf->result->fields->get('date')->locations[0]);
290-
$this->assertEquals(0, $inf->result->fields->get('date')->locations[0]->page);
303+
$response = $this->loadFromResource('v2/products/financial_document/complete_with_coordinates.json');
304+
$inference = $response->inference;
305+
$this->assertNotNull($inference);
306+
$dateField = $inference->result->fields->getSimpleField('date');
307+
$this->assertNotNull($dateField->locations);
308+
$this->assertNotNull($dateField->locations[0]);
309+
$this->assertEquals(0, $dateField->locations[0]->page);
291310
$this->assertEquals(
292311
0.948979073166918,
293-
$inf->result->fields->get('date')->locations[0]->polygon->coordinates[0][0]
312+
$dateField->locations[0]->polygon->coordinates[0]->x
294313
);
295314
$this->assertEquals(
296315
0.23097924535067715,
297-
$inf->result->fields->get('date')->locations[0]->polygon->coordinates[0][1]
316+
$dateField->locations[0]->polygon->coordinates[0][1]
298317
);
299-
$this->assertEquals(0.85422, $inf->result->fields->get('date')->locations[0]->polygon->coordinates[1][0]);
300-
$this->assertEquals(0.230072, $inf->result->fields->get('date')->locations[0]->polygon->coordinates[1][1]);
318+
$this->assertEquals(0.85422, $dateField->locations[0]->polygon->coordinates[1][0]);
319+
$this->assertEquals(0.230072, $dateField->locations[0]->polygon->coordinates[1][1]);
301320
$this->assertEquals(
302321
0.8540899268330819,
303-
$inf->result->fields->get('date')->locations[0]->polygon->coordinates[2][0]
322+
$dateField->locations[0]->polygon->coordinates[2][0]
304323
);
305324
$this->assertEquals(
306325
0.24365775464932288,
307-
$inf->result->fields->get('date')->locations[0]->polygon->coordinates[2][1]
326+
$dateField->locations[0]->polygon->coordinates[2][1]
308327
);
309-
$this->assertEquals(0.948849, $inf->result->fields->get('date')->locations[0]->polygon->coordinates[3][0]);
310-
$this->assertEquals(0.244565, $inf->result->fields->get('date')->locations[0]->polygon->coordinates[3][1]);
311-
$this->assertEquals(FieldConfidence::MEDIUM, $inf->result->fields->get('date')->confidence);
312-
$this->assertEquals("Medium", $inf->result->fields->get('date')->confidence->getValue());
328+
$this->assertEquals(0.948849, $dateField->locations[0]->polygon->coordinates[3][0]);
329+
$this->assertEquals(0.244565, $dateField->locations[0]->polygon->coordinates[3][1]);
330+
$this->assertEquals(FieldConfidence::MEDIUM, $dateField->confidence);
331+
$this->assertEquals('Medium', $dateField->confidence->getValue());
313332
}
314333
}

0 commit comments

Comments
 (0)