การแบ่งข้อความตามความหมาย
กลยุทธ์การแบ่งข้อความทั้งหมดที่ใช้มาจนถึงตอนนี้มีข้อจำกัดร่วมกัน คือการแบ่งไม่ได้คำนึงถึงบริบทของข้อความโดยรอบ ทำให้บริบทอาจสูญหายได้ระหว่างการแบ่ง
ในแบบฝึกหัดนี้ จะได้สร้างและนำ semantic text splitter มาใช้งาน ซึ่งเป็นวิธีเชิงทดลองที่ทันสมัยสำหรับการแบ่งข้อความตามความหมาย เมื่อ splitter ตรวจพบว่าความหมายของข้อความเปลี่ยนแปลงเกินค่า threshold ที่กำหนด ก็จะทำการแบ่งข้อความ
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Retrieval Augmented Generation (RAG) ด้วย LangChain
คำแนะนำการฝึกหัด
- สร้าง instance ของโมเดล embedding
'text-embedding-3-small'จาก OpenAI - สร้าง semantic text splitter ที่ใช้ vector gradients ในการวัดความคล้ายคลึงเชิงความหมาย และใช้
0.8เป็นค่า threshold สำหรับการแบ่งข้อความ - แบ่ง
documentโดยใช้ semantic splitter
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# 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])