Hệ thống gợi ý

Nhập môn Embeddings với OpenAI API

Emmanuel Pire

Senior Software Engineer, DataCamp

Hệ thống gợi ý với embedding

 

  • Rất giống tìm kiếm ngữ nghĩa!

 

Quy trình:

  1. Nhúng (embed) các mục gợi ý và điểm dữ liệu

Một điểm dữ liệu màu xanh và nhiều điểm đỏ trong không gian vector.

Nhập môn Embeddings với OpenAI API

Hệ thống gợi ý với embedding

 

  • Rất giống tìm kiếm ngữ nghĩa!

 

Quy trình:

  1. Nhúng (embed) các mục gợi ý và điểm dữ liệu
  2. Tính khoảng cách cosine

Một điểm dữ liệu màu xanh và nhiều điểm đỏ trong không gian vector. Có một đường từ mỗi điểm đỏ tới điểm xanh biểu thị khoảng cách cosine.

Nhập môn Embeddings với OpenAI API

Hệ thống gợi ý với embedding

 

  • Rất giống tìm kiếm ngữ nghĩa!

 

Quy trình:

  1. Nhúng (embed) các mục gợi ý và điểm dữ liệu
  2. Tính khoảng cách cosine
  3. Gợi ý mục gần nhất

Ba điểm đỏ gần nhất được tô nổi bật.

Nhập môn Embeddings với OpenAI API

Ví dụ: Bài viết được gợi ý

articles = [
    {"headline": "Economic Growth Continues Amid Global Uncertainty",
     "topic": "Business",
     "keywords": ["economy", "business", "finance"]},
    ...
    {"headline": "1.5 Billion Tune-in to the World Cup Final",
     "topic": "Sport",
     "keywords": ["soccer", "world cup", "tv"]}
]

current_article = {"headline": "How NVIDIA GPUs Could Decide Who Wins the AI Race", "topic": "Tech", "keywords": ["ai", "business", "computers"]}
Nhập môn Embeddings với OpenAI API

Kết hợp đặc trưng

def create_article_text(article):
  return f"""Headline: {article['headline']}
Topic: {article['topic']}
Keywords: {', '.join(article['keywords'])}"""
article_texts = [create_article_text(article) for article in articles]
current_article_text = create_article_text(current_article)
print(current_article_text)
Headline: How NVIDIA GPUs Could Decide Who Wins the AI Race
Topic: Tech
Keywords: ai, business, computers
Nhập môn Embeddings với OpenAI API

Tạo embedding

def create_embeddings(texts):
  response = openai.Embedding.create(
    model="text-embedding-3-small",
    input=texts
  )
  response_dict = response.model_dump()

  return [data['embedding'] for data in response_dict['data']]
current_article_embeddings = create_embeddings(current_article_text)[0]
article_embeddings = create_embeddings(article_texts)
Nhập môn Embeddings với OpenAI API

Tìm bài viết tương tự nhất

def find_n_closest(query_vector, embeddings, n=3):
  distances = []
  for index, embedding in enumerate(embeddings):
    dist = spatial.distance.cosine(query_vector, embedding)
    distances.append({"distance": dist, "index": index})
  distances_sorted = sorted(distances, key=lambda x: x["distance"])
  return distances_sorted[0:n]
hits = find_n_closest(current_article_embeddings, article_embeddings)

for hit in hits: article = articles[hit['index']] print(article['headline'])
Nhập môn Embeddings với OpenAI API

Tìm bài viết tương tự nhất

Tech Giant Buys 49% Stake In AI Startup
Tech Company Launches Innovative Product to Improve Online Accessibility
Scientists Make Breakthrough Discovery in Renewable Energy
Nhập môn Embeddings với OpenAI API

Thêm lịch sử người dùng

user_history = [
    {"headline": "How NVIDIA GPUs Could Decide Who Wins the AI Race",
     "topic": "Tech",
     "keywords": ["ai", "business", "computers"]},
    {"headline": "Tech Giant Buys 49% Stake In AI Startup",
     "topic": "Tech",
     "keywords": ["business", "AI"]}
]
Nhập môn Embeddings với OpenAI API

Gợi ý từ nhiều điểm dữ liệu

Biểu đồ các bài viết đã được embed; các bài trong lịch sử người dùng màu xanh, bài chưa xem màu đỏ.

Nhập môn Embeddings với OpenAI API

Gợi ý từ nhiều điểm dữ liệu

 

Quy trình:

  • Gộp nhiều vector bằng giá trị trung bình
  • Tính khoảng cách cosine

Một điểm tính từ trung bình của hai vector được thêm giữa hai điểm.

Nhập môn Embeddings với OpenAI API

Gợi ý từ nhiều điểm dữ liệu

 

Quy trình:

  • Gộp nhiều vector bằng giá trị trung bình
  • Tính khoảng cách cosine
  • Gợi ý vector gần nhất

Điểm đỏ gần nhất được tô nổi bật để gợi ý.

Nhập môn Embeddings với OpenAI API

Gợi ý từ nhiều điểm dữ liệu

 

Quy trình:

  • Gộp nhiều vector bằng giá trị trung bình
  • Tính khoảng cách cosine
  • Gợi ý vector gần nhất

Điểm gần nhất chuyển thành màu xanh để nhấn mạnh việc loại trừ bài đã đọc.

Nhập môn Embeddings với OpenAI API

Gợi ý từ nhiều điểm dữ liệu

 

Quy trình:

  • Gộp nhiều vector bằng giá trị trung bình
  • Tính khoảng cách cosine
  • Gợi ý vector gần nhất
    • Đảm bảo chưa đọc

Điểm đỏ gần nhất (xa hơn lần trước) được tô nổi bật.

Nhập môn Embeddings với OpenAI API

Gợi ý từ nhiều điểm dữ liệu

def create_article_text(article):
  return f"""Headline: {article['headline']}
Topic: {article['topic']}
Keywords: {', '.join(article['keywords'])}"""

history_texts = [create_article_text(article) for article in user_history]
history_embeddings = create_embeddings(history_texts)

mean_history_embeddings = np.mean(history_embeddings, axis=0)
articles_filtered = [article for article in articles if article not in user_history]
article_texts = [create_article_text(article) for article in articles_filtered] article_embeddings = create_embeddings(article_texts)
Nhập môn Embeddings với OpenAI API

Gợi ý từ nhiều điểm dữ liệu

hits = find_n_closest(mean_history_embeddings, article_embeddings)

for hit in hits: article = articles_filtered[hit['index']] print(article['headline'])
Tech Company Launches Innovative Product to Improve Online Accessibility
New Social Media Platform Has Everyone Talking!
Scientists Make Breakthrough Discovery in Renewable Energy
Nhập môn Embeddings với OpenAI API

Vamos praticar!

Nhập môn Embeddings với OpenAI API

Preparing Video For Download...