code: import json
  from util import rm_file
  from tqdm import tqdm 
  import argparse
  from copy import deepcopy
  import os 
  from util import JSONReader
  import openai
  from typing import List, Dict
  
  from llama_index import (
      ServiceContext,
      OpenAIEmbedding,
      PromptHelper,
      VectorStoreIndex,
      set_global_service_context
  )
  from llama_index.extractors import BaseExtractor
  from llama_index.ingestion import IngestionPipeline
  from llama_index.embeddings.cohereai import CohereEmbedding
  from llama_index.llms import OpenAI
  from llama_index.text_splitter import SentenceSplitter
  from llama_index.embeddings import HuggingFaceEmbedding,VoyageEmbedding,InstructorEmbedding
  from llama_index.postprocessor import FlagEmbeddingReranker
  from llama_index.schema import QueryBundle,MetadataMode
  
  class CustomExtractor(BaseExtractor):
      async def aextract(self, nodes) -> List[Dict]:
          metadata_list = [
              {
                  "title": (
                      node.metadata["title"]
                  ),
                  "source": (
                      node.metadata["source"]
                  ),      
                  "published_at": (
                      node.metadata["published_at"]
                  )
              }
              for node in nodes
          ]
          return metadata_list
      
  if __name__ == '__main__':
      openai.api_key = os.environ.get("OPENAI_API_KEY", "your_openai_api_key")
      openai.base_url = "your_api_base"
      voyage_api_key = os.environ.get("VOYAGE_API_KEY", "your_voyage_api_key")
      cohere_api_key = os.environ.get("COHERE_API_KEY", "your_cohere_api_key")
  
      
      parser = argparse.ArgumentParser(description="running script.")
      parser.add_argument('--retriever', type=str, required=True, help='retriever name')
      parser.add_argument('--llm', type=str, required=False,default="gpt-3.5-turbo-1106", help='LLMs')
      parser.add_argument('--rerank', action='store_true',required=False,default=False, help='if rerank')
      parser.add_argument('--topk', type=int, required=False,default=10, help='Top K')
      parser.add_argument('--chunk_size', type=int, required=False,default=256, help='chunk_size')
      parser.add_argument('--context_window', type=int, required=False,default=2048, help='context_window')
      parser.add_argument('--num_output', type=int, required=False,default=256, help='num_output')
  
      args = parser.parse_args()
      model_name = args.retriever
      rerank = args.rerank
      top_k = args.topk
      save_model_name = model_name.split('/')
      llm = OpenAI(model=args.llm, temperature=0, max_tokens=args.context_window)
  
      # define save file
      if rerank:
          save_file = f'output/{save_model_name[-1]}_rerank_retrieval_test.json'
      else:
          save_file = f'output/{save_model_name[-1]}_retrieval_test.json'
      rm_file(save_file)
      print(f'save_file:{save_file}')
  
      if 'text' in model_name:
          # "text-embedding-ada-002" “text-search-ada-query-001”
          embed_model = OpenAIEmbedding(model = model_name,embed_batch_size=10)
      elif 'Cohere' in model_name:
          embed_model = CohereEmbedding(
              cohere_api_key=cohere_api_key,
              model_name="embed-english-v3.0",
              input_type="search_query",
          )
      elif 'voyage-02' in model_name:
          embed_model = VoyageEmbedding(
              model_name='voyage-02', voyage_api_key=voyage_api_key
          )
      elif 'instructor' in model_name:
          embed_model = InstructorEmbedding(model_name=model_name)
      else:
          embed_model = HuggingFaceEmbedding(model_name=model_name, trust_remote_code=True)
  
      # service context 
      text_splitter = SentenceSplitter(chunk_size=args.chunk_size)
      prompt_helper = PromptHelper(
          context_window=args.context_window,
          num_output=args.num_output,
          chunk_overlap_ratio=0.1,
          chunk_size_limit=None,
      )
      service_context = ServiceContext.from_defaults(
          llm=llm,
          embed_model=embed_model,
          text_splitter=text_splitter,
          prompt_helper=prompt_helper,
      )
      set_global_service_context(service_context)
  
      reader = JSONReader()
      data = reader.load_data('dataset/corpus.json')
      # print(data[0])
  
          
      transformations = [text_splitter,CustomExtractor()] 
      pipeline = IngestionPipeline(transformations=transformations)
      nodes = pipeline.run(documents=data)
      nodes_see = deepcopy(nodes)
      print(
          "LLM sees:\n",
          (nodes_see)[0].get_content(metadata_mode=MetadataMode.LLM),
      )
      print('Finish Loading...')
  
      index = VectorStoreIndex(nodes, show_progress=True)
      print('Finish Indexing...')
  
      with open('dataset/MultiHopRAG.json', 'r') as file:
          query_data = json.load(file)
  
      if rerank:
          rerank_postprocessors = FlagEmbeddingReranker(model="BAAI/bge-reranker-large", top_n=top_k)
  
      # test retrieval quality
      retrieval_save_list = []
      print("start to retrieve...")
      for data in tqdm(query_data):
          query = data['query']   
          if rerank:
              nodes_score = index.as_retriever(similarity_top_k=20).retrieve(query)
              nodes_score = rerank_postprocessors.postprocess_nodes(
                          nodes_score, query_bundle=QueryBundle(query_str=query)
                      )
          else:
              nodes_score = index.as_retriever(similarity_top_k=top_k).retrieve(query)
          retrieval_list = []
          for ns in nodes_score:
              dic = {}
              dic['text'] = ns.get_content(metadata_mode=MetadataMode.LLM)
              dic['score'] = ns.get_score()
              retrieval_list.append(dic)
          save = {}
          save['query'] = data['query']   
          save['answer'] = data['answer']   
          save['question_type'] = data['question_type'] 
          save['retrieval_list'] = retrieval_list
          save['gold_list'] = data['evidence_list']   
          retrieval_save_list.append(save)
  
      with open(save_file, 'w') as json_file:
          json.dump(retrieval_save_list, json_file)
  
