code: def convert_ie(
      record: Dict,
      sample: int,
      task: str,
      neg_sampler,
      converter,
      neg_ratio: 0.1,
      input_text="input",
      random_sort=True,
  ):
      if sample == -1:  # 从4种指令和4种输出格式(共16种)中随机采样其中一种
          rand1 = random.randint(0, 19)
          rand2 = random.randint(0, 3)
      else:  # 使用sample指定的指令和数据格式
          rand1 = sample
          rand2 = sample
  
      neg = False
      if neg_ratio > 0:
          rand3 = random.random()
          if rand3 < neg_ratio:
              neg = True
  
      if task == "EE":
          if neg:  # all表示指定需要抽取的类型是全部schema, 而非仅出现在标签中的类型
              record["event"] = neg_sampler.negative_sample(
                  record["event"], "EE"
              )
          if random_sort:
              record["event"], type_role_dict = ee_sort(
                  record[input_text], record["event"]
              )
          else:
              type_role_dict = get_positive_type_role(
                  record["event"], "EE"
              )[2]
              type_role_dict = {
                  k: sorted(list(v))
                  for k, v in sorted(type_role_dict.items())
              }
          sinstruct, output_text = converter.convert(
              record["event"], rand1, rand2, s_schema1=type_role_dict
          )
      elif task == "EEA":
          if neg:  # all表示指定需要抽取的类型是全部schema, 而非仅出现在标签中的类型
              record["event"] = neg_sampler.negative_sample(
                  record["event"], "EEA"
              )
          if random_sort:
              record["event"], type_role_dict = ee_sort(
                  record[input_text], record["event"]
              )
          else:
              type_role_dict = get_positive_type_role(
                  record["event"], "EE"
              )[2]
              type_role_dict = {
                  k: sorted(list(v))
                  for k, v in sorted(type_role_dict.items())
              }
          schema2 = set()
          for event in record["event"]:
              schema2.add((event["event_type"], event["event_trigger"]))
          schema2_dict = [
              {"event_type": e[0], "event_trigger": e[1]}
              for e in schema2
          ]
          sinstruct, output_text = converter.convert(
              record["event"],
              rand1,
              rand2,
              s_schema1=type_role_dict,
              s_schema2=schema2_dict,
          )
      elif task == "EET":
          if neg:  # all表示指定需要抽取的类型是全部schema, 而非仅出现在标签中的类型
              record["event"] = neg_sampler.negative_sample(
                  record["event"], "EET"
              )
          if random_sort:
              record["event"], event_type_list = eet_sort(
                  record[input_text], record["event"]
              )
          else:
              event_type_list = list(
                  get_positive_type_role(record["event"], "EET")[0]
              )
              event_type_list = sorted(event_type_list)
          sinstruct, output_text = converter.convert(
              record["event"], rand1, rand2, s_schema1=event_type_list
          )
      elif task == "RE":
          if neg:
              record["relation"] = neg_sampler.negative_sample(
                  record["relation"], "RE"
              )
          if random_sort:
              record["relation"], rels_type = rel_sort(
                  record[input_text], record["relation"]
              )  # 按关系、头实体、尾实体随机排序
          else:
              rels_type = list(
                  get_positive_type_role(record["relation"], "RE")[1]
              )
              rels_type = sorted(rels_type)
          sinstruct, output_text = converter.convert(
              record["relation"], rand1, rand2, s_schema1=rels_type
          )
      elif task == "NER":
          if neg:
              record["entity"] = neg_sampler.negative_sample(
                  record["entity"], "NER"
              )
          if random_sort:
              record["entity"], ents_type = ent_sort(
                  record[input_text], record["entity"]
              )  # 按实体类型、实体随机排序
          else:
              ents_type = list(
                  get_positive_type_role(record["entity"], "NER")[0]
              )
              ents_type = sorted(ents_type)
          sinstruct, output_text = converter.convert(
              record["entity"], rand1, rand2, s_schema1=ents_type
          )
      else:
          raise KeyError
      return sinstruct, output_text
  
file_path: DeepKE/example/llm/InstructKGC/kg2instruction/convert.py
imports: import sys
  import argparse
  import json
  import os
  from collections import defaultdict
  import random
  from typing import Dict
  from convert.sampler import Sampler, get_positive_type_role
  from convert.random_sort import rel_sort, ent_sort, ee_sort, eet_sort
  from convert.converter import NERConverter, REConverter, EEAConverter, EETConverter, EEConverter
  from utils import stable_hash
label_desc: Use image node_id 2 for calling a global function with example usage: convert_ie(record, sample, task, neg_sampler, converter, neg_ratio, input_text, random_sort) and returns: sinstruct,  output_text