Upsert YouTube 逐字稿
在接下來的練習中,你將建立一個聊天機器人,能透過匯入影片逐字稿與額外中繼資料,來回答關於 YouTube 影片的問題,並寫入你的 'pinecone-datacamp' 索引。
首先,請從 youtube_rag_data.csv 準備資料,並將含有所有中繼資料的向量 upsert 到 '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 應用
練習說明
- 使用你的 API 金鑰初始化 Pinecone 用戶端(OpenAI 用戶端已可透過
client使用)。 - 從每個
row萃取'id'、'text'、'title'、'url'與'published'中繼資料。 - 使用 OpenAI 的
'text-embedding-3-small'對texts進行編碼。 - 將向量與中繼資料 upsert 到名為
'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())