Skip to content

Commit

Permalink
Support one-to-one on input
Browse files Browse the repository at this point in the history
Signed-off-by: Tyler Ohlsen <ohltyler@amazon.com>
  • Loading branch information
ohltyler committed Sep 16, 2024
1 parent 370683b commit fd2ddb3
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
2 changes: 1 addition & 1 deletion common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function getCharacterLimitedString(
: '';
}

export function customStringify(jsonObj: {}): string {
export function customStringify(jsonObj: {} | []): string {
return JSON.stringify(jsonObj, undefined, 2);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,12 @@ export function InputTransformModal(props: InputTransformModalProps) {
const [sourceInput, setSourceInput] = useState<string>('[]');
const [transformedInput, setTransformedInput] = useState<string>('{}');

// get the current input map
// get some current form values
const map = getIn(values, props.inputMapFieldPath) as MapArrayFormValue;
const oneToOne = getIn(
values,
`${props.baseConfigPath}.${props.config.id}.one_to_one`
);

// selected transform state
const transformOptions = map.map((_, idx) => ({
Expand Down Expand Up @@ -310,7 +314,11 @@ export function InputTransformModal(props: InputTransformModalProps) {
(hit: SearchHit) => hit._source
);
if (hits.length > 0) {
setSourceInput(customStringify(hits[0]));
setSourceInput(
// if one-to-one, treat the source input as a single retrieved document
// else, treat it as all of the returned documents
customStringify(oneToOne ? hits[0] : hits)
);
}
})
.catch((error: any) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export function OutputTransformModal(props: OutputTransformModalProps) {
const [sourceOutput, setSourceOutput] = useState<string>('[]');
const [transformedOutput, setTransformedOutput] = useState<string>('{}');

// get some current values
// get some current form values
const map = getIn(values, props.outputMapFieldPath) as MapArrayFormValue;
const fullResponsePath = getIn(
values,
Expand Down
29 changes: 20 additions & 9 deletions public/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
import { getCore, getDataSourceEnabled } from '../services';
import {
MDSQueryParams,
MapEntry,
ModelInputMap,
ModelOutputMap,
} from '../../common/interfaces';
Expand Down Expand Up @@ -177,19 +178,17 @@ export function unwrapTransformedDocs(

// ML inference processors will use standard dot notation or JSONPath depending on the input.
// We follow the same logic here to generate consistent results.
export function generateTransform(input: {}, map: MapFormValue): {} {
// Collapse the values depending on if the input is an array or not.
export function generateTransform(input: {} | [], map: MapFormValue): {} | [] {
let output = {};
map.forEach((mapEntry) => {
const path = mapEntry.value;
try {
let transformedResult = undefined;
if (mapEntry.value.startsWith(JSONPATH_ROOT_SELECTOR)) {
// JSONPath transform
transformedResult = jsonpath.query(input, path);
// Standard dot notation
} else {
transformedResult = get(input, path);
}
const transformedResult = Array.isArray(input)
? input.map((inputEntry) =>
getTransformedResult(mapEntry, inputEntry, path)
)
: getTransformedResult(mapEntry, input, path);
output = {
...output,
[mapEntry.key]: transformedResult || '',
Expand All @@ -199,6 +198,18 @@ export function generateTransform(input: {}, map: MapFormValue): {} {
return output;
}

function getTransformedResult(
mapEntry: MapEntry,
input: {},
path: string
): any {
return mapEntry.value.startsWith(JSONPATH_ROOT_SELECTOR)
? // JSONPath transform
jsonpath.query(input, path)
: // Standard dot notation
get(input, path);
}

// Derive the collection of model inputs from the model interface JSONSchema into a form-ready list
export function parseModelInputs(
modelInterface: ModelInterface | undefined
Expand Down

0 comments on commit fd2ddb3

Please sign in to comment.