Cập nhật và xóa mục khỏi một collection
Chỉ vì tài liệu đã được lưu trong một vector database không có nghĩa là bạn không thể thay đổi để bổ sung vào collection hoặc cập nhật các mục hiện có.
Trong bài này, bạn được cung cấp hai tựa phim Netflix mới lưu trong 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'}]
Bạn sẽ thêm mới hoặc cập nhật các ID này trong cơ sở dữ liệu tùy theo việc chúng đã có trong collection hay chưa.
Bài tập này là một phần của khóa học
Nhập môn Embeddings với OpenAI API
Hướng dẫn bài tập
- Trích xuất các ID và document từ
new_data, và dùng một phương thức duy nhất để cập nhật chúng trong collectionnetflix_titlesnếu đã tồn tại và thêm mới nếu chưa có. - Sau khi đã thêm/cập nhật các mục, hãy xóa mục có ID
's95'.
Bài tập tương tác thực hành trực tiếp
Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.
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)