> ## Documentation Index
> Fetch the complete documentation index at: https://ikun.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# 在 Weaviate 中过滤

> 学习如何在 Weaviate 中使用具有用户特定元数据的 PDF 文档来过滤知识库搜索。

## 代码

```python theme={null}
from agno.agent import Agent
from agno.knowledge.pdf import PDFKnowledgeBase
from agno.utils.media import (
    SampleDataFileExtension,
    download_knowledge_filters_sample_data,
)
from agno.vectordb.search import SearchType
from agno.vectordb.weaviate import Distance, VectorIndex, Weaviate

# 下载所有样本简历并获取它们的路径
downloaded_cv_paths = download_knowledge_filters_sample_data(
    num_files=5, file_extension=SampleDataFileExtension.PDF
)

# 步骤 1：用文档和元数据初始化知识库
# ------------------------------------------------------------------------------
# 在初始化知识库时，我们可以附加将被用于过滤的元数据。
# 此元数据可以包括用户 ID、文档类型、日期或任何其他属性。

vector_db = Weaviate(
    collection="recipes",
    vector_index=VectorIndex.HNSW,
    distance=Distance.COSINE,
    local=False,  # 如果使用 Weaviate Cloud 则设置为 False，如果使用本地实例则设置为 True
)

knowledge_base = PDFKnowledgeBase(
    path=[
        {
            "path": downloaded_cv_paths[0],
            "metadata": {
                "user_id": "jordan_mitchell",
                "document_type": "cv",
                "year": 2025,
            },
        },
        {
            "path": downloaded_cv_paths[1],
            "metadata": {
                "user_id": "taylor_brooks",
                "document_type": "cv",
                "year": 2025,
            },
        },
        {
            "path": downloaded_cv_paths[2],
            "metadata": {
                "user_id": "morgan_lee",
                "document_type": "cv",
                "year": 2025,
            },
        },
        {
            "path": downloaded_cv_paths[3],
            "metadata": {
                "user_id": "casey_jordan",
                "document_type": "cv",
                "year": 2025,
            },
        },
        {
            "path": downloaded_cv_paths[4],
            "metadata": {
                "user_id": "alex_rivera",
                "document_type": "cv",
                "year": 2025,
            },
        },
    ],
    vector_db=vector_db,
)

# 将所有文档加载到向量数据库中
knowledge_base.load(recreate=True)

# 步骤 2：使用不同的过滤组合查询知识库
# ------------------------------------------------------------------------------

agent = Agent(
    knowledge=knowledge_base,
    search_knowledge=True,
)

agent.print_response(
    "告诉我关于 Jordan Mitchell 的经验和技能",
    knowledge_filters={"user_id": "jordan_mitchell"},
    markdown=True,
)
```

## 用法

<Steps>
  <Step title="安装库">
    ```bash theme={null}
    pip install -U agno weaviate-client openai
    ```
  </Step>

  <Step title="运行示例">
    <CodeGroup>
      ```bash Mac theme={null}
      python cookbook/agent_concepts/knowledge/filters/filtering_weaviate.py
      ```

      ```bash Windows theme={null}
      python cookbook/agent_concepts/knowledge/filters/filtering_weaviate.py
      ```
    </CodeGroup>
  </Step>
</Steps>
