-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathutils.ts
243 lines (233 loc) · 6.95 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
import { snakeCase } from 'lodash';
import {
MLIngestProcessor,
MLSearchRequestProcessor,
NormalizationProcessor,
} from '../../../configs';
import {
WorkflowTemplate,
START_FROM_SCRATCH_WORKFLOW_NAME,
DEFAULT_NEW_WORKFLOW_NAME,
UIState,
WORKFLOW_TYPE,
FETCH_ALL_QUERY,
customStringify,
TERM_QUERY,
MULTIMODAL_SEARCH_QUERY_BOOL,
IProcessorConfig,
VECTOR_TEMPLATE_PLACEHOLDER,
VECTOR_PATTERN,
KNN_QUERY,
HYBRID_SEARCH_QUERY_MATCH_KNN,
} from '../../../../common';
import { generateId } from '../../../utils';
// Fn to produce the complete preset template with all necessary UI metadata.
export function enrichPresetWorkflowWithUiMetadata(
presetWorkflow: Partial<WorkflowTemplate>
): WorkflowTemplate {
let uiMetadata = {} as UIState;
switch (presetWorkflow.ui_metadata?.type || WORKFLOW_TYPE.CUSTOM) {
case WORKFLOW_TYPE.SEMANTIC_SEARCH: {
uiMetadata = fetchSemanticSearchMetadata();
break;
}
case WORKFLOW_TYPE.MULTIMODAL_SEARCH: {
uiMetadata = fetchMultimodalSearchMetadata();
break;
}
case WORKFLOW_TYPE.HYBRID_SEARCH: {
uiMetadata = fetchHybridSearchMetadata();
break;
}
default: {
uiMetadata = fetchEmptyMetadata();
break;
}
}
return {
...presetWorkflow,
ui_metadata: {
...presetWorkflow.ui_metadata,
...uiMetadata,
},
} as WorkflowTemplate;
}
export function fetchEmptyMetadata(): UIState {
return {
type: WORKFLOW_TYPE.CUSTOM,
config: {
ingest: {
enabled: {
id: 'enabled',
type: 'boolean',
value: true,
},
pipelineName: {
id: 'pipelineName',
type: 'string',
value: generateId('ingest_pipeline'),
},
enrich: {
processors: [],
},
index: {
name: {
id: 'indexName',
type: 'string',
value: generateId('my_index', 6),
},
mappings: {
id: 'indexMappings',
type: 'json',
value: customStringify({
properties: {},
}),
},
settings: {
id: 'indexSettings',
type: 'json',
},
},
},
search: {
request: {
id: 'request',
type: 'json',
value: customStringify(FETCH_ALL_QUERY),
},
pipelineName: {
id: 'pipelineName',
type: 'string',
value: generateId('search_pipeline'),
},
index: {
name: {
id: 'indexName',
type: 'string',
},
},
enrichRequest: {
processors: [],
},
enrichResponse: {
processors: [],
},
},
},
};
}
export function fetchSemanticSearchMetadata(): UIState {
let baseState = fetchEmptyMetadata();
baseState.type = WORKFLOW_TYPE.SEMANTIC_SEARCH;
baseState.config.ingest.enrich.processors = [new MLIngestProcessor().toObj()];
baseState.config.ingest.index.name.value = generateId('knn_index', 6);
baseState.config.ingest.index.settings.value = customStringify({
[`index.knn`]: true,
});
baseState.config.search.request.value = customStringify(TERM_QUERY);
baseState.config.search.enrichRequest.processors = [
injectQueryTemplateInProcessor(
new MLSearchRequestProcessor().toObj(),
KNN_QUERY
),
];
return baseState;
}
export function fetchMultimodalSearchMetadata(): UIState {
let baseState = fetchEmptyMetadata();
baseState.type = WORKFLOW_TYPE.MULTIMODAL_SEARCH;
baseState.config.ingest.enrich.processors = [new MLIngestProcessor().toObj()];
baseState.config.ingest.index.name.value = generateId('knn_index', 6);
baseState.config.ingest.index.settings.value = customStringify({
[`index.knn`]: true,
});
baseState.config.search.request.value = customStringify(
MULTIMODAL_SEARCH_QUERY_BOOL
);
baseState.config.search.enrichRequest.processors = [
injectQueryTemplateInProcessor(
new MLSearchRequestProcessor().toObj(),
MULTIMODAL_SEARCH_QUERY_BOOL
),
];
return baseState;
}
export function fetchHybridSearchMetadata(): UIState {
let baseState = fetchEmptyMetadata();
baseState.type = WORKFLOW_TYPE.HYBRID_SEARCH;
baseState.config.ingest.enrich.processors = [new MLIngestProcessor().toObj()];
baseState.config.ingest.index.name.value = generateId('knn_index', 6);
baseState.config.ingest.index.settings.value = customStringify({
[`index.knn`]: true,
});
baseState.config.search.request.value = customStringify(TERM_QUERY);
baseState.config.search.enrichResponse.processors = [
injectDefaultWeightsInNormalizationProcessor(
new NormalizationProcessor().toObj()
),
];
baseState.config.search.enrichRequest.processors = [
injectQueryTemplateInProcessor(
new MLSearchRequestProcessor().toObj(),
HYBRID_SEARCH_QUERY_MATCH_KNN
),
];
return baseState;
}
// Utility fn to process workflow names from their presentable/readable titles
// on the UI, to a valid name format.
// This leads to less friction if users decide to save the name later on.
export function processWorkflowName(workflowName: string): string {
return workflowName === START_FROM_SCRATCH_WORKFLOW_NAME
? DEFAULT_NEW_WORKFLOW_NAME
: snakeCase(workflowName);
}
// populate the `query_template` config value with a given query template
// by default, we replace any vector pattern ("{{vector}}") with the unquoted
// vector template placeholder (${vector}) so it becomes a proper template
function injectQueryTemplateInProcessor(
processorConfig: IProcessorConfig,
queryObj: {}
): IProcessorConfig {
processorConfig.optionalFields = processorConfig.optionalFields?.map(
(optionalField) => {
let updatedField = optionalField;
if (optionalField.id === 'query_template') {
updatedField = {
...updatedField,
value: customStringify(queryObj).replace(
new RegExp(`"${VECTOR_PATTERN}"`, 'g'),
VECTOR_TEMPLATE_PLACEHOLDER
),
};
}
return updatedField;
}
);
return processorConfig;
}
// set default weights for a normalization processor. assumes there is 2 queries, and equally
// balances the weight. We don't hardcode in the configuration, since we don't want to set
// invalid defaults for arbitrary use cases (e.g., more than 2 queries). In this case, we
// are already setting 2 queries by default, so we can make this assumption.
function injectDefaultWeightsInNormalizationProcessor(
processorConfig: IProcessorConfig
): IProcessorConfig {
processorConfig.optionalFields = processorConfig.optionalFields?.map(
(optionalField) => {
let updatedField = optionalField;
if (optionalField.id === 'weights') {
updatedField = {
...updatedField,
value: '0.5, 0.5',
};
}
return updatedField;
}
);
return processorConfig;
}