code: import os
  from dotenv import load_dotenv, find_dotenv
  
  import numpy as np
  from trulens_eval import (
      Feedback,
      TruLlama,
      OpenAI
  )
  
  from trulens_eval.feedback import Groundedness
  import nest_asyncio
  
  nest_asyncio.apply()
  
  
  def get_openai_api_key():
      _ = load_dotenv(find_dotenv())
  
      return os.getenv("OPENAI_API_KEY")
  
  
  def get_hf_api_key():
      _ = load_dotenv(find_dotenv())
  
      return os.getenv("HUGGINGFACE_API_KEY")
  
  openai = OpenAI()
  
  qa_relevance = (
      Feedback(openai.relevance_with_cot_reasons, name="Answer Relevance")
      .on_input_output()
  )
  
  qs_relevance = (
      Feedback(openai.relevance_with_cot_reasons, name = "Context Relevance")
      .on_input()
      .on(TruLlama.select_source_nodes().node.text)
      .aggregate(np.mean)
  )
  
  #grounded = Groundedness(groundedness_provider=openai, summarize_provider=openai)
  grounded = Groundedness(groundedness_provider=openai)
  
  groundedness = (
      Feedback(grounded.groundedness_measure_with_cot_reasons, name="Groundedness")
          .on(TruLlama.select_source_nodes().node.text)
          .on_output()
          .aggregate(grounded.grounded_statements_aggregator)
  )
  
  feedbacks = [qa_relevance, qs_relevance, groundedness]
  
  def get_trulens_recorder(query_engine, feedbacks, app_id):
      tru_recorder = TruLlama(
          query_engine,
          app_id=app_id,
          feedbacks=feedbacks
      )
      return tru_recorder
  
  def get_prebuilt_trulens_recorder(query_engine, app_id):
      tru_recorder = TruLlama(
          query_engine,
          app_id=app_id,
          feedbacks=feedbacks
          )
      return tru_recorder
  
  from llama_index import ServiceContext, VectorStoreIndex, StorageContext
  from llama_index.node_parser import SentenceWindowNodeParser
  from llama_index.indices.postprocessor import MetadataReplacementPostProcessor
  from llama_index.indices.postprocessor import SentenceTransformerRerank
  from llama_index import load_index_from_storage
  import os
  
  
  def build_sentence_window_index(
      document, llm, embed_model="local:BAAI/bge-small-en-v1.5", save_dir="sentence_index"
  ):
      # create the sentence window node parser w/ default settings
      node_parser = SentenceWindowNodeParser.from_defaults(
          window_size=3,
          window_metadata_key="window",
          original_text_metadata_key="original_text",
      )
      sentence_context = ServiceContext.from_defaults(
          llm=llm,
          embed_model=embed_model,
          node_parser=node_parser,
      )
      if not os.path.exists(save_dir):
          sentence_index = VectorStoreIndex.from_documents(
              [document], service_context=sentence_context
          )
          sentence_index.storage_context.persist(persist_dir=save_dir)
      else:
          sentence_index = load_index_from_storage(
              StorageContext.from_defaults(persist_dir=save_dir),
              service_context=sentence_context,
          )
  
      return sentence_index
  
  
  def get_sentence_window_query_engine(
      sentence_index,
      similarity_top_k=6,
      rerank_top_n=2,
  ):
      # define postprocessors
      postproc = MetadataReplacementPostProcessor(target_metadata_key="window")
      rerank = SentenceTransformerRerank(
          top_n=rerank_top_n, model="BAAI/bge-reranker-base"
      )
  
      sentence_window_engine = sentence_index.as_query_engine(
          similarity_top_k=similarity_top_k, node_postprocessors=[postproc, rerank]
      )
      return sentence_window_engine
  
  
  from llama_index.node_parser import HierarchicalNodeParser
  
  from llama_index.node_parser import get_leaf_nodes
  from llama_index import StorageContext
  from llama_index.retrievers import AutoMergingRetriever
  from llama_index.indices.postprocessor import SentenceTransformerRerank
  from llama_index.query_engine import RetrieverQueryEngine
  
  
  def build_automerging_index(
      documents,
      llm,
      embed_model="local:BAAI/bge-small-en-v1.5",
      save_dir="merging_index",
      chunk_sizes=None,
  ):
      chunk_sizes = chunk_sizes or [2048, 512, 128]
      node_parser = HierarchicalNodeParser.from_defaults(chunk_sizes=chunk_sizes)
      nodes = node_parser.get_nodes_from_documents(documents)
      leaf_nodes = get_leaf_nodes(nodes)
      merging_context = ServiceContext.from_defaults(
          llm=llm,
          embed_model=embed_model,
      )
      storage_context = StorageContext.from_defaults()
      storage_context.docstore.add_documents(nodes)
  
      if not os.path.exists(save_dir):
          automerging_index = VectorStoreIndex(
              leaf_nodes, storage_context=storage_context, service_context=merging_context
          )
          automerging_index.storage_context.persist(persist_dir=save_dir)
      else:
          automerging_index = load_index_from_storage(
              StorageContext.from_defaults(persist_dir=save_dir),
              service_context=merging_context,
          )
      return automerging_index
  
  
  def get_automerging_query_engine(
      automerging_index,
      similarity_top_k=12,
      rerank_top_n=2,
  ):
      base_retriever = automerging_index.as_retriever(similarity_top_k=similarity_top_k)
      retriever = AutoMergingRetriever(
          base_retriever, automerging_index.storage_context, verbose=True
      )
      rerank = SentenceTransformerRerank(
          top_n=rerank_top_n, model="BAAI/bge-reranker-base"
      )
      auto_merging_engine = RetrieverQueryEngine.from_args(
          retriever, node_postprocessors=[rerank]
      )
      return auto_merging_engine
