始める無料で始める

コレクションにデータを追加する

Netflix の映画とTV番組をコレクションに追加していきましょう。すでに idsdocuments に、ドキュメントIDとテキストのリストが用意されています。これは 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: {____}")
コードを編集して実行