跳转到主要内容

概述

我们仍在努力改进工具,因此未来可能会出现意外行为或变更。
此工具用于对文本文件的内容执行 RAG(检索增强生成)搜索。它允许在指定文本文件内容中进行查询的语义搜索,使其成为根据所提供的查询快速提取信息或查找特定文本部分的宝贵资源。

安装

要使用 TXTSearchTool,您首先需要安装 crewai_tools 包。这可以使用 pip(Python 的包管理器)完成。打开您的终端或命令提示符,输入以下命令:
pip install 'crewai[tools]'
此命令将下载并安装 TXTSearchTool 以及所有必要的依赖项。

示例

以下示例演示了如何使用 TXTSearchTool 在文本文件中进行搜索。此示例展示了工具使用特定文本文件进行初始化以及随后在该文件内容中进行搜索。
代码
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 进行嵌入和摘要。要自定义模型,您可以使用如下所示的配置字典:
代码
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").
            }
        },
    }
)