问题验证
- 我已在文档和discord中寻找答案。
问题
我正在为文本实现一个句子分割器。如果文本包含超链接,那么在IngestionPipeline内运行分割器与在IngestionPipeline外运行分割器的行为如下:
file_splitted = splitter.split_text_metadata_aware(text, node.get_metadata_str())
超链接显示的方式不同,但文本的其余部分没有变化,我对这种行为感到困惑。
我正在为文本实现一个句子分割器。如果文本包含超链接,那么在IngestionPipeline内运行分割器与在IngestionPipeline外运行分割器的行为如下:
file_splitted = splitter.split_text_metadata_aware(text, node.get_metadata_str())
超链接显示的方式不同,但文本的其余部分没有变化,我对这种行为感到困惑。
4条答案
按热度按时间1u4esq0p1#
嘿,@truski22!👋 我在您等待人工维护者时为您提供任何错误、问题或贡献方面的帮助。让我们一起解决这些错误吧!
为了在使用
splitter.split_text_metadata_aware
方法时确保在IngestionPipeline
内部和外部都能一致处理超链接,您需要自定义分词器或分割函数以识别和保留超链接。以下是如何修改
SentenceSplitter
类以一致处理超链接的示例:在此示例中,
custom_tokenizer
函数使用正则表达式识别超链接并将其视为单个标记。然后,CustomSentenceSplitter
类使用此分词器确保在分割过程中不会破坏超链接。这种方法确保了IngestionPipeline
1(https://github.com/run-llama/llama_index/blob/main/llama-index-legacy/llama_index/legacy/node_parser/text/token.py)2(https://github.com/run-llama/llama_index/blob/main/llama-index-core/llama_index/core/node_parser/text/token.py)3(https://github.com/run-llama/llama_index/blob/main/llama-index-legacy/llama_index/legacy/node_parser/text/sentence.py)4(https://github.com/run-llama/llama_index/blob/main/llama-index-core/llama_index/core/node_parser/text/sentence.py) 内外对超链接的一致处理。ct2axkht2#
为什么在使用IngestionPipeline时问题不出现?问题出现在我将splitter移出IngestionPipeline时。
wmomyfyw3#
问题出现在
IngestionPipeline
之外的超链接显示可能是由于IngestionPipeline
内部应用的转换导致的。具体来说,SentenceSplitter
和HuggingFaceEmbedding
转换可能会以影响超链接处理的方式更改文本。为了确保
IngestionPipeline
内外的超链接处理行为一致,您需要确保IngestionPipeline
内部应用的转换不会更改超链接。您可以通过修改或添加保留超链接的自定义转换来实现这一点。以下是如何修改
IngestionPipeline
以包含保留超链接的自定义转换的示例:通过使用自定义
HyperlinkPreservingSplitter
,您可以确保在摄取过程中保留超链接,从而使IngestionPipeline
1(https://github.com/run-llama/llama_index/blob/main/llama-index-packs/llama-index-packs-rag-cli-local/llama_index/packs/rag_cli_local/base.py)2(https://github.com/run-llama/llama_index/blob/main/llama-index-core/tests/ingestion/test_pipeline.py) 内外的行为保持一致。mu0hgdu04#
这并不能解决我的问题。例如,原始文本是:
经过分割器处理后,这两行变成了:
文本被转换成了这样:
有一些差异是我不明白为什么会出现的。其他例子:
原始文本:
经过分割器处理后: