向集合添加数据
现在把这些 Netflix 电影和剧集添加到您的集合中吧!我们已经为您准备好了一份文档 ID 列表和文本列表,分别保存在 ids 和 documents 中。它们是通过以下代码从 netflix_titles.csv 提取的:
ids = []
documents = []
with open('netflix_titles.csv') as csvfile:
reader = csv.DictReader(csvfile)
for i, row in enumerate(reader):
ids.append(row['show_id'])
text = f"Title: {row['title']} ({row['type']})\nDescription: {row['description']}\nCategories: {row['listed_in']}"
documents.append(text)
以下是将被嵌入的信息示例,即 documents 中的第一条文档:
Title: Dick Johnson Is Dead (Movie)
Description: As her father nears the end of his life, filmmaker Kirsten Johnson stages his death in inventive and comical ways to help them both face the inevitable.
Categories: Documentaries
所有必要的函数和包都已导入,并且已创建持久化客户端并赋值给 client。
本练习是课程的一部分
使用 OpenAI API 的 Embeddings 入门
练习说明
- 重新创建您的
netflix_titles集合。 - 将文档及其 ID 添加到该集合。
- 打印
collection中的文档数量以及前 10 个条目。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Recreate the netflix_titles collection
collection = client.____(
name="netflix_titles",
embedding_function=OpenAIEmbeddingFunction(model_name="text-embedding-3-small", api_key="")
)
# Add the documents and IDs to the collection
____
# Print the collection size and first ten items
print(f"No. of documents: {____}")
print(f"First ten documents: {____}")