การเพิ่มข้อมูลลงใน collection
ถึงเวลาเพิ่มข้อมูลภาพยนตร์และรายการทีวีของ Netflix ลงใน collection แล้ว! มีการเตรียม list ของ document ID และข้อความไว้ให้แล้ว โดยเก็บอยู่ในตัวแปร ids และ documents ตามลำดับ ซึ่งดึงมาจากไฟล์ netflix_titles.csv ด้วยโค้ดดังนี้:
ids = []
documents = []
with open('netflix_titles.csv') as csvfile:
reader = csv.DictReader(csvfile)
for i, row in enumerate(reader):
ids.append(row['show_id'])
text = f"Title: {row['title']} ({row['type']})\nDescription: {row['description']}\nCategories: {row['listed_in']}"
documents.append(text)
เพื่อให้เห็นภาพว่าข้อมูลที่จะถูก embed มีหน้าตาอย่างไร ต่อไปนี้คือ document แรกจาก documents:
Title: Dick Johnson Is Dead (Movie)
Description: As her father nears the end of his life, filmmaker Kirsten Johnson stages his death in inventive and comical ways to help them both face the inevitable.
Categories: Documentaries
ฟังก์ชันและแพ็กเกจที่จำเป็นทั้งหมดได้ถูก import ไว้แล้ว และมีการสร้าง persistent client พร้อมกำหนดให้กับตัวแปร client เรียบร้อยแล้ว
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การสร้าง Embeddings ด้วย OpenAI API
คำแนะนำการฝึกหัด
- สร้าง collection ชื่อ
netflix_titlesขึ้นใหม่ - เพิ่ม documents และ ID ของแต่ละรายการลงใน collection
- แสดงจำนวน documents ใน
collectionและ 10 รายการแรก
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Recreate the netflix_titles collection
collection = client.____(
name="netflix_titles",
embedding_function=OpenAIEmbeddingFunction(model_name="text-embedding-3-small", api_key="")
)
# Add the documents and IDs to the collection
____
# Print the collection size and first ten items
print(f"No. of documents: {____}")
print(f"First ten documents: {____}")