code: from typing import Optional, Union
  
  from llama_index import ServiceContext
  from llama_index.callbacks import CallbackManager
  from llama_index.embeddings.utils import EmbedType
  from llama_index.extractors import (
      EntityExtractor,
      KeywordExtractor,
      QuestionsAnsweredExtractor,
      SummaryExtractor,
      TitleExtractor,
  )
  from llama_index.llms.utils import LLMType
  from llama_index.prompts import PromptTemplate
  from llama_index.prompts.base import BasePromptTemplate
  from llama_index.text_splitter import SentenceSplitter
  
  from autollm.callbacks.cost_calculating import CostCalculatingHandler
  from autollm.utils.llm_utils import set_default_prompt_template
  
  
  class AutoServiceContext:
      """AutoServiceContext extends the functionality of LlamaIndex's ServiceContext to include token
      counting.
      """
  
      @staticmethod
      def from_defaults(
              llm: Optional[LLMType] = "default",
              embed_model: Optional[EmbedType] = "default",
              system_prompt: str = None,
              query_wrapper_prompt: Union[str, BasePromptTemplate] = None,
              enable_cost_calculator: bool = False,
              chunk_size: Optional[int] = 512,
              chunk_overlap: Optional[int] = 100,
              context_window: Optional[int] = None,
              enable_title_extractor: bool = False,
              enable_summary_extractor: bool = False,
              enable_qa_extractor: bool = False,
              enable_keyword_extractor: bool = False,
              enable_entity_extractor: bool = False,
              **kwargs) -> ServiceContext:
          """
          Create a ServiceContext with default parameters with extended enable_token_counting functionality. If
          enable_token_counting is True, tracks the number of tokens used by the LLM for each query.
  
          Parameters:
              llm (LLM): The LLM to use for the query engine. Defaults to gpt-3.5-turbo.
              embed_model (BaseEmbedding): The embedding model to use for the query engine. Defaults to OpenAIEmbedding.
              system_prompt (str): The system prompt to use for the query engine.
              query_wrapper_prompt (Union[str, BasePromptTemplate]): The query wrapper prompt to use for the query engine.
              enable_cost_calculator (bool): Flag to enable cost calculator logging.
              chunk_size (int): The token chunk size for each chunk.
              chunk_overlap (int): The token overlap between each chunk.
              context_window (int): The maximum context size that will get sent to the LLM.
              enable_title_extractor (bool): Flag to enable title extractor.
              enable_summary_extractor (bool): Flag to enable summary extractor.
              enable_qa_extractor (bool): Flag to enable question answering extractor.
              enable_keyword_extractor (bool): Flag to enable keyword extractor.
              enable_entity_extractor (bool): Flag to enable entity extractor.
              **kwargs: Arbitrary keyword arguments.
  
          Returns:
              ServiceContext: The initialized ServiceContext from default parameters with extra token counting functionality.
          """
          if not system_prompt and not query_wrapper_prompt:
              system_prompt, query_wrapper_prompt = set_default_prompt_template()
          # Convert query_wrapper_prompt to PromptTemplate if it is a string
          if isinstance(query_wrapper_prompt, str):
              query_wrapper_prompt = PromptTemplate(template=query_wrapper_prompt)
  
          callback_manager: CallbackManager = kwargs.get('callback_manager', CallbackManager())
          kwargs.pop(
              'callback_manager', None)  # Make sure callback_manager is not passed to ServiceContext twice
  
          if enable_cost_calculator:
              llm_model_name = llm.metadata.model_name if not "default" else "gpt-3.5-turbo"
              callback_manager.add_handler(CostCalculatingHandler(model_name=llm_model_name, verbose=True))
  
          sentence_splitter = SentenceSplitter(chunk_size=chunk_size, chunk_overlap=chunk_overlap)
          transformations = [sentence_splitter]
          if enable_entity_extractor:
              transformations.append(EntityExtractor())
          if enable_keyword_extractor:
              transformations.append(KeywordExtractor(llm=llm, keywords=5))
          if enable_summary_extractor:
              transformations.append(SummaryExtractor(llm=llm, summaries=["prev", "self"]))
          if enable_title_extractor:
              transformations.append(TitleExtractor(llm=llm, nodes=5))
          if enable_qa_extractor:
              transformations.append(QuestionsAnsweredExtractor(llm=llm, questions=5))
  
          service_context = ServiceContext.from_defaults(
              llm=llm,
              embed_model=embed_model,
              transformations=transformations,
              system_prompt=system_prompt,
              query_wrapper_prompt=query_wrapper_prompt,
              chunk_size=chunk_size,
              chunk_overlap=chunk_overlap,
              context_window=context_window,
              callback_manager=callback_manager,
              **kwargs)
  
          return service_context
  
