code: from typing import Any, List, Optional, Sequence
  
  from llama_index.core.base.base_query_engine import BaseQueryEngine
  from llama_index.core.base.base_retriever import BaseRetriever
  from llama_index.core.base.response.schema import RESPONSE_TYPE
  from llama_index.core.bridge.pydantic import BaseModel
  from llama_index.core.callbacks.base import CallbackManager
  from llama_index.core.callbacks.schema import CBEventType, EventPayload
  from llama_index.core.llms.llm import LLM
  from llama_index.core.postprocessor.types import BaseNodePostprocessor
  from llama_index.core.prompts import BasePromptTemplate
  from llama_index.core.prompts.mixin import PromptMixinType
  from llama_index.core.response_synthesizers import (
      BaseSynthesizer,
      ResponseMode,
      get_response_synthesizer,
  )
  from llama_index.core.schema import NodeWithScore, QueryBundle
  from llama_index.core.service_context import ServiceContext
  from llama_index.core.settings import (
      Settings,
      callback_manager_from_settings_or_context,
      llm_from_settings_or_context,
  )
  import llama_index.core.instrumentation as instrument
  
  dispatcher = instrument.get_dispatcher(__name__)
  
  
  class RetrieverQueryEngine(BaseQueryEngine):
      """Retriever query engine.
  
      Args:
          retriever (BaseRetriever): A retriever object.
          response_synthesizer (Optional[BaseSynthesizer]): A BaseSynthesizer
              object.
          callback_manager (Optional[CallbackManager]): A callback manager.
      """
  
      def __init__(
          self,
          retriever: BaseRetriever,
          response_synthesizer: Optional[BaseSynthesizer] = None,
          node_postprocessors: Optional[List[BaseNodePostprocessor]] = None,
          callback_manager: Optional[CallbackManager] = None,
      ) -> None:
          self._retriever = retriever
          self._response_synthesizer = response_synthesizer or get_response_synthesizer(
              llm=llm_from_settings_or_context(Settings, retriever.get_service_context()),
              callback_manager=callback_manager
              or callback_manager_from_settings_or_context(
                  Settings, retriever.get_service_context()
              ),
          )
  
          self._node_postprocessors = node_postprocessors or []
          callback_manager = (
              callback_manager or self._response_synthesizer.callback_manager
          )
          for node_postprocessor in self._node_postprocessors:
              node_postprocessor.callback_manager = callback_manager
          super().__init__(callback_manager=callback_manager)
  
      def _get_prompt_modules(self) -> PromptMixinType:
          """Get prompt sub-modules."""
          return {"response_synthesizer": self._response_synthesizer}
  
      @classmethod
      def from_args(
          cls,
          retriever: BaseRetriever,
          llm: Optional[LLM] = None,
          response_synthesizer: Optional[BaseSynthesizer] = None,
          node_postprocessors: Optional[List[BaseNodePostprocessor]] = None,
          # response synthesizer args
          response_mode: ResponseMode = ResponseMode.COMPACT,
          text_qa_template: Optional[BasePromptTemplate] = None,
          refine_template: Optional[BasePromptTemplate] = None,
          summary_template: Optional[BasePromptTemplate] = None,
          simple_template: Optional[BasePromptTemplate] = None,
          output_cls: Optional[BaseModel] = None,
          use_async: bool = False,
          streaming: bool = False,
          # deprecated
          service_context: Optional[ServiceContext] = None,
          **kwargs: Any,
      ) -> "RetrieverQueryEngine":
          """Initialize a RetrieverQueryEngine object.".
  
          Args:
              retriever (BaseRetriever): A retriever object.
              service_context (Optional[ServiceContext]): A ServiceContext object.
              node_postprocessors (Optional[List[BaseNodePostprocessor]]): A list of
                  node postprocessors.
              verbose (bool): Whether to print out debug info.
              response_mode (ResponseMode): A ResponseMode object.
              text_qa_template (Optional[BasePromptTemplate]): A BasePromptTemplate
                  object.
              refine_template (Optional[BasePromptTemplate]): A BasePromptTemplate object.
              simple_template (Optional[BasePromptTemplate]): A BasePromptTemplate object.
  
              use_async (bool): Whether to use async.
              streaming (bool): Whether to use streaming.
              optimizer (Optional[BaseTokenUsageOptimizer]): A BaseTokenUsageOptimizer
                  object.
  
          """
          llm = llm or llm_from_settings_or_context(Settings, service_context)
  
          response_synthesizer = response_synthesizer or get_response_synthesizer(
              llm=llm,
              service_context=service_context,
              text_qa_template=text_qa_template,
              refine_template=refine_template,
              summary_template=summary_template,
              simple_template=simple_template,
              response_mode=response_mode,
              output_cls=output_cls,
              use_async=use_async,
              streaming=streaming,
          )
  
          callback_manager = callback_manager_from_settings_or_context(
              Settings, service_context
          )
  
          return cls(
              retriever=retriever,
              response_synthesizer=response_synthesizer,
              callback_manager=callback_manager,
              node_postprocessors=node_postprocessors,
          )
  
      def _apply_node_postprocessors(
          self, nodes: List[NodeWithScore], query_bundle: QueryBundle
      ) -> List[NodeWithScore]:
          for node_postprocessor in self._node_postprocessors:
              nodes = node_postprocessor.postprocess_nodes(
                  nodes, query_bundle=query_bundle
              )
          return nodes
  
      def retrieve(self, query_bundle: QueryBundle) -> List[NodeWithScore]:
          nodes = self._retriever.retrieve(query_bundle)
          return self._apply_node_postprocessors(nodes, query_bundle=query_bundle)
  
      async def aretrieve(self, query_bundle: QueryBundle) -> List[NodeWithScore]:
          nodes = await self._retriever.aretrieve(query_bundle)
          return self._apply_node_postprocessors(nodes, query_bundle=query_bundle)
  
      def with_retriever(self, retriever: BaseRetriever) -> "RetrieverQueryEngine":
          return RetrieverQueryEngine(
              retriever=retriever,
              response_synthesizer=self._response_synthesizer,
              callback_manager=self.callback_manager,
              node_postprocessors=self._node_postprocessors,
          )
  
      def synthesize(
          self,
          query_bundle: QueryBundle,
          nodes: List[NodeWithScore],
          additional_source_nodes: Optional[Sequence[NodeWithScore]] = None,
      ) -> RESPONSE_TYPE:
          return self._response_synthesizer.synthesize(
              query=query_bundle,
              nodes=nodes,
              additional_source_nodes=additional_source_nodes,
          )
  
      async def asynthesize(
          self,
          query_bundle: QueryBundle,
          nodes: List[NodeWithScore],
          additional_source_nodes: Optional[Sequence[NodeWithScore]] = None,
      ) -> RESPONSE_TYPE:
          return await self._response_synthesizer.asynthesize(
              query=query_bundle,
              nodes=nodes,
              additional_source_nodes=additional_source_nodes,
          )
  
      @dispatcher.span
      def _query(self, query_bundle: QueryBundle) -> RESPONSE_TYPE:
          """Answer a query."""
          with self.callback_manager.event(
              CBEventType.QUERY, payload={EventPayload.QUERY_STR: query_bundle.query_str}
          ) as query_event:
              nodes = self.retrieve(query_bundle)
              response = self._response_synthesizer.synthesize(
                  query=query_bundle,
                  nodes=nodes,
              )
              query_event.on_end(payload={EventPayload.RESPONSE: response})
  
          return response
  
      @dispatcher.span
      async def _aquery(self, query_bundle: QueryBundle) -> RESPONSE_TYPE:
          """Answer a query."""
          with self.callback_manager.event(
              CBEventType.QUERY, payload={EventPayload.QUERY_STR: query_bundle.query_str}
          ) as query_event:
              nodes = await self.aretrieve(query_bundle)
  
              response = await self._response_synthesizer.asynthesize(
                  query=query_bundle,
                  nodes=nodes,
              )
  
              query_event.on_end(payload={EventPayload.RESPONSE: response})
  
          return response
  
      @property
      def retriever(self) -> BaseRetriever:
          """Get the retriever object."""
          return self._retriever
  
