基于语义的切分
到目前为止,您使用的所有切分策略都有同一个缺点:切分不考虑周围文本的上下文,因此在切分过程中很容易丢失上下文。
在本练习中,您将创建并应用一个语义文本切分器。这是一种前沿的实验性方法,可根据语义含义来切分文本。当切分器检测到文本含义偏离超过某个阈值时,就会执行切分。
本练习是课程的一部分
使用 LangChain 的 Retrieval Augmented Generation (RAG)
练习说明
- 实例化来自 OpenAI 的
'text-embedding-3-small'嵌入向量模型。 - 创建一个语义文本切分器,使用向量梯度来判定语义相似度,并将切分阈值设为
0.8。 - 使用该语义切分器对
document进行切分。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Instantiate an OpenAI embeddings model
embedding_model = ____(api_key="", model='____')
# Create the semantic text splitter with desired parameters
semantic_splitter = ____(
embeddings=____, breakpoint_threshold_type="____", breakpoint_threshold_amount=____
)
# Split the document
chunks = ____
print(chunks[0])