export async function createIndex()

in storage-reverse-image-search/functions/src/common/vertex.ts [49:124]


export async function createIndex(
  dimensions: number
): Promise<string | undefined> {
  const algorithmConfig =
    config.algorithmConfig === AlgorithmConfig.BruteForceConfig
      ? {
          structValue: {
            fields: {
              bruteForceConfig: {
                structValue: {
                  fields: {},
                },
              },
            },
          },
        }
      : {
          structValue: {
            fields: {
              treeAhConfig: {
                structValue: {
                  fields: {
                    // TODO mais - add treeAHConfig https://cloud.google.com/vertex-ai/docs/matching-engine/configuring-indexes#tree-ah-config
                  },
                },
              },
            },
          },
        };
  const metadata = {
    structValue: {
      fields: {
        contentsDeltaUri: {
          stringValue: `gs://${config.bucketName}/datapoints`,
        },
        isCompleteOverwrite: {boolValue: false},
        config: {
          structValue: {
            fields: {
              dimensions: {
                numberValue: dimensions,
              },
              approximateNeighborsCount: {numberValue: config.neighbors},
              distanceMeasureType: {stringValue: config.distanceMeasureType},
              shardSize: {stringValue: config.shardSize},
              featureNormType: {stringValue: config.featureNormType},
              algorithmConfig: {},
            },
          },
        },
      },
    },
  };

  metadata.structValue.fields.config.structValue.fields.algorithmConfig =
    algorithmConfig;

  const [operation] = await indexClient.createIndex({
    parent: `projects/${config.projectId}/locations/${config.location}`,
    index: {
      name: 'ext-' + config.instanceId,
      displayName: 'Storage Image Similarity Extension',
      indexUpdateMethod: 'STREAM_UPDATE',
      metadataSchemaUri:
        'gs://google-cloud-aiplatform/schema/matchingengine/metadata/nearest_neighbor_search_1.0.0.yaml',
      metadata: metadata,
    },
  });

  // We cannot await the operation here because it will take longer than the Function timeout.
  if (operation.error) {
    throw new Error(operation.error.message);
  }

  return operation.name;
}