ระบบแนะนำ

การสร้าง Embeddings ด้วย OpenAI API

Emmanuel Pire

Senior Software Engineer, DataCamp

ระบบแนะนำด้วย embedding

 

  • คล้ายกับการค้นหาเชิงความหมายมาก!

 

ขั้นตอน:

  1. แปลงตัวเลือกที่จะแนะนำและจุดข้อมูลเป็น embedding

จุดข้อมูลสีน้ำเงินหนึ่งจุดและจุดข้อมูลสีแดงหลายจุดในเวกเตอร์สเปซ

การสร้าง Embeddings ด้วย OpenAI API

ระบบแนะนำด้วย embedding

 

  • คล้ายกับการค้นหาเชิงความหมายมาก!

 

ขั้นตอน:

  1. แปลงตัวเลือกที่จะแนะนำและจุดข้อมูลเป็น embedding
  2. คำนวณ cosine distance

จุดข้อมูลสีน้ำเงินหนึ่งจุดและจุดข้อมูลสีแดงหลายจุดในเวกเตอร์สเปซ โดยมีเส้นเชื่อมระหว่างจุดสีแดงแต่ละจุดกับจุดสีน้ำเงินเพื่อแสดง cosine distance

การสร้าง Embeddings ด้วย OpenAI API

ระบบแนะนำด้วย embedding

 

  • คล้ายกับการค้นหาเชิงความหมายมาก!

 

ขั้นตอน:

  1. แปลงตัวเลือกที่จะแนะนำและจุดข้อมูลเป็น embedding
  2. คำนวณ cosine distance
  3. แนะนำรายการที่ใกล้เคียงที่สุด

จุดสีแดงสามจุดที่ใกล้ที่สุดถูกไฮไลต์ไว้

การสร้าง Embeddings ด้วย 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"]}
การสร้าง Embeddings ด้วย 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
การสร้าง Embeddings ด้วย OpenAI API

การสร้าง 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)
การสร้าง Embeddings ด้วย 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'])
การสร้าง Embeddings ด้วย 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
การสร้าง Embeddings ด้วย 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"]}
]
การสร้าง Embeddings ด้วย OpenAI API

การแนะนำจากหลายจุดข้อมูล

กราฟแสดง embedding ของบทความ โดยบทความที่อยู่ในประวัติผู้ใช้แสดงเป็นสีน้ำเงิน และบทความที่ยังไม่ได้อ่านแสดงเป็นสีแดง

การสร้าง Embeddings ด้วย OpenAI API

การแนะนำจากหลายจุดข้อมูล

 

ขั้นตอน:

  • รวมหลายเวกเตอร์เป็นหนึ่งด้วยการหาค่าเฉลี่ย
  • คำนวณ cosine distance

มีการเพิ่มจุดที่คำนวณจากค่าเฉลี่ยของสองเวกเตอร์ระหว่างสองจุดนั้น

การสร้าง Embeddings ด้วย OpenAI API

การแนะนำจากหลายจุดข้อมูล

 

ขั้นตอน:

  • รวมหลายเวกเตอร์เป็นหนึ่งด้วยการหาค่าเฉลี่ย
  • คำนวณ cosine distance
  • แนะนำเวกเตอร์ที่ใกล้ที่สุด

จุดสีแดงที่ใกล้ที่สุดถูกไฮไลต์เพื่อแนะนำ

การสร้าง Embeddings ด้วย OpenAI API

การแนะนำจากหลายจุดข้อมูล

 

ขั้นตอน:

  • รวมหลายเวกเตอร์เป็นหนึ่งด้วยการหาค่าเฉลี่ย
  • คำนวณ cosine distance
  • แนะนำเวกเตอร์ที่ใกล้ที่สุด

จุดที่ใกล้ที่สุดถูกเปลี่ยนเป็นสีน้ำเงินเพื่อแสดงว่าโค้ดควรยกเว้นบทความที่ผู้ใช้อ่านไปแล้ว

การสร้าง Embeddings ด้วย OpenAI API

การแนะนำจากหลายจุดข้อมูล

 

ขั้นตอน:

  • รวมหลายเวกเตอร์เป็นหนึ่งด้วยการหาค่าเฉลี่ย
  • คำนวณ cosine distance
  • แนะนำเวกเตอร์ที่ใกล้ที่สุด
    • ตรวจสอบว่ายังไม่ได้อ่าน

จุดสีแดงที่ใกล้ที่สุด ซึ่งอยู่ห่างออกไปกว่าเดิม ถูกไฮไลต์ไว้

การสร้าง Embeddings ด้วย 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)
การสร้าง Embeddings ด้วย 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
การสร้าง Embeddings ด้วย OpenAI API

มาฝึกกันเถอะ!

การสร้าง Embeddings ด้วย OpenAI API

Preparing Video For Download...