다중 쿼리 및 필터링

OpenAI API로 시작하는 임베딩 Introduction

Emmanuel Pire

Senior Software Engineer, DataCamp

여러 데이터 기반 영화 추천

 

  • Terrifier (id: 's8170')
  • Strawberry Shortcake: Berry Bitty Adventures (id: 's8103')
OpenAI API로 시작하는 임베딩 Introduction

다중 쿼리 텍스트

reference_ids = ['s8170', 's8103']


reference_texts = collection.get(ids=reference_ids)["documents"]
result = collection.query( query_texts=reference_texts, n_results=3 )
OpenAI API로 시작하는 임베딩 Introduction

다중 쿼리 텍스트 결과

{'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]]}
OpenAI API로 시작하는 임베딩 Introduction

메타데이터 추가

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 목록 생성
OpenAI API로 시작하는 임베딩 Introduction

메타데이터 추가 및 쿼리

 

collection.update(ids=ids, metadatas=metadatas)
result = collection.query(
    query_texts=reference_texts, 
    n_results=3,
    where={ 
        "type": "Movie"
    }
)
OpenAI API로 시작하는 임베딩 Introduction

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)
OpenAI API로 시작하는 임베딩 Introduction

다중 Where 필터

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

 

  • $or: 하나 이상의 조건으로 필터링
OpenAI API로 시작하는 임베딩 Introduction
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) [...]
OpenAI API로 시작하는 임베딩 Introduction

연습해 봅시다!

OpenAI API로 시작하는 임베딩 Introduction

Preparing Video For Download...