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

TXT RAG 搜索

TXTSearchTool 旨在在文本文件的内容中执行 RAG(检索增强生成)搜索。

概述

NOTE

我们仍在改进这些工具,因此未来可能会出现意外行为或变更。

该工具用于在文本文件的内容中执行 RAG(检索增强生成)搜索。 它支持在指定的文本文件内容中对查询进行语义搜索, 使其成为基于所提供查询快速提取信息或查找特定文本片段的宝贵资源。

安装

要使用 TXTSearchTool,你首先需要安装 crewai_tools 包。 这可以通过 pip(Python 的包管理器)完成。 打开终端或命令提示符,输入以下命令:

shell
pip install 'crewai[tools]'

该命令将下载并安装 TXTSearchTool 及其所需的依赖项。

示例

以下示例演示了如何使用 TXTSearchTool 在文本文件中进行搜索。 该示例同时展示了如何使用特定文本文件初始化该工具,以及随后在该文件内容中进行搜索。

python
from crewai_tools import TXTSearchTool

# Initialize the tool to search within any text file's content 
# the agent learns about during its execution
tool = TXTSearchTool()

# OR

# Initialize the tool with a specific text file, 
# so the agent can search within the given text file's content
tool = TXTSearchTool(txt='path/to/text/file.txt')

参数

  • txt(str):可选。要搜索的文本文件路径。 仅当工具未使用特定文本文件初始化时才需要该参数; 否则,搜索将在最初提供的文本文件内进行。

自定义模型和嵌入

默认情况下,该工具使用 OpenAI 进行嵌入和摘要。 要自定义模型,可以使用如下配置字典:

python
from chromadb.config import Settings

tool = TXTSearchTool(
    config={
        # Required: embeddings provider + config
        "embedding_model": {
            "provider": "openai",  # or google-generativeai, cohere, ollama, ...
            "config": {
                "model": "text-embedding-3-small",
                # "api_key": "sk-...",  # optional if env var is set (e.g., OPENAI_API_KEY or EMBEDDINGS_OPENAI_API_KEY)
                # Provider examples:
                # Google → model_name: "gemini-embedding-001", task_type: "RETRIEVAL_DOCUMENT"
                # Cohere → model: "embed-english-v3.0"
                # Ollama → model: "nomic-embed-text"
            },
        },

        # Required: vector database config
        "vectordb": {
            "provider": "chromadb",  # or "qdrant"
            "config": {
                # Chroma settings (optional persistence)
                # "settings": Settings(
                #     persist_directory="/content/chroma",
                #     allow_reset=True,
                #     is_persistent=True,
                # ),

                # Qdrant vector params example:
                # from qdrant_client.models import VectorParams, Distance
                # "vectors_config": VectorParams(size=384, distance=Distance.COSINE),

                # Note: collection name is controlled by the tool (default: "rag_tool_collection").
            }
        },
    }
)

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