[问题反馈] 关于LlamaIndex 查询路由介绍的修改

Author: tnlfCreated Sep 3, 2026Updated Sep 3, 2026
Labelsbug

提交前检查

  • 我已在 Issues 中搜索,未找到相同问题
  • 我已使用仓库最新版(或 main 分支)进行复现

问题类型

文档描述错误/不清晰

位置(章节/文件路径)

docs/chapter4/14_query_rewriting.md

问题描述

文中对于LlamaIndex 路由查询的介绍写到

嵌入相似性路由:LlamaIndex 没有提供直接基于向量相似度计算的独立路由组件。它的“语义路由”是融合在基于 LLM 的意图识别中的——即让 LLM 理解每个 Tool 描述的 语义,并据此做出决策。这种方式更灵活,能够处理更复杂的路由逻辑,而不仅仅是文本相似度匹配。

但在查询LlamaIndex源码时,发现LlamaIndex存在EmbeddingSingleSelector组件能够实现嵌入相似性路由,其中查询过程部分代码为:

python
def _select(
        self, choices: Sequence[ToolMetadata], query: QueryBundle
    ) -> SelectorResult:
        query_embedding = self._embed_model.get_query_embedding(query.query_str)
        text_embeddings = [
            self._embed_model.get_text_embedding(choice.description)
            for choice in choices
        ]

        top_similarities, top_ids = get_top_k_embeddings(
            query_embedding,
            text_embeddings,
            similarity_top_k=1,
            embedding_ids=list(range(len(choices))),
        )
        # get top choice
        top_selection_reason = f"Top similarity match: {top_similarities[0]:.2f}, {choices[top_ids[0]].name}"
        top_selection = SingleSelection(index=top_ids[0], reason=top_selection_reason)

        # parse output
        return SelectorResult(selections=[top_selection])

相关源码地址:https://github.com/run-llama/llama_index/blob/main/llama-index-core/llama_index/core/selectors/embedding_selectors.py

另外,感谢作者的贡献,祝作者工作顺利

复现步骤

No response

最小可复现代码/命令

bash

环境信息

No response

日志/报错信息与截图

bash

Source: datawhalechina/all-in-rag