apis: llama_index.VectorStoreIndex.from_documents llama_index.retrievers.AutoMergingRetriever llama_index.node_parser.get_leaf_nodes llama_index.ServiceContext.from_defaults llama_index.StorageContext.from_defaults llama_index.node_parser.SentenceWindowNodeParser.from_defaults llama_index.VectorStoreIndex llama_index.node_parser.HierarchicalNodeParser.from_defaults llama_index.query_engine.RetrieverQueryEngine.from_args llama_index.indices.postprocessor.MetadataReplacementPostProcessor llama_index.indices.postprocessor.SentenceTransformerRerank
extract_api: [((211, 231), 'nest_asyncio.apply', 'nest_asyncio.apply', ([], {}), '()\n', (229, 231), False, 'import nest_asyncio\n'), ((449, 457), 'trulens_eval.OpenAI', 'OpenAI', ([], {}), '()\n', (455, 457), False, 'from trulens_eval import Feedback, TruLlama, OpenAI\n'), ((854, 896), 'trulens_eval.feedback.Groundedness', 'Groundedness', ([], {'groundedness_provider': 'openai'}), '(groundedness_provider=openai)\n', (866, 896), False, 'from trulens_eval.feedback import Groundedness\n'), ((307, 334), 'os.getenv', 'os.getenv', (['"""OPENAI_API_KEY"""'], {}), "('OPENAI_API_KEY')\n", (316, 334), False, 'import os\n'), ((406, 438), 'os.getenv', 'os.getenv', (['"""HUGGINGFACE_API_KEY"""'], {}), "('HUGGINGFACE_API_KEY')\n", (415, 438), False, 'import os\n'), ((1269, 1327), 'trulens_eval.TruLlama', 'TruLlama', (['query_engine'], {'app_id': 'app_id', 'feedbacks': 'feedbacks'}), '(query_engine, app_id=app_id, feedbacks=feedbacks)\n', (1277, 1327), False, 'from trulens_eval import Feedback, TruLlama, OpenAI\n'), ((1459, 1517), 'trulens_eval.TruLlama', 'TruLlama', (['query_engine'], {'app_id': 'app_id', 'feedbacks': 'feedbacks'}), '(query_engine, app_id=app_id, feedbacks=feedbacks)\n', (1467, 1517), False, 'from trulens_eval import Feedback, TruLlama, OpenAI\n'), ((2130, 2262), 'llama_index.node_parser.SentenceWindowNodeParser.from_defaults', 'SentenceWindowNodeParser.from_defaults', ([], {'window_size': '(3)', 'window_metadata_key': '"""window"""', 'original_text_metadata_key': '"""original_text"""'}), "(window_size=3, window_metadata_key=\n    'window', original_text_metadata_key='original_text')\n", (2168, 2262), False, 'from llama_index.node_parser import SentenceWindowNodeParser\n'), ((2312, 2404), 'llama_index.ServiceContext.from_defaults', 'ServiceContext.from_defaults', ([], {'llm': 'llm', 'embed_model': 'embed_model', 'node_parser': 'node_parser'}), '(llm=llm, embed_model=embed_model, node_parser=\n    node_parser)\n', (2340, 2404), False, 'from llama_index import ServiceContext, VectorStoreIndex, StorageContext\n'), ((3019, 3081), 'llama_index.indices.postprocessor.MetadataReplacementPostProcessor', 'MetadataReplacementPostProcessor', ([], {'target_metadata_key': '"""window"""'}), "(target_metadata_key='window')\n", (3051, 3081), False, 'from llama_index.indices.postprocessor import MetadataReplacementPostProcessor\n'), ((3095, 3172), 'llama_index.indices.postprocessor.SentenceTransformerRerank', 'SentenceTransformerRerank', ([], {'top_n': 'rerank_top_n', 'model': '"""BAAI/bge-reranker-base"""'}), "(top_n=rerank_top_n, model='BAAI/bge-reranker-base')\n", (3120, 3172), False, 'from llama_index.indices.postprocessor import SentenceTransformerRerank\n'), ((3935, 3996), 'llama_index.node_parser.HierarchicalNodeParser.from_defaults', 'HierarchicalNodeParser.from_defaults', ([], {'chunk_sizes': 'chunk_sizes'}), '(chunk_sizes=chunk_sizes)\n', (3971, 3996), False, 'from llama_index.node_parser import HierarchicalNodeParser\n'), ((4074, 4095), 'llama_index.node_parser.get_leaf_nodes', 'get_leaf_nodes', (['nodes'], {}), '(nodes)\n', (4088, 4095), False, 'from llama_index.node_parser import get_leaf_nodes\n'), ((4118, 4180), 'llama_index.ServiceContext.from_defaults', 'ServiceContext.from_defaults', ([], {'llm': 'llm', 'embed_model': 'embed_model'}), '(llm=llm, embed_model=embed_model)\n', (4146, 4180), False, 'from llama_index import ServiceContext, VectorStoreIndex, StorageContext\n'), ((4226, 4256), 'llama_index.StorageContext.from_defaults', 'StorageContext.from_defaults', ([], {}), '()\n', (4254, 4256), False, 'from llama_index import StorageContext\n'), ((4983, 5072), 'llama_index.retrievers.AutoMergingRetriever', 'AutoMergingRetriever', (['base_retriever', 'automerging_index.storage_context'], {'verbose': '(True)'}), '(base_retriever, automerging_index.storage_context,\n    verbose=True)\n', (5003, 5072), False, 'from llama_index.retrievers import AutoMergingRetriever\n'), ((5096, 5173), 'llama_index.indices.postprocessor.SentenceTransformerRerank', 'SentenceTransformerRerank', ([], {'top_n': 'rerank_top_n', 'model': '"""BAAI/bge-reranker-base"""'}), "(top_n=rerank_top_n, model='BAAI/bge-reranker-base')\n", (5121, 5173), False, 'from llama_index.indices.postprocessor import SentenceTransformerRerank\n'), ((5214, 5285), 'llama_index.query_engine.RetrieverQueryEngine.from_args', 'RetrieverQueryEngine.from_args', (['retriever'], {'node_postprocessors': '[rerank]'}), '(retriever, node_postprocessors=[rerank])\n', (5244, 5285), False, 'from llama_index.query_engine import RetrieverQueryEngine\n'), ((280, 293), 'dotenv.find_dotenv', 'find_dotenv', ([], {}), '()\n', (291, 293), False, 'from dotenv import load_dotenv, find_dotenv\n'), ((379, 392), 'dotenv.find_dotenv', 'find_dotenv', ([], {}), '()\n', (390, 392), False, 'from dotenv import load_dotenv, find_dotenv\n'), ((480, 548), 'trulens_eval.Feedback', 'Feedback', (['openai.relevance_with_cot_reasons'], {'name': '"""Answer Relevance"""'}), "(openai.relevance_with_cot_reasons, name='Answer Relevance')\n", (488, 548), False, 'from trulens_eval import Feedback, TruLlama, OpenAI\n'), ((2442, 2466), 'os.path.exists', 'os.path.exists', (['save_dir'], {}), '(save_dir)\n', (2456, 2466), False, 'import os\n'), ((2493, 2570), 'llama_index.VectorStoreIndex.from_documents', 'VectorStoreIndex.from_documents', (['[document]'], {'service_context': 'sentence_context'}), '([document], service_context=sentence_context)\n', (2524, 2570), False, 'from llama_index import ServiceContext, VectorStoreIndex, StorageContext\n'), ((4319, 4343), 'os.path.exists', 'os.path.exists', (['save_dir'], {}), '(save_dir)\n', (4333, 4343), False, 'import os\n'), ((4373, 4471), 'llama_index.VectorStoreIndex', 'VectorStoreIndex', (['leaf_nodes'], {'storage_context': 'storage_context', 'service_context': 'merging_context'}), '(leaf_nodes, storage_context=storage_context,\n    service_context=merging_context)\n', (4389, 4471), False, 'from llama_index import ServiceContext, VectorStoreIndex, StorageContext\n'), ((2734, 2784), 'llama_index.StorageContext.from_defaults', 'StorageContext.from_defaults', ([], {'persist_dir': 'save_dir'}), '(persist_dir=save_dir)\n', (2762, 2784), False, 'from llama_index import StorageContext\n'), ((4637, 4687), 'llama_index.StorageContext.from_defaults', 'StorageContext.from_defaults', ([], {'persist_dir': 'save_dir'}), '(persist_dir=save_dir)\n', (4665, 4687), False, 'from llama_index import StorageContext\n'), ((692, 722), 'trulens_eval.TruLlama.select_source_nodes', 'TruLlama.select_source_nodes', ([], {}), '()\n', (720, 722), False, 'from trulens_eval import Feedback, TruLlama, OpenAI\n'), ((596, 665), 'trulens_eval.Feedback', 'Feedback', (['openai.relevance_with_cot_reasons'], {'name': '"""Context Relevance"""'}), "(openai.relevance_with_cot_reasons, name='Context Relevance')\n", (604, 665), False, 'from trulens_eval import Feedback, TruLlama, OpenAI\n'), ((919, 996), 'trulens_eval.Feedback', 'Feedback', (['grounded.groundedness_measure_with_cot_reasons'], {'name': '"""Groundedness"""'}), "(grounded.groundedness_measure_with_cot_reasons, name='Groundedness')\n", (927, 996), False, 'from trulens_eval import Feedback, TruLlama, OpenAI\n'), ((1009, 1039), 'trulens_eval.TruLlama.select_source_nodes', 'TruLlama.select_source_nodes', ([], {}), '()\n', (1037, 1039), False, 'from trulens_eval import Feedback, TruLlama, OpenAI\n')]