files-to-prompt深度解析:从.gitignore到Claude XML的完整配置教程

【免费下载链接】files-to-prompt Concatenate a directory full of files into a single prompt for use with LLMs 【免费下载链接】files-to-prompt 项目地址: https://gitcode.com/gh_mirrors/fi/files-to-prompt

files-to-prompt是一款高效的文件内容合并工具,能够将目录中的多个文件内容整合为单个文本输出,特别适合为大语言模型(LLM)准备提示词。本文将详细介绍如何从基础安装到高级配置,全面掌握这款工具的使用技巧。

快速安装:30秒上手files-to-prompt

files-to-prompt支持通过Python包管理器pip快速安装,在终端中执行以下命令即可完成安装:

pip install files-to-prompt

安装完成后,可通过files-to-prompt --help命令验证安装是否成功,并查看所有可用选项。

基础使用:一键合并文件内容

最简化操作

只需指定目标目录路径,工具会自动递归处理所有文件:

files-to-prompt path/to/your/directory

输出格式示例:

path/to/file1.txt
---
文件1的内容
---
path/to/subdir/file2.txt
---
文件2的内容
---

核心功能选项

  • 按扩展名筛选:使用-e参数指定需要包含的文件类型

    files-to-prompt ./src -e py -e md
    
  • 包含隐藏文件:添加--include-hidden参数

    files-to-prompt ./docs --include-hidden
    
  • 输出到文件:使用-o参数指定输出文件路径

    files-to-prompt ./project -o prompt.txt
    
  • 显示行号:添加-n参数为代码内容添加行号

    files-to-prompt ./scripts -n
    

.gitignore高级配置:精准控制文件包含

files-to-prompt默认会遵守项目中的.gitignore规则,自动排除不需要的文件。通过以下方式可以灵活控制文件筛选:

基本.gitignore支持

工具会自动读取目录中的.gitignore文件,例如以下.gitignore内容:

*.log
temp/
.env

执行命令时会自动排除所有.log文件、temp目录和.env文件:

files-to-prompt ./project

忽略.gitignore规则

如需强制包含.gitignore中排除的文件,使用--ignore-gitignore参数:

files-to-prompt ./project --ignore-gitignore

自定义忽略模式

使用--ignore参数添加额外忽略规则,支持通配符匹配:

files-to-prompt ./project --ignore "*.tmp" --ignore "test_*"

如果希望忽略规则仅适用于文件而不影响目录,添加--ignore-files-only参数:

files-to-prompt ./project --ignore-files-only --ignore "docs*"

Claude XML格式:优化LLM提示词结构

Anthropic的Claude模型推荐使用特定的XML格式来组织长文本输入,files-to-prompt提供了--cxml参数直接生成符合要求的格式。

生成Claude XML格式

files-to-prompt ./code --cxml

输出示例:

<documents>
<document index="1">
<source>code/main.py</source>
<document_content>
import os
def main():
    print("Hello World")
</document_content>
</document>
<document index="2">
<source>code/utils.py</source>
<document_content>
def helper():
    return True
</document_content>
</document>
</documents>

XML格式优势

  • 清晰的文件边界划分,帮助LLM区分不同文件内容
  • 包含文件路径元数据,便于模型理解代码结构
  • 符合Claude长上下文优化建议,提升处理效率

实战案例:构建LLM代码分析提示词

以下是一个完整的使用案例,将Python项目转换为适合代码审查的提示词:

files-to-prompt ./my_python_project -e py -n --cxml -o code_analysis_prompt.xml

这个命令将:

  1. 仅包含.py文件
  2. 添加行号便于引用
  3. 生成Claude XML格式
  4. 输出到code_analysis_prompt.xml文件

开发与扩展

files-to-prompt是开源项目,源码托管在https://gitcode.com/gh_mirrors/fi/files-to-prompt。如需贡献代码或了解更多实现细节,可以通过以下步骤获取源码:

git clone https://gitcode.com/gh_mirrors/fi/files-to-prompt
cd files-to-prompt
python -m venv venv
source venv/bin/activate
pip install -e '.[test]'

核心功能实现位于files_to_prompt/cli.py,主要包含文件遍历、过滤和内容格式化逻辑。测试用例位于tests/test_files_to_prompt.py,覆盖了各种使用场景。

总结

files-to-prompt提供了从简单到高级的文件内容合并方案,无论是快速生成代码提示词,还是构建结构化的LLM输入,都能满足需求。通过灵活配置.gitignore规则和输出格式,你可以轻松定制符合特定场景的文本合并结果。

无论是开发者、研究人员还是内容创作者,files-to-prompt都能成为你与大语言模型交互的得力助手,让复杂的文件处理变得简单高效。

【免费下载链接】files-to-prompt Concatenate a directory full of files into a single prompt for use with LLMs 【免费下载链接】files-to-prompt 项目地址: https://gitcode.com/gh_mirrors/fi/files-to-prompt

Logo

欢迎加入DeepSeek 技术社区。在这里,你可以找到志同道合的朋友,共同探索AI技术的奥秘。

更多推荐