Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Onboard rerank by field processor #476

Merged
merged 1 commit into from
Nov 13, 2024
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
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
Loading