コレクションへのデータ追加
それでは、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 で学ぶ埋め込み入門
演習の手順
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: {____}")