IniziaInizia gratis

Upsert dei transcript di YouTube

Nei seguenti esercizi creerai un chatbot che può rispondere a domande sui video di YouTube inserendo i transcript dei video e metadati aggiuntivi nel tuo indice 'pinecone-datacamp'.

Per iniziare, preparerai i dati dal file youtube_rag_data.csv e farai l'upsert dei vettori con tutti i relativi metadati nell'indice 'pinecone-datacamp'. I dati sono forniti nel DataFrame youtube_df.

Ecco un esempio di transcript dal 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

Questo esercizio fa parte del corso

Database vettoriali per Embeddings con Pinecone

Visualizza il corso

Istruzioni dell'esercizio

  • Inizializza il client Pinecone con la tua chiave API (il client OpenAI è disponibile come client).
  • Estrai i metadati 'id', 'text', 'title', 'url' e 'published' da ogni row.
  • Codifica i texts usando 'text-embedding-3-small' di OpenAI.
  • Esegui l'upsert dei vettori e dei metadati in uno spazio dei nomi chiamato 'youtube_rag_dataset'.

Esercizio pratico interattivo

Prova a risolvere questo esercizio completando il codice di esempio.

# 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())
Modifica ed esegui il codice