使用中介資料進行篩選
在資料庫中使用中介資料(metadata),可以更輕鬆地依額外條件來篩選結果。想像一下,你建立的電影推薦系統能讀取使用者的偏好設定,並據此進一步過濾推薦清單。
在這個練習中,你會使用額外的中介資料來篩選 Netflix 的電影推薦。netflix_titles 集合已更新,為每個標題新增了中介資料,包括「'rating'」(分級)— 該標題的年齡分級,以及「'release_year'」— 該標題最初發行的年份。
以下是更新後項目的預覽:
{'ids': ['s999'],
'embeddings': None,
'metadatas': [{'rating': 'TV-14', 'release_year': 2021}],
'documents': ['Title: Searching For Sheela (Movie)\nDescription: Journalists and fans await Ma Anand Sheela as the infamous former Rajneesh commune's spokesperson returns to India after decades for an interview tour.\nCategories: Documentaries, International Movies'],
'uris': None,
'data': None}
本練習屬於課程
Introduction to Embeddings with the OpenAI API
練習說明
- 使用
reference_texts從集合中查詢 2 筆結果。 - 將結果篩選為同時符合分級為
'G',且發行年份早於2019的標題。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
collection = client.get_collection(
name="netflix_titles",
embedding_function=OpenAIEmbeddingFunction(model_name="text-embedding-3-small", api_key="")
)
reference_texts = ["children's story about a car", "lions"]
# Query two results using reference_texts
result = collection.query(
____,
____,
# Filter for titles with a G rating released before 2019
____={
____: [
{"____":
{____}
},
{"____":
{____}
}
]
}
)
print(result['documents'])