वेक्टर स्पेस की जाँच

OpenAI API के साथ Embeddings परिचय

Emmanuel Pire

Senior Software Engineer, DataCamp

उदाहरण: हेडलाइन को एम्बेड करना

articles = [
    {"headline": "Economic Growth Continues Amid Global Uncertainty", "topic": "Business"},
    {"headline": "Interest rates fall to historic lows", "topic": "Business"},
    {"headline": "Scientists Make Breakthrough Discovery in Renewable Energy", "topic": "Science"},
    {"headline": "India Successfully Lands Near Moon's South Pole", "topic": "Science"},
    {"headline": "New Particle Discovered at CERN", "topic": "Science"},
    {"headline": "Tech Company Launches Innovative Product to Improve Online Accessibility", "topic": "Tech"},
    {"headline": "Tech Giant Buys 49% Stake In AI Startup", "topic": "Tech"},
    {"headline": "New Social Media Platform Has Everyone Talking!", "topic": "Tech"},
    {"headline": "The Blues get promoted on the final day of the season!", "topic": "Sport"},
    {"headline": "1.5 Billion Tune-in to the World Cup Final", "topic": "Sport"}
]
OpenAI API के साथ Embeddings परिचय

उदाहरण: हेडलाइन को एम्बेड करना

हेडलाइन और उनके एम्बेडिंग वाले डिक्शनरी की सूची।

OpenAI API के साथ Embeddings परिचय

एक साथ कई इनपुट एम्बेड करना

headline_text = [article['headline'] for article in articles]
headline_text
["Economic Growth Continues Amid Global Uncertainty",
 ...,
 "1.5 Billion Tune-in to the World Cup Final"]
response = client.embeddings.create(
  model="text-embedding-3-small",
  input=headline_text
)
response_dict = response.model_dump()
  • बैचिंग कई API कॉल्स की तुलना में ज्यादा कुशल है
OpenAI API के साथ Embeddings परिचय
[...]

'data': [
    {
      "embedding": [-0.017142612487077713, ..., -0.0012911480152979493],
      "index": 0,
      "object": "embedding"
    },
    {
      "embedding": [-0.032995883375406265, ..., -0.0028605300467461348],
      "index": 1,
      "object": "embedding"
    },
    ...
  ]

[...]
OpenAI API के साथ Embeddings परिचय

एक साथ कई इनपुट एम्बेड करना

articles = [
    {"headline": "Economic Growth Continues Amid Global Uncertainty", "topic": "Business"},
     ...
]
for i, article in enumerate(articles):

article['embedding'] = response_dict['data'][i]['embedding']
print(articles[:2])
[{'headline': 'Economic Growth Continues Amid Global Uncertainty',
  'topic': 'Business',
  'embedding': [-0.017142612487077713, ..., -0.0012911480152979493]}
 {'headline': 'Interest rates fall to historic lows',
  'topic': 'Business',
  'embedding': [-0.032995883375406265, ..., -0.0028605300467461348]}]
OpenAI API के साथ Embeddings परिचय

एम्बेडिंग्स वेक्टर की लंबाई कितनी है?

  • "Economic Growth Continues Amid Global Uncertainty"
len(articles[0]['embedding'])
1536
  • "Tech Company Launches Innovative Product to Improve Accessibility"
len(articles[5]['embedding'])
1536
  • हमेशा 1536 नंबर लौटते हैं!
OpenAI API के साथ Embeddings परिचय

डायमेंशनलिटी रिडक्शन और t-SNE

 

  • डायमेंशन्स घटाने की कई तकनीकें
  • t-SNE (t-distributed Stochastic Neighbor Embedding)
1 https://www.datacamp.com/tutorial/introduction-t-sne
OpenAI API के साथ Embeddings परिचय

t-SNE लागू करना

from sklearn.manifold import TSNE
import numpy as np


embeddings = [article['embedding'] for article in articles]
tsne = TSNE(n_components=2, perplexity=5)
embeddings_2d = tsne.fit_transform(np.array(embeddings))
  • n_components: बनने वाले डायमेंशन्स की संख्या
  • perplexity: एल्गोरिदम के लिए, डेटा पॉइंट्स से कम होना चाहिए
  • इससे information loss होगा
1 https://www.datacamp.com/tutorial/introduction-t-sne
OpenAI API के साथ Embeddings परिचय

एम्बेडिंग्स को विज़ुअलाइज़ करना

import matplotlib.pyplot as plt

plt.scatter(embeddings_2d[:, 0], embeddings_2d[:, 1])

topics = [article['topic'] for article in articles] for i, topic in enumerate(topics): plt.annotate(topic, (embeddings_2d[i, 0], embeddings_2d[i, 1])) plt.show()
OpenAI API के साथ Embeddings परिचय

एम्बेडिंग्स को विज़ुअलाइज़ करना

 

  • मिलते-जुलते आर्टिकल साथ क्लस्टर होते हैं!
  • मॉडल ने semantic अर्थ को कैप्चर किया

 

  • आगे: Similarity निकालना

 

2D वेक्टर स्पेस का प्लॉट, जो दिखाता है कि एक जैसे सेंटिमेंट और टॉपिक वाली रिव्यूज़ वेक्टर स्पेस में पास आती हैं।

OpenAI API के साथ Embeddings परिचय

अभ्यास करते हैं!

OpenAI API के साथ Embeddings परिचय

Preparing Video For Download...