code: def build_candidate_sampler_config(
      sampler,
  ) -> cs_config_pb2.CandidateSamplerConfig:
      """Builds a CandidateSamplerConfig from given sampler.
  
      Args:
        sampler: an instance of NegativeSamplerConfig or
          BruteForceTopkSamplerConfig.
  
      Returns:
        A valid CandidateSamplerConfig.
  
      Raises:
        ValueError if `sampler` is not valid.
      """
      if not (
          isinstance(sampler, cs_config_pb2.NegativeSamplerConfig)
          or isinstance(
              sampler, cs_config_pb2.BruteForceTopkSamplerConfig
          )
      ):
          raise ValueError(
              "sampler must be one of NegativeSamplerConfig or BruteForceTopkSamplerConfig"
          )
  
      sampler_config = cs_config_pb2.CandidateSamplerConfig()
      sampler_config.extension.Pack(sampler)
      return sampler_config
  
imports: import typing
  from research.carls.candidate_sampling import candidate_sampler_config_pb2
label_desc: Use image node_id 3 for calling a global function with example usage: build_candidate_sampler_config(sampler) and returns: sampler_config