apis: llama_index.core.response_synthesizers.get_response_synthesizer llama_index.core.settings.callback_manager_from_settings_or_context llama_index.core.settings.llm_from_settings_or_context llama_index.core.instrumentation.get_dispatcher
extract_api: [((1112, 1147), 'llama_index.core.instrumentation.get_dispatcher', 'instrument.get_dispatcher', (['__name__'], {}), '(__name__)\n', (1137, 1147), True, 'import llama_index.core.instrumentation as instrument\n'), ((5090, 5158), 'llama_index.core.settings.callback_manager_from_settings_or_context', 'callback_manager_from_settings_or_context', (['Settings', 'service_context'], {}), '(Settings, service_context)\n', (5131, 5158), False, 'from llama_index.core.settings import Settings, callback_manager_from_settings_or_context, llm_from_settings_or_context\n'), ((4522, 4577), 'llama_index.core.settings.llm_from_settings_or_context', 'llm_from_settings_or_context', (['Settings', 'service_context'], {}), '(Settings, service_context)\n', (4550, 4577), False, 'from llama_index.core.settings import Settings, callback_manager_from_settings_or_context, llm_from_settings_or_context\n'), ((4634, 4946), 'llama_index.core.response_synthesizers.get_response_synthesizer', 'get_response_synthesizer', ([], {'llm': 'llm', 'service_context': 'service_context', 'text_qa_template': 'text_qa_template', 'refine_template': 'refine_template', 'summary_template': 'summary_template', 'simple_template': 'simple_template', 'response_mode': 'response_mode', 'output_cls': 'output_cls', 'use_async': 'use_async', 'streaming': 'streaming'}), '(llm=llm, service_context=service_context,\n    text_qa_template=text_qa_template, refine_template=refine_template,\n    summary_template=summary_template, simple_template=simple_template,\n    response_mode=response_mode, output_cls=output_cls, use_async=use_async,\n    streaming=streaming)\n', (4658, 4946), False, 'from llama_index.core.response_synthesizers import BaseSynthesizer, ResponseMode, get_response_synthesizer\n')]