class BaseQueryEngine(ChainableMixin, PromptMixin):
"""基本查询引擎。"""
def __init__(
self,
callback_manager: Optional[CallbackManager],
) -> None:
self.callback_manager = callback_manager or CallbackManager([])
def _get_prompts(self) -> Dict[str, Any]:
"""获取提示。"""
return {}
def _update_prompts(self, prompts: PromptDictType) -> None:
"""更新提示。"""
@dispatcher.span
def query(self, str_or_query_bundle: QueryType) -> RESPONSE_TYPE:
dispatch_event = dispatcher.get_dispatch_event()
dispatch_event(QueryStartEvent(query=str_or_query_bundle))
with self.callback_manager.as_trace("query"):
if isinstance(str_or_query_bundle, str):
str_or_query_bundle = QueryBundle(str_or_query_bundle)
query_result = self._query(str_or_query_bundle)
dispatch_event(QueryEndEvent(query=str_or_query_bundle, response=query_result))
return query_result
@dispatcher.span
async def aquery(self, str_or_query_bundle: QueryType) -> RESPONSE_TYPE:
dispatch_event = dispatcher.get_dispatch_event()
dispatch_event(QueryStartEvent(query=str_or_query_bundle))
with self.callback_manager.as_trace("query"):
if isinstance(str_or_query_bundle, str):
str_or_query_bundle = QueryBundle(str_or_query_bundle)
query_result = await self._aquery(str_or_query_bundle)
dispatch_event(QueryEndEvent(query=str_or_query_bundle, response=query_result))
return query_result
def retrieve(self, query_bundle: QueryBundle) -> List[NodeWithScore]:
raise NotImplementedError(
"This query engine does not support retrieve, use query directly"
)
def synthesize(
self,
query_bundle: QueryBundle,
nodes: List[NodeWithScore],
additional_source_nodes: Optional[Sequence[NodeWithScore]] = None,
) -> RESPONSE_TYPE:
raise NotImplementedError(
"This query engine does not support synthesize, use query directly"
)
async def asynthesize(
self,
query_bundle: QueryBundle,
nodes: List[NodeWithScore],
additional_source_nodes: Optional[Sequence[NodeWithScore]] = None,
) -> RESPONSE_TYPE:
raise NotImplementedError(
"This query engine does not support asynthesize, use aquery directly"
)
@abstractmethod
def _query(self, query_bundle: QueryBundle) -> RESPONSE_TYPE:
pass
@abstractmethod
async def _aquery(self, query_bundle: QueryBundle) -> RESPONSE_TYPE:
pass
def _as_query_component(self, **kwargs: Any) -> QueryComponent:
"""返回一个查询组件。"""
return QueryEngineComponent(query_engine=self)