开始使用免费开始使用

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())
编辑并运行代码