code: def process_result(self, batch):
      while True:
          try:
              items = [i["item"] for i in batch]
              turns = [i["turn"] for i in batch]
              prompts = [i["prompt"] for i in batch]
  
              for label_id in range(1, len(selected_keys)):
                  results = self.req(
                      prompts,
                      greedy=True,
                      add_BOS=False,
                      token_to_gen=1,
                      min_tokens=1,
                      temp=0.1,
                      top_p=1.0,
                      top_k=1,
                      repetition=1.0,
                      end_strings=["<extra_id_1>", "<|endoftext|>"],
                  )
                  # get current value from result
                  current_values = []
                  nums = []
                  for result in results:
                      # promblem result[-1] is '\n'
                      current_val = result.split("quality")[-1]
                      current_val = "quality" + current_val
                      # remove whatever after new line
                      current_val = current_val.split("\n")[0].strip()
                      # remove everything that is >= selected_keys[label_id]
                      splits = current_val.split(",")
                      filtered = []
                      for item in splits:
                          filtered.append(item)
                          if (
                              item.split(":")[0]
                              == selected_keys[label_id - 1]
                          ):
                              nums.append(item.split(":")[1])
                              break
                      current_val = "<extra_id_2>" + ",".join(filtered)
                      current_values.append(current_val)
  
                  filtered_items = []
                  filtered_turns = []
                  filtered_prompts = []
                  filtered_current_values = []
  
                  for result, item, turn, num, current_value in zip(
                      results, items, turns, nums, current_values
                  ):
                      try:
                          value = int(num)
                      except Exception as e:
                          print(f"error {e} when convert {num} to int")
                          continue
                      filtered_current_values.append(current_value)
                      filtered_items.append(item)
                      filtered_turns.append(turn)
                      if label_id < len(selected_keys):
                          prompt = get_prompt(
                              item,
                              turn,
                              current_label=current_value,
                              label_id=label_id,
                          )
                          filtered_prompts.append(prompt)
                  items = filtered_items
                  turns = filtered_turns
                  prompts = filtered_prompts
                  current_values = filtered_current_values
  
              if self.process_lang:
                  results = self.req(
                      prompts,
                      greedy=True,
                      add_BOS=False,
                      token_to_gen=1,
                      min_tokens=1,
                      temp=0.1,
                      top_p=1.0,
                      top_k=1,
                      repetition=1.0,
                      end_strings=["<extra_id_1>", "<|endoftext|>"],
                  )
                  # get current value from result
                  current_values = []
                  for result in results:
                      # promblem result[-1] is '\n'
                      if result.endswith("\n"):
                          result = result[:-1] + "@"
                      current_values.append(result.split("\n")[-1])
  
                  nums = []
                  for result in results:
                      # promblem result[-1] is '\n'
                      current_val = result.split("quality")[-1]
                      current_val = "quality" + current_val
                      # remove whatever after new line
                      current_val = current_val.split("\n")[0].strip()
                      # remove everything that is >= selected_keys[label_id]
                      splits = current_val.split(",")
                      filtered = []
                      for item in splits:
                          filtered.append(item)
                          if (
                              item.split(":")[0]
                              == selected_keys[label_id]
                          ):
                              nums.append(item.split(":")[1])
                              break
                      current_val = "<extra_id_2>" + ",".join(filtered)
                      current_values.append(current_val)
  
                  filtered_items = []
                  filtered_turns = []
                  filtered_prompts = []
                  filtered_current_values = []
  
                  for result, item, turn, num, current_value in zip(
                      results, items, turns, nums, current_values
                  ):
                      if num not in langs:
                          print(f"error {num} not in langs")
                          continue
                      filtered_current_values.append(current_value)
                      filtered_items.append(item)
                      filtered_turns.append(turn)
                  items = filtered_items
                  turns = filtered_turns
                  current_values = filtered_current_values
  
              batch = []
              for item, turn, current_value in zip(
                  items, turns, current_values
              ):
                  response_text = current_value[12:]
                  if "label" in item["conversations"][turn]:
                      item["conversations"][turn]["gt_label"] = item[
                          "conversations"
                      ][turn]["label"]
                  item["conversations"][turn]["label"] = response_text
                  prompt = get_prompt(
                      item, turn + 1, current_label="", label_id=0
                  )
                  if prompt is not None:
                      batch.append(
                          {
                              "prompt": prompt,
                              "item": item,
                              "turn": turn + 1,
                          }
                      )
                  else:
                      self.progress_bar.update(1)
                      self.fout.write(
                          json.dumps(item, ensure_ascii=False) + "\n"
                      )
                      self.fout.flush()
                      if self.progress_bar.n >= self.progress_bar.total:
                          break
              if len(batch) == 0:
                  break
          except Exception as e:
              print(f"error {e} when processing {batch}")
              # ignore the error and continue
              self.progress_bar.update(1)
              if self.progress_bar.n >= self.progress_bar.total:
                  break
  
file_path: NeMo/scripts/nlp_language_modeling/sft/attribute_annotate.py
imports: import json
  import os
  import fire
  import tqdm
  from langchain.prompts.few_shot import PromptTemplate
  from nemo.collections.nlp.modules.common.megatron.retrieval_services.util import text_generation
label_desc: Use image node_id 2 for calling the Worker obj's underlying member method code with example usage: obj.process_result(batch) without return types