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

JSON RAG 搜索

JSONSearchTool 用于搜索 JSON 文件并返回最相关的结果。

JSONSearchTool

NOTE

JSONSearchTool 目前处于实验阶段。这意味着该工具 正处于积极开发中,用户可能会遇到意外的行为或 变更。我们非常欢迎就任何问题或改进建议 提供反馈。

描述

JSONSearchTool 旨在帮助在 JSON 文件内容中进行高效且精确的搜索。它采用 RAG(检索与生成)搜索机制,允许用户指定 JSON 路径,以便在特定的 JSON 文件中进行有针对性的搜索。这一能力显著提升了搜索结果的准确性和相关性。

安装

要安装 JSONSearchTool,请使用以下 pip 命令:

shell
pip install 'crewai[tools]'

用法示例

以下是关于如何有效利用 JSONSearchTool 在 JSON 文件中进行搜索的最新示例。这些示例考虑了代码库中当前的实现方式和用法模式。

python
from crewai_tools import JSONSearchTool

# General JSON content search
# This approach is suitable when the JSON path is either known beforehand or can be dynamically identified.
tool = JSONSearchTool()

# Restricting search to a specific JSON file
# Use this initialization method when you want to limit the search scope to a specific JSON file.
tool = JSONSearchTool(json_path='./path/to/your/file.json')

参数

  • json_path(str,可选):指定要搜索的 JSON 文件的路径。如果工具是为通用搜索初始化的,则不需要此参数。提供后,搜索将限制在指定的 JSON 文件中。

配置选项

JSONSearchTool 通过配置字典支持广泛的定制。这让用户可以根据自身需求为嵌入和摘要选择不同的模型。

python
tool = JSONSearchTool(
    config={
        "llm": {
            "provider": "ollama",  # Other options include google, openai, anthropic, llama2, etc.
            "config": {
                "model": "llama2",
                # Additional optional configurations can be specified here.
                # temperature=0.5,
                # top_p=1,
                # stream=true,
            },
        },
        "embedding_model": {
            "provider": "google-generativeai", # or openai, ollama, ...
            "config": {
                "model_name": "gemini-embedding-001",
                "task_type": "RETRIEVAL_DOCUMENT",
                # Further customization options can be added here.
            },
        },
    }
)

安全性

路径校验

提供给该工具的文件路径会针对当前工作目录进行校验。解析后位于工作目录之外的路径会被拒绝,并抛出 ValueError。

要允许工作目录之外的路径(例如在测试或可信流水线中),请设置以下环境变量:

shell
CREWAI_TOOLS_ALLOW_UNSAFE_PATHS=true

URL 校验

URL 输入会被校验:file:// URI 以及指向私有或保留 IP 段的请求会被阻止,以防止服务端请求伪造(SSRF)攻击。

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