將資料加入集合
該把那些 Netflix 電影與影集加入你的集合了!已提供一份文件 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)
以下是會被嵌入向量化的資訊範例,這是 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
所有必要的函式與套件都已匯入,且已建立持久化用戶端並指定給 client。
本練習屬於課程
Introduction to Embeddings with the OpenAI API
練習說明
- 重新建立你的
netflix_titles集合。 - 將文件與其 ID 加入集合。
- 印出
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: {____}")