レコメンデーションシステム

OpenAI API で学ぶ埋め込み入門

Emmanuel Pire

Senior Software Engineer, DataCamp

埋め込みを用いたレコメンデーションシステム

  • セマンティック検索に似ている!

プロセス:

  1. 推薦候補のアイテムと基準のデータポイントを埋め込む

One blue data point and several red data points in the vector space.

OpenAI API で学ぶ埋め込み入門

埋め込みを用いたレコメンデーションシステム

  • セマンティック検索に似ている! プロセス:

  • 推薦候補のアイテムと基準のデータポイントを埋め込む

  • コサイン距離を計算する

One blue data point and several red data points in the vector space. There's a line between each red point and the blue point to denote the cosine distance.

OpenAI API で学ぶ埋め込み入門

埋め込みを用いたレコメンデーションシステム

  • セマンティック検索に似ている! プロセス:

  • 推薦候補のアイテムと基準のデータポイントを埋め込む

  • コサイン距離を計算する

  • 最も近い項目を推薦する

The three closest red points have been highlighted.

OpenAI API で学ぶ埋め込み入門

例: おすすめ記事

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"]}
OpenAI API で学ぶ埋め込み入門

情報をまとめる

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
OpenAI API で学ぶ埋め込み入門

埋め込みを作成する

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)
OpenAI API で学ぶ埋め込み入門

最も類似した記事を見つける

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'])
OpenAI API で学ぶ埋め込み入門

最も類似した記事を見つける

Tech Giant Buys 49% Stake In AI Startup
Tech Company Launches Innovative Product to Improve Online Accessibility
Scientists Make Breakthrough Discovery in Renewable Energy
OpenAI API で学ぶ埋め込み入門

ユーザー履歴の追加

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"]}
]
OpenAI API で学ぶ埋め込み入門

複数のデータポイントに基づくレコメンデーション

A plot showing embedded articles, where the articles in the user's history are shown in blue and articles they haven't seen are shown in red.

OpenAI API で学ぶ埋め込み入門

複数のデータポイントに基づくレコメンデーション

プロセス:

  • 複数のベクトルを 平均 して 1 つにまとめる
  • コサイン距離を計算する

A point computed from the mean of the two vectors has been added between the two points.

OpenAI API で学ぶ埋め込み入門

複数のデータポイントに基づくレコメンデーション

プロセス:

  • 複数のベクトルを平均して 1 つにまとめる
  • コサイン距離を計算する
  • 最も近いベクトルを推薦する

The nearest red point has been highlighted for recommendation.

OpenAI API で学ぶ埋め込み入門

複数のデータポイントに基づくレコメンデーション

プロセス:

  • 複数のベクトルを平均して 1 つにまとめる
  • コサイン距離を計算する
  • 最も近いベクトルを推薦する

The nearest point has now been colored blue to emphasize that the code should exclude articles that the user has already read.

OpenAI API で学ぶ埋め込み入門

複数のデータポイントに基づくレコメンデーション

プロセス:

  • 複数のベクトルを平均して 1 つにまとめる
  • コサイン距離を計算する
  • 最も近いベクトルを推薦する
    • 未閲覧のもの

The nearest red point, further away this time, has been highlighted.

OpenAI API で学ぶ埋め込み入門

複数のデータポイントに基づくレコメンデーション

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)
OpenAI API で学ぶ埋め込み入門

複数のデータポイントに基づくレコメンデーション

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
OpenAI API で学ぶ埋め込み入門

練習しましょう!

OpenAI API で学ぶ埋め込み入門

Preparing Video For Download...