在集合中更新与删除条目
把文档存入向量数据库后,您仍然可以对集合进行增补或更新已有条目。
在本练习中,已为您提供了存放在 new_data 中的两条新的 Netflix 标题:
[{"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 的 Embeddings 入门
练习说明
- 从
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)