Skip to content
CrewAI 文档版本 跟随上游 源快照 65b91b48 更新于 2026-10-18

目录 RAG 搜索

DirectorySearchTool 是一个强大的 RAG(检索增强生成)工具,专为在目录内容中执行语义搜索而设计。

DirectorySearchTool

NOTE

实验性功能:DirectorySearchTool 仍在持续开发中。随着我们不断完善该工具,功能可能会演变,并且可能会出现意外行为。

描述

DirectorySearchTool 可在指定目录的内容中执行语义搜索,利用检索增强生成(RAG)方法高效浏览文件。为提供灵活性,它允许用户在运行时动态指定搜索目录,或在初始设置时设置固定目录。

安装

要使用 DirectorySearchTool,请先安装 crewai_tools 包。在终端中执行以下命令:

shell
pip install 'crewai[tools]'

初始化与使用

首先从 crewai_tools 包导入 DirectorySearchTool。你可以初始化该工具时不指定目录,从而在运行时设置搜索目录。或者,也可以使用预定义的目录初始化该工具。

python
from crewai_tools import DirectorySearchTool

# For dynamic directory specification at runtime
tool = DirectorySearchTool()

# For fixed directory searches
tool = DirectorySearchTool(directory='/path/to/directory')

参数

  • directory:一个指定搜索目录的字符串参数。该参数在初始化时为可选,但如果初始时未设置,则搜索时必需。

自定义模型和嵌入

默认情况下,DirectorySearchTool 使用 OpenAI 进行嵌入和摘要。这些设置的自定义选项包括更改模型服务商和配置,为高级用户提供更高的灵活性。

python
from chromadb.config import Settings

tool = DirectorySearchTool(
    config={
        "embedding_model": {
            "provider": "openai",
            "config": {
                "model": "text-embedding-3-small",
                # "api_key": "sk-...",
            },
        },
        "vectordb": {
            "provider": "chromadb",  # or "qdrant"
            "config": {
                # "settings": Settings(persist_directory="/content/chroma", allow_reset=True, is_persistent=True),
                # from qdrant_client.models import VectorParams, Distance
                # "vectors_config": VectorParams(size=384, distance=Distance.COSINE),
            }
        },
    }
)

## Security

### Path Validation

Directory paths provided to this tool are validated against the current working directory. Paths that resolve outside the working directory are rejected with a `ValueError`.

To allow paths outside the working directory (for example, in tests or trusted pipelines), set the environment variable:

CREWAI_TOOLS_ALLOW_UNSAFE_PATHS=true


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.

BytesRouter 是社区翻译站点,所有译文仅供参考,一切以官方英文原文为准。