apis: llama_index.extractors.TitleExtractor llama_index.extractors.QuestionsAnsweredExtractor llama_index.ServiceContext.from_defaults llama_index.prompts.PromptTemplate llama_index.extractors.SummaryExtractor llama_index.extractors.EntityExtractor llama_index.extractors.KeywordExtractor llama_index.callbacks.CallbackManager llama_index.text_splitter.SentenceSplitter
extract_api: [((3952, 4020), 'llama_index.text_splitter.SentenceSplitter', 'SentenceSplitter', ([], {'chunk_size': 'chunk_size', 'chunk_overlap': 'chunk_overlap'}), '(chunk_size=chunk_size, chunk_overlap=chunk_overlap)\n', (3968, 4020), False, 'from llama_index.text_splitter import SentenceSplitter\n'), ((4643, 4954), 'llama_index.ServiceContext.from_defaults', 'ServiceContext.from_defaults', ([], {'llm': 'llm', 'embed_model': 'embed_model', 'transformations': 'transformations', 'system_prompt': 'system_prompt', 'query_wrapper_prompt': 'query_wrapper_prompt', 'chunk_size': 'chunk_size', 'chunk_overlap': 'chunk_overlap', 'context_window': 'context_window', 'callback_manager': 'callback_manager'}), '(llm=llm, embed_model=embed_model,\n    transformations=transformations, system_prompt=system_prompt,\n    query_wrapper_prompt=query_wrapper_prompt, chunk_size=chunk_size,\n    chunk_overlap=chunk_overlap, context_window=context_window,\n    callback_manager=callback_manager, **kwargs)\n', (4671, 4954), False, 'from llama_index import ServiceContext\n'), ((3233, 3262), 'autollm.utils.llm_utils.set_default_prompt_template', 'set_default_prompt_template', ([], {}), '()\n', (3260, 3262), False, 'from autollm.utils.llm_utils import set_default_prompt_template\n'), ((3423, 3468), 'llama_index.prompts.PromptTemplate', 'PromptTemplate', ([], {'template': 'query_wrapper_prompt'}), '(template=query_wrapper_prompt)\n', (3437, 3468), False, 'from llama_index.prompts import PromptTemplate\n'), ((3545, 3562), 'llama_index.callbacks.CallbackManager', 'CallbackManager', ([], {}), '()\n', (3560, 3562), False, 'from llama_index.callbacks import CallbackManager\n'), ((3858, 3921), 'autollm.callbacks.cost_calculating.CostCalculatingHandler', 'CostCalculatingHandler', ([], {'model_name': 'llm_model_name', 'verbose': '(True)'}), '(model_name=llm_model_name, verbose=True)\n', (3880, 3921), False, 'from autollm.callbacks.cost_calculating import CostCalculatingHandler\n'), ((4138, 4155), 'llama_index.extractors.EntityExtractor', 'EntityExtractor', ([], {}), '()\n', (4153, 4155), False, 'from llama_index.extractors import EntityExtractor, KeywordExtractor, QuestionsAnsweredExtractor, SummaryExtractor, TitleExtractor\n'), ((4229, 4266), 'llama_index.extractors.KeywordExtractor', 'KeywordExtractor', ([], {'llm': 'llm', 'keywords': '(5)'}), '(llm=llm, keywords=5)\n', (4245, 4266), False, 'from llama_index.extractors import EntityExtractor, KeywordExtractor, QuestionsAnsweredExtractor, SummaryExtractor, TitleExtractor\n'), ((4340, 4393), 'llama_index.extractors.SummaryExtractor', 'SummaryExtractor', ([], {'llm': 'llm', 'summaries': "['prev', 'self']"}), "(llm=llm, summaries=['prev', 'self'])\n", (4356, 4393), False, 'from llama_index.extractors import EntityExtractor, KeywordExtractor, QuestionsAnsweredExtractor, SummaryExtractor, TitleExtractor\n'), ((4465, 4497), 'llama_index.extractors.TitleExtractor', 'TitleExtractor', ([], {'llm': 'llm', 'nodes': '(5)'}), '(llm=llm, nodes=5)\n', (4479, 4497), False, 'from llama_index.extractors import EntityExtractor, KeywordExtractor, QuestionsAnsweredExtractor, SummaryExtractor, TitleExtractor\n'), ((4566, 4614), 'llama_index.extractors.QuestionsAnsweredExtractor', 'QuestionsAnsweredExtractor', ([], {'llm': 'llm', 'questions': '(5)'}), '(llm=llm, questions=5)\n', (4592, 4614), False, 'from llama_index.extractors import EntityExtractor, KeywordExtractor, QuestionsAnsweredExtractor, SummaryExtractor, TitleExtractor\n')]