开始使用免费开始使用

使用元数据进行过滤

在数据库中提供可用的元数据,可以更方便地基于附加条件来过滤结果。想象一下,您正在创建的电影推荐系统可以访问用户的偏好设置,并据此进一步过滤结果。

在本练习中,您将使用额外的元数据来过滤 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}

本练习是课程的一部分

使用 OpenAI API 的 Embeddings 入门

查看课程

练习说明

  • 使用 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'])
编辑并运行代码