コレクションのアイテムの更新・削除
ドキュメントをベクターデータベースに保存した後でも、コレクションへの追加や既存アイテムの更新は行えます。
この演習では、 2件の新しい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がコレクションにすでに存在するかどうかに応じて、データベースへの追加または更新を行いましょう。
この演習はコースの一部です
OpenAI API で学ぶ埋め込み入門
演習の手順
new_dataからIDとドキュメントを取り出し、1つのメソッドを使って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)