Skip to content

Commit

Permalink
remove ts-ignore and add temporary semantic_text field mapping type
Browse files Browse the repository at this point in the history
  • Loading branch information
joemcelroy committed Jun 18, 2024
1 parent 20d7be5 commit a1a4f50
Showing 1 changed file with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
FieldCapsResponse,
IndicesGetMappingResponse,
FieldCapsFieldCapability,
MappingPropertyBase,
} from '@elastic/elasticsearch/lib/api/types';

import { IScopedClusterClient } from '@kbn/core-elasticsearch-server';
Expand All @@ -20,10 +21,12 @@ interface FieldModelId {
modelId: string | undefined;
}

type SemanticEmbeddingType = 'sparse_vector' | 'dense_vector';

interface SemanticField {
field: string;
inferenceId: string;
embeddingType: 'sparse_vector' | 'dense_vector';
embeddingType?: SemanticEmbeddingType;
}

interface IndexFieldModel {
Expand All @@ -32,7 +35,20 @@ interface IndexFieldModel {
semanticTextFields: SemanticField[];
}

const EMBEDDING_TYPE = { sparse_embedding: 'sparse_vector', text_embedding: 'dense_vector' };
type TaskType = 'sparse_embedding' | 'text_embedding';

interface MappingSemanticTextProperty extends MappingPropertyBase {
type: 'semantic_text';
inference_id: string;
model_settings?: {
task_type: TaskType;
};
}

const EMBEDDING_TYPE: Record<TaskType, SemanticEmbeddingType> = {
sparse_embedding: 'sparse_vector',
text_embedding: 'dense_vector',
};

export const getModelIdFields = (fieldCapsResponse: FieldCapsResponse) => {
const { fields } = fieldCapsResponse;
Expand Down Expand Up @@ -181,13 +197,13 @@ export const parseFieldsCapabilities = (
(field) => mappingProperties[field].type === 'semantic_text'
)
.map((field) => {
const mapping = mappingProperties[field] as unknown as MappingSemanticTextProperty;
return {
field,
// @ts-ignore
inferenceId: mappingProperties[field]?.inference_id,
embeddingType:
// @ts-ignore
EMBEDDING_TYPE[mappingProperties[field]?.model_settings?.task_type],
inferenceId: mapping?.inference_id,
embeddingType: mapping?.model_settings?.task_type
? EMBEDDING_TYPE[mapping.model_settings.task_type]
: undefined,
};
});

Expand Down

0 comments on commit a1a4f50

Please sign in to comment.