Skip to content

Commit fc3fd57

Browse files
committed
Refactoring and cleanup in workflow_to_template_utils
Signed-off-by: Tyler Ohlsen <ohltyler@amazon.com>
1 parent ab01756 commit fc3fd57

File tree

1 file changed

+45
-74
lines changed

1 file changed

+45
-74
lines changed

public/pages/workflow_detail/utils/workflow_to_template_utils.ts

+45-74
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ function toTemplateNodes(
104104
edges: ReactFlowEdge[]
105105
): TemplateNode[] | undefined {
106106
if (flowNode.data.baseClasses?.includes(COMPONENT_CLASS.ML_TRANSFORMER)) {
107-
return toIngestPipelineNodes(flowNode);
107+
return transformerToTemplateNodes(flowNode);
108108
} else if (flowNode.data.baseClasses?.includes(COMPONENT_CLASS.INDEXER)) {
109-
return [toIndexerNode(flowNode, prevNodes, edges)];
109+
return [indexerToTemplateNode(flowNode, prevNodes, edges)];
110110
}
111111
}
112112

@@ -121,7 +121,9 @@ function toTemplateEdge(flowEdge: ReactFlowEdge): TemplateEdge {
121121
// ingest pipeline with a processor specific to the final class of the node.
122122
// Optionally prepend a register pretrained model step if the selected model
123123
// is a pretrained and undeployed one.
124-
function toIngestPipelineNodes(flowNode: ReactFlowComponent): TemplateNode[] {
124+
function transformerToTemplateNodes(
125+
flowNode: ReactFlowComponent
126+
): TemplateNode[] {
125127
// TODO a few improvements to make here:
126128
// 1. Consideration of multiple ingest processors and how to collect them all, and finally create
127129
// a single ingest pipeline with all of them, in the same order as done on the UI
@@ -143,18 +145,14 @@ function toIngestPipelineNodes(flowNode: ReactFlowComponent): TemplateNode[] {
143145
| RegisterPretrainedModelNode
144146
| undefined;
145147
if (model.category === MODEL_CATEGORY.PRETRAINED) {
146-
const pretrainedModelMap = {} as {
147-
[modelName: string]: PretrainedSentenceTransformer;
148-
};
149-
[
148+
const pretrainedModel = [
150149
ROBERTA_SENTENCE_TRANSFORMER,
151150
MPNET_SENTENCE_TRANSFORMER,
152151
BERT_SENTENCE_TRANSFORMER,
153-
].map((transformer) => {
154-
pretrainedModelMap[transformer.name] = transformer;
155-
});
156-
// the model ID in the form will be the unique name of the pretrained model
157-
const pretrainedModel = pretrainedModelMap[modelId];
152+
].find(
153+
// the model ID in the form will be the unique name of the pretrained model
154+
(model) => model.name === modelId
155+
) as PretrainedSentenceTransformer;
158156
registerModelStep = {
159157
id: REGISTER_LOCAL_PRETRAINED_MODEL_STEP_TYPE,
160158
type: REGISTER_LOCAL_PRETRAINED_MODEL_STEP_TYPE,
@@ -168,73 +166,46 @@ function toIngestPipelineNodes(flowNode: ReactFlowComponent): TemplateNode[] {
168166
} as RegisterPretrainedModelNode;
169167
}
170168

171-
// If we have a register model step, add it first, and use
172-
// the produced model ID in the ingest pipeline step
173-
if (registerModelStep !== undefined) {
174-
return [
175-
registerModelStep,
176-
{
177-
id: flowNode.data.id,
178-
type: CREATE_INGEST_PIPELINE_STEP_TYPE,
179-
previous_node_inputs: {
180-
[REGISTER_LOCAL_PRETRAINED_MODEL_STEP_TYPE]: 'model_id',
181-
},
182-
user_inputs: {
183-
pipeline_id: ingestPipelineName,
184-
model_id: `\${{${REGISTER_LOCAL_PRETRAINED_MODEL_STEP_TYPE}.model_id}}`,
185-
input_field: inputField,
186-
output_field: vectorField,
187-
configurations: {
188-
description:
189-
'An ingest pipeline with a text embedding processor.',
190-
processors: [
191-
{
192-
text_embedding: {
193-
model_id: `\${{${REGISTER_LOCAL_PRETRAINED_MODEL_STEP_TYPE}.model_id}}`,
194-
field_map: {
195-
[inputField]: vectorField,
196-
},
197-
},
198-
} as TextEmbeddingProcessor,
199-
],
200-
},
201-
},
202-
} as CreateIngestPipelineNode,
203-
];
204-
} else {
205-
return [
206-
{
207-
id: flowNode.data.id,
208-
type: CREATE_INGEST_PIPELINE_STEP_TYPE,
209-
user_inputs: {
210-
pipeline_id: ingestPipelineName,
211-
model_id: modelId,
212-
input_field: inputField,
213-
output_field: vectorField,
214-
configurations: {
215-
description:
216-
'An ingest pipeline with a text embedding processor.',
217-
processors: [
218-
{
219-
text_embedding: {
220-
model_id: modelId,
221-
field_map: {
222-
[inputField]: vectorField,
223-
},
224-
},
225-
} as TextEmbeddingProcessor,
226-
],
227-
},
228-
},
229-
} as CreateIngestPipelineNode,
230-
];
231-
}
169+
// The model ID depends on if we are consuming it from a previous pretrained model step,
170+
// or directly from the user
171+
const finalModelId =
172+
registerModelStep !== undefined
173+
? `\${{${REGISTER_LOCAL_PRETRAINED_MODEL_STEP_TYPE}.model_id}}`
174+
: modelId;
175+
176+
const createIngestPipelineStep = {
177+
id: flowNode.data.id,
178+
type: CREATE_INGEST_PIPELINE_STEP_TYPE,
179+
user_inputs: {
180+
pipeline_id: ingestPipelineName,
181+
model_id: finalModelId,
182+
input_field: inputField,
183+
output_field: vectorField,
184+
configurations: {
185+
description: 'An ingest pipeline with a text embedding processor.',
186+
processors: [
187+
{
188+
text_embedding: {
189+
model_id: finalModelId,
190+
field_map: {
191+
[inputField]: vectorField,
192+
},
193+
},
194+
} as TextEmbeddingProcessor,
195+
],
196+
},
197+
},
198+
} as CreateIngestPipelineNode;
199+
200+
return registerModelStep !== undefined
201+
? [registerModelStep, createIngestPipelineStep]
202+
: [createIngestPipelineStep];
232203
}
233204
}
234205
}
235206

236207
// General fn to convert an indexer node to a final CreateIndexNode template node.
237-
function toIndexerNode(
208+
function indexerToTemplateNode(
238209
flowNode: ReactFlowComponent,
239210
prevNodes: ReactFlowComponent[],
240211
edges: ReactFlowEdge[]

0 commit comments

Comments
 (0)