Více dotazů a filtrování

Introduction to Embeddings with the OpenAI API

Emmanuel Pire

Senior Software Engineer, DataCamp

Doporučení filmů na základě více datových bodů

 

  • Terrifier (id: 's8170')
  • Strawberry Shortcake: Berry Bitty Adventures (id: 's8103')
Introduction to Embeddings with the OpenAI API

Více textů dotazu

reference_ids = ['s8170', 's8103']


reference_texts = collection.get(ids=reference_ids)["documents"]
result = collection.query( query_texts=reference_texts, n_results=3 )
Introduction to Embeddings with the OpenAI API

Výsledek více textů dotazu

{'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]]}
Introduction to Embeddings with the OpenAI API

Přidávání metadat

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'])
    })

 

  • Vytvoření seznamu slovníků pro metadata
  • Vytvoření seznamu ID pro přidání ke stávajícím položkám
Introduction to Embeddings with the OpenAI API

Přidávání a dotazování metadat

 

collection.update(ids=ids, metadatas=metadatas)
result = collection.query(
    query_texts=reference_texts, 
    n_results=3,
    where={ 
        "type": "Movie"
    }
)
Introduction to Embeddings with the OpenAI API

Operátory where

where={
  "type": "Movie"
}

je stejné jako

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

Seznam operátorů:

  • $eq – rovno (string, int, float)
  • $ne – nerovno (string, int, float)
  • $gt – větší než (int, float)
  • $gte – větší nebo rovno (int, float)
  • $lt – menší než (int, float)
  • $lte – menší nebo rovno (int, float)
Introduction to Embeddings with the OpenAI API

Více filtrů where

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

 

  • $or: filtrování na základě alespoň jedné podmínky
Introduction to Embeddings with the 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) [...]
Introduction to Embeddings with the OpenAI API

Pojďme si procvičit!

Introduction to Embeddings with the OpenAI API

Preparing Video For Download...