Кілька запитів і фільтрування

Вступ до Embeddings з OpenAI API

Emmanuel Pire

Senior Software Engineer, DataCamp

Рекомендації фільмів на основі кількох точок даних

 

  • Terrifier (id: 's8170')
  • Strawberry Shortcake: Berry Bitty Adventures (id: 's8103')
Вступ до Embeddings з OpenAI API

Кілька текстів запиту

reference_ids = ['s8170', 's8103']


reference_texts = collection.get(ids=reference_ids)["documents"]
result = collection.query( query_texts=reference_texts, n_results=3 )
Вступ до Embeddings з OpenAI API

Результат для кількох текстів запиту

{'ids': [['s8170', 's6939', 's7000'],['s8103', 's2968', 's3085']],
 'embeddings': None,
 'documents': [['Title: Terrifier (Movie)...',
   'Title: Haunters: The Art of the Scare (Movie)...',
   'Title: Horror Story (Movie)...'],
  ["Title: Strawberry Shortcake: Berry Bitty Adventures (TV Show)...",
   "Title: Shopkins (TV Show)...",
   "Title: Rainbow Ruby (TV Show)..."]],
 'metadatas': [[None, None, None], [None, None, None]],
 'distances': [[0.00, 0.25, 0.26], [0.00, 0.25, 0.28]]}
Вступ до Embeddings з OpenAI API

Додавання метаданих

import csv 

ids = []
metadatas = []

with open('netflix_titles.csv') as csvfile:
  reader = csv.DictReader(csvfile)
  for i, row in enumerate(reader):
    ids.append(row['show_id'])
    metadatas.append({
      "type":row['type'],
      "release_year": int(row['release_year'])
    })

 

  • Створіть список словників для метаданих
  • Створіть список ID, щоб додати їх до наявних елементів
Вступ до Embeddings з OpenAI API

Додавання й запити з метаданими

 

collection.update(ids=ids, metadatas=metadatas)
result = collection.query(
    query_texts=reference_texts, 
    n_results=3,
    where={ 
        "type": "Movie"
    }
)
Вступ до Embeddings з OpenAI API

Оператори where

where={
  "type": "Movie"
}

це те саме, що

where={
  "type": {
    "$eq": "Movie"
  }
}

Список операторів:

  • $eq — дорівнює (string, int, float)
  • $ne — не дорівнює (string, int, float)
  • $gt — більше ніж (int, float)
  • $gte — не менше ніж (int, float)
  • $lt — менше ніж (int, float)
  • $lte — не більше ніж (int, float)
Вступ до Embeddings з OpenAI API

Кілька фільтрів where

where={
    "$and": [
        {"type": 
            {"$eq": "Movie"}
        },
        {"release_year": 
             {"$gt": 2020}
        }
    ]
}

 

  • $or: фільтр, якщо виконується принаймні одна умова
Вступ до Embeddings з OpenAI API
Title: A Classic Horror Story (Movie) [...]
===
Title: Nightbooks (Movie) [...]
===
Title: Irul (Movie) [...]
===
Title: Intrusion (Movie) [...]
===
Title: Things Heard & Seen (Movie) [...]
===
Title: A StoryBots Space Adventure (Movie) [...]
Вступ до Embeddings з OpenAI API

Давайте потренуємось!

Вступ до Embeddings з OpenAI API

Preparing Video For Download...