> ## 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.

# 使用 Docx 进行筛选

> 了解如何使用具有用户特定元数据的 Docx 文档来筛选知识库搜索。

## 代码

```python theme={null}
"""
用户级别知识筛选示例

本手册演示了如何使用知识筛选器将知识库搜索限制为特定用户、文档类型或任何其他元数据属性。

演示的关键概念：
1. 加载具有用户特定元数据的文档
2. 按用户 ID 筛选知识库搜索
3. 组合多个筛选条件
4. 比较不同筛选组合的结果

您可以通过以下方式传递筛选条件：
1. 如果仅在 Agent 上传递，则所有运行都使用它
2. 如果在 run/print_response 上传递，则该运行使用它
3. 如果两者都传递，则我们使用 run/print_response 上传递的筛选条件在此运行中覆盖
"""

from agno.agent import Agent
from agno.knowledge.docx import DocxKnowledgeBase
from agno.utils.media import (
    SampleDataFileExtension,
    download_knowledge_filters_sample_data,
)
from agno.vectordb.lancedb import LanceDb

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

# 初始化 LanceDB
vector_db = LanceDb(
    table_name="recipes",
    uri="tmp/lancedb",
)

# 现在在知识库初始化中使用下载的路径
knowledge_base = DocxKnowledgeBase(
    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：使用不同的筛选条件组合查询知识库
# ------------------------------------------------------------------------------

# 选项 1：在 Agent 上设置筛选条件
# 使用知识库和筛选条件初始化 Agent
agent = Agent(
    knowledge=knowledge_base,
    search_knowledge=True,
    knowledge_filters={"user_id": "jordan_mitchell"},
)

# 查询 Jordan Mitchell 的经验和技能
agent.print_response(
    "告诉我关于 Jordan Mitchell 的经验和技能",
    markdown=True,
)

# # 选项 2：在 run/print_response 上设置筛选条件
# agent = Agent(
#     knowledge=knowledge_base,
#     search_knowledge=True,
# )

# # 查询 Taylor Brooks 作为候选人
# agent.print_response(
#     "告诉我关于 Taylor Brooks 作为候选人的信息",
#     knowledge_filters={"user_id": "taylor_brooks"},
#     markdown=True,
# )
```

## 用法

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

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

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