在集合中更新與刪除項目
把文件存進向量資料庫後,並不代表你就不能再變更內容,像是擴充集合或更新既有項目。
在這個練習中,已為你準備了兩個新的 Netflix 片名,儲存在 new_data:
[{"id": "s1001", "document": "Title: Cats & Dogs (Movie)\nDescription: A look at the top-secret, high-tech espionage war going on between cats and dogs, of which their human owners are blissfully unaware."},
{"id": "s6884", "document": 'Title: Goosebumps 2: Haunted Halloween (Movie)\nDescription: Three teens spend their Halloween trying to stop a magical book, which brings characters from the "Goosebumps" novels to life.\nCategories: Children & Family Movies, Comedies'}]
你需要視這些 ID 是否已存在於集合中,來選擇新增或更新它們在資料庫裡的內容。
本練習屬於課程
Introduction to Embeddings with the OpenAI API
練習說明
- 從
new_data擷取 ID 與文件,使用單一方法在netflix_titles集合中進行更新(若已存在)或新增(若不存在)。 - 在新增/更新完成後,刪除 ID 為
's95'的項目。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
collection = client.get_collection(
name="netflix_titles",
embedding_function=OpenAIEmbeddingFunction(model_name="text-embedding-3-small", api_key="")
)
# Update or add the new documents
____(
ids=____,
documents=____
)
# Delete the item with ID "s95"
____
result = collection.query(
query_texts=["films about dogs"],
n_results=3
)
print(result)