Skip to content

Commit

Permalink
Onboard rerank by field processor (#476)
Browse files Browse the repository at this point in the history
Signed-off-by: Tyler Ohlsen <[email protected]>
(cherry picked from commit 4025002)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
github-actions[bot] committed Nov 13, 2024
1 parent 55ef260 commit e801d4c
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 0 deletions.
1 change: 1 addition & 0 deletions common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ export enum PROCESSOR_TYPE {
TEXT_CHUNKING = 'text_chunking',
NORMALIZATION = 'normalization-processor',
COLLAPSE = 'collapse',
RERANK = 'rerank',
}

export enum MODEL_TYPE {
Expand Down
1 change: 1 addition & 0 deletions public/configs/search_response_processors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export * from './split_search_response_processor';
export * from './sort_search_response_processor';
export * from './normalization_processor';
export * from './collapse_processor';
export * from './rerank_processor';
53 changes: 53 additions & 0 deletions public/configs/search_response_processors/rerank_processor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { PROCESSOR_TYPE } from '../../../common';
import { Processor } from '../processor';
import { generateId } from '../../utils';

/**
* The rerank processor config. Used in search flows.
* For now, only supports the by_field type. For details, see
* https://opensearch.org/docs/latest/search-plugins/search-pipelines/rerank-processor/#the-by_field-rerank-type
*/
export class RerankProcessor extends Processor {
constructor() {
super();
this.id = generateId('rerank_processor');
this.type = PROCESSOR_TYPE.RERANK;
this.name = 'Rerank Processor';
this.fields = [
{
id: 'target_field',
type: 'string',
},
];
this.optionalFields = [
{
id: 'remove_target_field',
type: 'boolean',
value: false,
},
{
id: 'keep_previous_score',
type: 'boolean',
value: false,
},
{
id: 'tag',
type: 'string',
},
{
id: 'description',
type: 'string',
},
{
id: 'ignore_failure',
type: 'boolean',
value: false,
},
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
MLSearchRequestProcessor,
MLSearchResponseProcessor,
NormalizationProcessor,
RerankProcessor,
SortIngestProcessor,
SortSearchResponseProcessor,
SplitIngestProcessor,
Expand Down Expand Up @@ -276,6 +277,13 @@ export function ProcessorsList(props: ProcessorsListProps) {
);
},
},
{
name: 'Rerank Processor',
onClick: () => {
closePopover();
addProcessor(new RerankProcessor().toObj());
},
},
{
name: 'Split Processor',
onClick: () => {
Expand Down
21 changes: 21 additions & 0 deletions public/utils/config_to_template_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,27 @@ export function processorConfigsToTemplateProcessors(
});
break;
}
// Since we only support the by_field type of the rerank processor,
// we need to nest the form values within the parent "by_field" field.
case PROCESSOR_TYPE.RERANK: {
const formValues = processorConfigToFormik(processorConfig);
let finalFormValues = {} as FormikValues;
Object.keys(formValues).forEach((formKey: string) => {
const formValue = formValues[formKey];
finalFormValues = optionallyAddToFinalForm(
finalFormValues,
formKey,
formValue
);
});
finalFormValues = {
by_field: finalFormValues,
};
processorsList.push({
[processorConfig.type]: finalFormValues,
});
break;
}
case PROCESSOR_TYPE.SPLIT:
case PROCESSOR_TYPE.SORT:
case PROCESSOR_TYPE.COLLAPSE:
Expand Down
8 changes: 8 additions & 0 deletions public/utils/config_to_workspace_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,14 @@ function processorsConfigToWorkspaceFlow(
);
break;
}
case PROCESSOR_TYPE.RERANK: {
transformer = new BaseTransformer(
processorConfig.name,
'Rerank results by a document field',
context
);
break;
}
default: {
transformer = new BaseTransformer(processorConfig.name, '', context);
break;
Expand Down

0 comments on commit e801d4c

Please sign in to comment.