Aan de slagGa gratis aan de slag

Updating and deleting items from a collection

Just because the documents have been stored away in a vector database, that doesn't mean that you can't make changes to add to the collection or update existing items.

In this exercise, you've been provided with two new Netflix titles stored in 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'}]

You'll either add or update these IDs in the database depending on whether they're already present in the collection.

Deze oefening maakt deel uit van de cursus

Introduction to Embeddings with the OpenAI API

Cursus bekijken

Oefeninstructies

  • Extract the IDs and documents from new_data, and use a single method to update them in the netflix_titles collection if they already exist and add them if they don't.
  • After you've added/updated the items, delete the item with ID 's95'.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

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)
Code bewerken en uitvoeren