apis: llama_index.OpenAIEmbedding llama_index.ServiceContext.from_defaults llama_index.embeddings.cohereai.CohereEmbedding llama_index.VectorStoreIndex llama_index.postprocessor.FlagEmbeddingReranker llama_index.llms.OpenAI llama_index.embeddings.VoyageEmbedding llama_index.embeddings.HuggingFaceEmbedding llama_index.PromptHelper llama_index.text_splitter.SentenceSplitter llama_index.embeddings.InstructorEmbedding llama_index.set_global_service_context llama_index.schema.QueryBundle llama_index.ingestion.IngestionPipeline
extract_api: [((1340, 1395), 'os.environ.get', 'os.environ.get', (['"""OPENAI_API_KEY"""', '"""your_openai_api_key"""'], {}), "('OPENAI_API_KEY', 'your_openai_api_key')\n", (1354, 1395), False, 'import os\n'), ((1455, 1510), 'os.environ.get', 'os.environ.get', (['"""VOYAGE_API_KEY"""', '"""your_voyage_api_key"""'], {}), "('VOYAGE_API_KEY', 'your_voyage_api_key')\n", (1469, 1510), False, 'import os\n'), ((1532, 1587), 'os.environ.get', 'os.environ.get', (['"""COHERE_API_KEY"""', '"""your_cohere_api_key"""'], {}), "('COHERE_API_KEY', 'your_cohere_api_key')\n", (1546, 1587), False, 'import os\n'), ((1607, 1661), 'argparse.ArgumentParser', 'argparse.ArgumentParser', ([], {'description': '"""running script."""'}), "(description='running script.')\n", (1630, 1661), False, 'import argparse\n'), ((2504, 2573), 'llama_index.llms.OpenAI', 'OpenAI', ([], {'model': 'args.llm', 'temperature': '(0)', 'max_tokens': 'args.context_window'}), '(model=args.llm, temperature=0, max_tokens=args.context_window)\n', (2510, 2573), False, 'from llama_index.llms import OpenAI\n'), ((2778, 2796), 'util.rm_file', 'rm_file', (['save_file'], {}), '(save_file)\n', (2785, 2796), False, 'from util import rm_file\n'), ((3610, 3654), 'llama_index.text_splitter.SentenceSplitter', 'SentenceSplitter', ([], {'chunk_size': 'args.chunk_size'}), '(chunk_size=args.chunk_size)\n', (3626, 3654), False, 'from llama_index.text_splitter import SentenceSplitter\n'), ((3675, 3803), 'llama_index.PromptHelper', 'PromptHelper', ([], {'context_window': 'args.context_window', 'num_output': 'args.num_output', 'chunk_overlap_ratio': '(0.1)', 'chunk_size_limit': 'None'}), '(context_window=args.context_window, num_output=args.num_output,\n    chunk_overlap_ratio=0.1, chunk_size_limit=None)\n', (3687, 3803), False, 'from llama_index import ServiceContext, OpenAIEmbedding, PromptHelper, VectorStoreIndex, set_global_service_context\n'), ((3861, 3985), 'llama_index.ServiceContext.from_defaults', 'ServiceContext.from_defaults', ([], {'llm': 'llm', 'embed_model': 'embed_model', 'text_splitter': 'text_splitter', 'prompt_helper': 'prompt_helper'}), '(llm=llm, embed_model=embed_model,\n    text_splitter=text_splitter, prompt_helper=prompt_helper)\n', (3889, 3985), False, 'from llama_index import ServiceContext, OpenAIEmbedding, PromptHelper, VectorStoreIndex, set_global_service_context\n'), ((4025, 4068), 'llama_index.set_global_service_context', 'set_global_service_context', (['service_context'], {}), '(service_context)\n', (4051, 4068), False, 'from llama_index import ServiceContext, OpenAIEmbedding, PromptHelper, VectorStoreIndex, set_global_service_context\n'), ((4083, 4095), 'util.JSONReader', 'JSONReader', ([], {}), '()\n', (4093, 4095), False, 'from util import JSONReader\n'), ((4250, 4300), 'llama_index.ingestion.IngestionPipeline', 'IngestionPipeline', ([], {'transformations': 'transformations'}), '(transformations=transformations)\n', (4267, 4300), False, 'from llama_index.ingestion import IngestionPipeline\n'), ((4358, 4373), 'copy.deepcopy', 'deepcopy', (['nodes'], {}), '(nodes)\n', (4366, 4373), False, 'from copy import deepcopy\n'), ((4526, 4569), 'llama_index.VectorStoreIndex', 'VectorStoreIndex', (['nodes'], {'show_progress': '(True)'}), '(nodes, show_progress=True)\n', (4542, 4569), False, 'from llama_index import ServiceContext, OpenAIEmbedding, PromptHelper, VectorStoreIndex, set_global_service_context\n'), ((4921, 4937), 'tqdm.tqdm', 'tqdm', (['query_data'], {}), '(query_data)\n', (4925, 4937), False, 'from tqdm import tqdm\n'), ((2948, 3002), 'llama_index.OpenAIEmbedding', 'OpenAIEmbedding', ([], {'model': 'model_name', 'embed_batch_size': '(10)'}), '(model=model_name, embed_batch_size=10)\n', (2963, 3002), False, 'from llama_index import ServiceContext, OpenAIEmbedding, PromptHelper, VectorStoreIndex, set_global_service_context\n'), ((4680, 4695), 'json.load', 'json.load', (['file'], {}), '(file)\n', (4689, 4695), False, 'import json\n'), ((4744, 4811), 'llama_index.postprocessor.FlagEmbeddingReranker', 'FlagEmbeddingReranker', ([], {'model': '"""BAAI/bge-reranker-large"""', 'top_n': 'top_k'}), "(model='BAAI/bge-reranker-large', top_n=top_k)\n", (4765, 4811), False, 'from llama_index.postprocessor import FlagEmbeddingReranker\n'), ((5926, 5967), 'json.dump', 'json.dump', (['retrieval_save_list', 'json_file'], {}), '(retrieval_save_list, json_file)\n', (5935, 5967), False, 'import json\n'), ((3059, 3170), 'llama_index.embeddings.cohereai.CohereEmbedding', 'CohereEmbedding', ([], {'cohere_api_key': 'cohere_api_key', 'model_name': '"""embed-english-v3.0"""', 'input_type': '"""search_query"""'}), "(cohere_api_key=cohere_api_key, model_name=\n    'embed-english-v3.0', input_type='search_query')\n", (3074, 3170), False, 'from llama_index.embeddings.cohereai import CohereEmbedding\n'), ((3271, 3341), 'llama_index.embeddings.VoyageEmbedding', 'VoyageEmbedding', ([], {'model_name': '"""voyage-02"""', 'voyage_api_key': 'voyage_api_key'}), "(model_name='voyage-02', voyage_api_key=voyage_api_key)\n", (3286, 3341), False, 'from llama_index.embeddings import HuggingFaceEmbedding, VoyageEmbedding, InstructorEmbedding\n'), ((3423, 3465), 'llama_index.embeddings.InstructorEmbedding', 'InstructorEmbedding', ([], {'model_name': 'model_name'}), '(model_name=model_name)\n', (3442, 3465), False, 'from llama_index.embeddings import HuggingFaceEmbedding, VoyageEmbedding, InstructorEmbedding\n'), ((3498, 3565), 'llama_index.embeddings.HuggingFaceEmbedding', 'HuggingFaceEmbedding', ([], {'model_name': 'model_name', 'trust_remote_code': '(True)'}), '(model_name=model_name, trust_remote_code=True)\n', (3518, 3565), False, 'from llama_index.embeddings import HuggingFaceEmbedding, VoyageEmbedding, InstructorEmbedding\n'), ((5190, 5218), 'llama_index.schema.QueryBundle', 'QueryBundle', ([], {'query_str': 'query'}), '(query_str=query)\n', (5201, 5218), False, 'from llama_index.schema import QueryBundle, MetadataMode\n')]