Upsert transcript YouTube
Trong các bài tập sau, bạn sẽ tạo một chatbot có thể trả lời câu hỏi về video YouTube bằng cách nạp transcript và metadata bổ sung của video vào chỉ mục 'pinecone-datacamp'.
Đầu tiên, bạn sẽ chuẩn bị dữ liệu từ tệp youtube_rag_data.csv và upsert các vector cùng toàn bộ metadata của chúng vào chỉ mục 'pinecone-datacamp'. Dữ liệu được cung cấp trong DataFrame youtube_df.
Dưới đây là một ví dụ transcript từ DataFrame youtube_df:
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
Bài tập này là một phần của khóa học
Cơ sở dữ liệu vector cho Embeddings với Pinecone
Hướng dẫn bài tập
- Khởi tạo Pinecone client với API key của bạn (OpenAI client đã có sẵn là
client). - Trích xuất metadata
'id','text','title','url', và'published'từ mỗirow. - Mã hóa
textsbằng'text-embedding-3-small'của OpenAI. - Upsert các vector và metadata vào namespace tên
'youtube_rag_dataset'.
Bài tập tương tác thực hành trực tiếp
Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.
# 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())