在人工智能领域,大模型有时会产生一个被称为“幻觉问题”的现象。在对话过程中,大模型可能会答非所问,生成与用户输入不符、与先前生成的内容矛盾或与已知世界知识不符的内容。这就是所谓的“幻觉问题”。
解决幻觉问题的一个有效办法就是数据投喂,即将知识库文件传递给大模型,让模型从知识库中寻找答案。然而,由于大模型的上下文长度有限,一次性投喂的内容过多会导致超出限制。为了解决这个问题,聪明的开发者们设计了检索增强生成(RAG)架构。
在处理长文本时,一个常见的问题是如何进行有效的切割。今天我们就来讨论一下这个问题。
最简单的办法就是固定长度切割,但这可能会导致一个完整的句子被拆分。固定长度+两端扩展在上下文比较连贯的场景下效果较好。但是,这两种方法都有各自的问题。
针对这些问题,我提出了一个新的解决方案:动态切割算法。这个算法设定一个长度范围,在这个范围内寻找代表句子结束的标点符号,并以此作为切割点。如果需要两端扩展,就向前寻找切割点进行切割。
下面是这个算法的具体实现代码:
def split_text(text, min_length=300, max_length=500, overlap_length=0, punctuation_marks="。?!.?!"):
"""
Splits the text into segments with specified overlap, ensuring that the overlap
respects sentence-ending punctuation.
:param text: The text to be split.
:param min_length: Minimum length of each segment.
:param max_length: Maximum length of each segment.
:param overlap_length: Length of overlap between consecutive segments (default 5).
:param punctuation_marks: String containing punctuation marks to consider for splitting (default "。?!.?!").
:return: List of text segments.
"""
segments = []
start_index = 0
while start_index < len(text):
end_index = min(start_index + max_length, len(text))
# Search for the nearest punctuation mark before the end index
split_index = -1
for i in range(end_index - 1, start_index + min_length - 1, -1):
if text[i] in punctuation_marks:
split_index = i
break
# If no punctuation mark is found, use the end index
if split_index == -1:
split_index = end_index - 1
# Ensure the segment is at least the minimum length
if split_index - start_index < min_length:
split_index = start_index + min_length - 1
segments.append(text[start_index:split_index + 1])
# Determine the start of the next segment with overlap
next_start_index = split_index + 1 - overlap_length
next_start_index = max(next_start_index, 0)
# Adjust next_start_index to avoid splitting at the middle of a sentence
if next_start_index < len(text):
for i in range(next_start_index, split_index):
if text[i] in punctuation_marks:
next_start_index = i + 1
break
start_index = next_start_index
return segments
这段代码的主要思想是用标点符号作为切割点来进行文本切割。这样可以在保证文本分段连贯的同时,避免出现将完整的句子拆分成两部分的情况。
上面的代码使用GPT4生成,咒语如下:
你是一个高级python开发工程,精通算法设计和python语法规范,请帮我设计一个文本切割算法,请先列出代表句子结束的标点符号,然后用标点符号作为切割点进行切割,文本长度范围在传入的最小和最大范围之间,请给出具体的代码实现
我们可以通过以下代码来测试上述算法的效果:
from chunk import split_text
from down_mp import get_detail_info
example_text = get_detail_info('https://mp.weixin.qq.com/s/kaNDY1CMVbotf8sErw597A')['content']
# Example usage with the same text (includes both Chinese and English punctuation)
segments_v2 = split_text(example_text,min_length=400, max_length=600, overlap_length=0)
for segment in segments_v2:
print('======= '+segment)
print()
测试结果如下:
从结果可以看出,这种动态切割的方法可以有效地处理长文本,使得切割后的文本段落在长度和连贯性上都保持得较好。
长文本切割是一个复杂而又重要的问题,未来可能会有更多的切割算法出现。而对于大模型的幻觉问题,我们也需要不断寻找更好的解决方案。希望这篇文章能对你有所启发,让我们一起期待更多的创新和突破。
如果你有更好的建议欢评论区留言探讨。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有