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
102 changes: 102 additions & 0 deletions src/main/java/com/mindee/v2/product/extraction/params/DataSchema.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.mindee.v2.product.extraction.params;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mindee.exceptions.MindeeInputException;
import java.io.IOException;
import java.util.Map;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
* Modify the Data Schema.
*/
@Getter
@Setter
@NoArgsConstructor
@EqualsAndHashCode
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class DataSchema {

/**
* If set, completely replaces the data schema of the model.
*/
@JsonProperty("replace")
private DataSchemaReplace replace;

/**
* Construct a new instance from a DataSchemaReplace object.
*
* @param replace The replacement schema structure.
*/
public DataSchema(DataSchemaReplace replace) {
this.replace = replace;
}

/**
* Construct a new instance from a Map/Dictionary.
*
* @param dataSchema Dictionary containing the data schema configuration.
*/
public DataSchema(Map<String, Object> dataSchema) {
setup(dataSchema);
}

/**
* Construct a new instance from a JSON string.
*
* @param jsonString JSON string containing the data schema configuration.
*/
public DataSchema(String jsonString) {
if (jsonString == null || jsonString.trim().isEmpty()) {
return;
}
try {
var objectMapper = new ObjectMapper();
Map<String, Object> dataSchema = objectMapper
.readValue(jsonString, new TypeReference<Map<String, Object>>() {
});
setup(dataSchema);
} catch (IOException e) {
throw new MindeeInputException("Invalid Data Schema format.", e);
}
}

/**
* Initializes the 'replace' property.
*
* @param dataSchema The map containing data schema configuration.
*/
private void setup(Map<String, Object> dataSchema) {
if (dataSchema == null) {
return;
}

if (!dataSchema.containsKey("replace")) {
throw new MindeeInputException("Invalid Data Schema format.");
}

try {
var objectMapper = new ObjectMapper();
this.replace = objectMapper.convertValue(dataSchema.get("replace"), DataSchemaReplace.class);
} catch (IllegalArgumentException e) {
throw new MindeeInputException("Invalid Data Schema format.", e);
}
}

@Override
public String toString() {
try {
return new ObjectMapper().writeValueAsString(this);
} catch (JsonProcessingException e) {
return super.toString();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package com.mindee.v2.product.extraction.params;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.List;
import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
* Data Schema Field.
*/
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonPropertyOrder(
{ "name", "title", "is_array", "type", "description", "guidelines", "unique_values",
"nested_fields", "classification_values" }
)
public class DataSchemaField {

/**
* Name of the field in the data schema.
*/
@JsonProperty("name")
private String name;

/**
* Display name for the field. Also impacts inference results.
*/
@JsonProperty("title")
private String title;

/**
* Whether this field can contain multiple values.
*/
@JsonProperty("is_array")
private boolean isArray;

/**
* Data type of the field.
*/
@JsonProperty("type")
private String type;

/**
* Detailed description of what this field represents.
*/
@JsonProperty("description")
private String description;

/**
* Optional extraction guidelines.
*/
@JsonProperty("guidelines")
private String guidelines;

/**
* Whether to remove duplicate values in the array.
* Only applicable if `is_array` is True.
*/
@JsonProperty("unique_values")
private Boolean uniqueValues;

/**
* Subfields when type is `nested_object`. Leave empty for other types.
*/
@JsonProperty("nested_fields")
private Map<String, Object> nestedFields;

/**
* Allowed values when type is `classification`. Leave empty for other types.
*/
@JsonProperty("classification_values")
private List<String> classificationValues;

/**
* Getter for isArray.
*/
public boolean getIsArray() {
return isArray;
}

@Override
public String toString() {
try {
return new ObjectMapper().writeValueAsString(this);
} catch (JsonProcessingException e) {
return super.toString();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.mindee.v2.product.extraction.params;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
* The structure to completely replace the data schema of the model.
*/
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class DataSchemaReplace {

/**
* List of fields in the Data Schema.
*/
@JsonProperty("fields")
private List<DataSchemaField> fields;

@Override
public String toString() {
try {
return new ObjectMapper().writeValueAsString(this);
} catch (JsonProcessingException e) {
return super.toString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public final class ExtractionParameters extends BaseProductParameters {
/**
* Dynamic changes to the data schema of the model for this inference.
*/
private final String dataSchema;
private final DataSchema dataSchema;
Comment thread
ianardee marked this conversation as resolved.
Resolved

private ExtractionParameters(
String modelId,
Expand All @@ -50,7 +50,7 @@ private ExtractionParameters(
Boolean polygon,
Boolean confidence,
String textContext,
String dataSchema
DataSchema dataSchema
) {
super(modelId, alias, webhookIds);
this.rag = rag;
Expand Down Expand Up @@ -81,7 +81,7 @@ public Map<String, String> getRequestParameters() {
parameters.put("text_context", this.getTextContext());
}
if (this.getDataSchema() != null) {
parameters.put("data_schema", this.getDataSchema());
parameters.put("data_schema", this.getDataSchema().toString());
}

return parameters;
Expand All @@ -106,7 +106,7 @@ public static final class Builder extends BaseProductParameters.BaseBuilder<Buil
private Boolean polygon = null;
private Boolean confidence = null;
private String textContext;
private String dataSchema;
private DataSchema dataSchema;

Builder(String modelId) {
super(modelId);
Expand Down Expand Up @@ -144,6 +144,18 @@ public Builder textContext(String textContext) {

/** @param dataSchema {@link #dataSchema} */
public Builder dataSchema(String dataSchema) {
this.dataSchema = dataSchema != null ? new DataSchema(dataSchema) : null;
return this;
}

/** @param dataSchema {@link #dataSchema} */
public Builder dataSchema(Map<String, Object> dataSchema) {
Comment thread
ianardee marked this conversation as resolved.
Resolved
this.dataSchema = dataSchema != null ? new DataSchema(dataSchema) : null;
return this;
}

/** @param dataSchema {@link #dataSchema} */
public Builder dataSchema(DataSchema dataSchema) {
this.dataSchema = dataSchema;
return this;
}
Expand Down
Loading
Loading