code: def query_index(
      query: str,
      cfg: DictConfig,
      model: object,
      index: object,
      pca: object,
      idx2id: dict,
      id2string: dict,
  ) -> Dict:
      """
      Query the nearest neighbor index of entities to find the
      concepts in the index dataset that are most similar to the
      query.
  
      Args:
          query (str): entity to look up in the index
          cfg (DictConfig): config object to specifiy query parameters
          model (EntityLinkingModel): entity linking encoder model
          index (object): faiss index
          pca (object): sklearn pca transformation to be applied to queries
          idx2id (dict): dictionary mapping unique concept dataset index to
                         its CUI
          id2string (dict): dictionary mapping each unqiue CUI to a
                            representative english description of
                            the concept
      Returns:
          A dictionary with the concept ids of the index's most similar
          entities as the keys and a tuple containing the string
          representation of that concept and its cosine similarity to
          the query as the values.
      """
      query_emb = (
          get_query_embedding(query, model).detach().cpu().numpy()
      )
  
      if cfg.apply_pca:
          query_emb = pca.transform(query_emb)
  
      dist, neighbors = index.search(
          query_emb.astype(np.float32), cfg.query_num_factor * cfg.top_n
      )
      dist, neighbors = dist[0], neighbors[0]
      unique_ids = OrderedDict()
      neighbor_idx = 0
  
      # Many of nearest neighbors could map to the same concept id, their idx is their unique identifier
      while len(unique_ids) < cfg.top_n and neighbor_idx < len(
          neighbors
      ):
          concept_id_idx = neighbors[neighbor_idx]
          concept_id = idx2id[concept_id_idx]
  
          # Only want one instance of each unique concept
          if concept_id not in unique_ids:
              concept = id2string[concept_id]
              unique_ids[concept_id] = (concept, 1 - dist[neighbor_idx])
  
          neighbor_idx += 1
  
      unique_ids = dict(unique_ids)
  
      return unique_ids
  
file_path: NeMo/examples/nlp/entity_linking/query_index.py
imports: import os
  import pickle
  from argparse import ArgumentParser
  from collections import OrderedDict
  from typing import Dict
  import numpy
  import torch
  from build_index import load_model
  from omegaconf import DictConfig, OmegaConf
  from nemo.utils import logging
label_desc: Use image node_id 2 for calling a global function with example usage: query_index(query, cfg, model, index, pca, idx2id, id2string) and returns: unique_ids