YouTubeの文字起こしをアップサートする
次の演習では、動画の文字起こしと追加のメタデータを 'pinecone-datacamp' インデックスに取り込み、YouTube動画に関する質問に答えられるチャットボットを作成します。
まず、youtube_rag_data.csv ファイルのデータを準備し、すべてのメタデータ付きでベクトルを 'pinecone-datacamp' インデックスにアップサートします。データは DataFrame youtube_df に用意されています。
以下は、youtube_df DataFrame に含まれるサンプルの文字起こしです:
id:
35Pdoyi6ZoQ-t0.0
title:
Training and Testing an Italian BERT - Transformers From Scratch #4
text:
Hi, welcome to the video. So this is the fourth video in a Transformers from Scratch
mini series. So if you haven't been following along, we've essentially covered what
you can see on the screen. So we got some data. We built a tokenizer with it...
url:
https://youtu.be/35Pdoyi6ZoQ
published:
01-01-2024
この演習はコースの一部です
Pineconeを使ったAIアプリケーション開発
演習の手順
- OpenAIのAPIキーでPineconeクライアントを初期化します(OpenAIクライアントは
clientとして利用できます)。 - 各
rowから'id'、'text'、'title'、'url'、'published'のメタデータを抽出します。 - OpenAIの
'text-embedding-3-small'を使ってtextsをエンコードします。 - ベクトルとメタデータを、
'youtube_rag_dataset'という名前の名前空間にアップサートします。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# Initialize the Pinecone client
pc = Pinecone(api_key="____")
index = pc.Index('pinecone-datacamp')
batch_limit = 100
for batch in np.array_split(youtube_df, len(youtube_df) / batch_limit):
# Extract the metadata from each row
metadatas = [{
"text_id": row['____'],
"text": row['____'],
"title": row['____'],
"url": row['____'],
"published": row['____']} for _, row in batch.iterrows()]
texts = batch['text'].tolist()
ids = [str(uuid4()) for _ in range(len(texts))]
# Encode texts using OpenAI
response = ____(input=____, model="text-embedding-3-small")
embeds = [np.array(x.embedding) for x in response.data]
# Upsert vectors to the correct namespace
____(vectors=____(ids, embeds, metadatas), namespace='____')
print(index.describe_index_stats())