AI prompts
base on Concatenate a directory full of files into a single prompt for use with LLMs # files-to-prompt
[](https://pypi.org/project/files-to-prompt/)
[](https://github.com/simonw/files-to-prompt/releases)
[](https://github.com/simonw/files-to-prompt/actions/workflows/test.yml)
[](https://github.com/simonw/files-to-prompt/blob/master/LICENSE)
Concatenate a directory full of files into a single prompt for use with LLMs
For background on this project see [Building files-to-prompt entirely using Claude 3 Opus](https://simonwillison.net/2024/Apr/8/files-to-prompt/).
## Installation
Install this tool using `pip`:
```bash
pip install files-to-prompt
```
## Usage
To use `files-to-prompt`, provide the path to one or more files or directories you want to process:
```bash
files-to-prompt path/to/file_or_directory [path/to/another/file_or_directory ...]
```
This will output the contents of every file, with each file preceded by its relative path and separated by `---`.
### Options
- `-e/--extension <extension>`: Only include files with the specified extension. Can be used multiple times.
```bash
files-to-prompt path/to/directory -e txt -e md
```
- `--include-hidden`: Include files and folders starting with `.` (hidden files and directories).
```bash
files-to-prompt path/to/directory --include-hidden
```
- `--ignore <pattern>`: Specify one or more patterns to ignore. Can be used multiple times. Patterns may match file names and directory names, unless you also specify `--ignore-files-only`. Pattern syntax uses [fnmatch](https://docs.python.org/3/library/fnmatch.html), which supports `*`, `?`, `[anychar]`, `[!notchars]` and `[?]` for special character literals.
```bash
files-to-prompt path/to/directory --ignore "*.log" --ignore "temp*"
```
- `--ignore-files-only`: Include directory paths which would otherwise be ignored by an `--ignore` pattern.
```bash
files-to-prompt path/to/directory --ignore-files-only --ignore "*dir*"
```
- `--ignore-gitignore`: Ignore `.gitignore` files and include all files.
```bash
files-to-prompt path/to/directory --ignore-gitignore
```
- `-c/--cxml`: Output in Claude XML format.
```bash
files-to-prompt path/to/directory --cxml
```
- `-m/--markdown`: Output as Markdown with fenced code blocks.
```bash
files-to-prompt path/to/directory --markdown
```
- `-o/--output <file>`: Write the output to a file instead of printing it to the console.
```bash
files-to-prompt path/to/directory -o output.txt
```
- `-n/--line-numbers`: Include line numbers in the output.
```bash
files-to-prompt path/to/directory -n
```
Example output:
```
files_to_prompt/cli.py
---
1 import os
2 from fnmatch import fnmatch
3
4 import click
...
```
- `-0/--null`: Use NUL character as separator when reading paths from stdin. Useful when filenames may contain spaces.
```bash
find . -name "*.py" -print0 | files-to-prompt --null
```
### Example
Suppose you have a directory structure like this:
```
my_directory/
├── file1.txt
├── file2.txt
├── .hidden_file.txt
├── temp.log
└── subdirectory/
└── file3.txt
```
Running `files-to-prompt my_directory` will output:
```
my_directory/file1.txt
---
Contents of file1.txt
---
my_directory/file2.txt
---
Contents of file2.txt
---
my_directory/subdirectory/file3.txt
---
Contents of file3.txt
---
```
If you run `files-to-prompt my_directory --include-hidden`, the output will also include `.hidden_file.txt`:
```
my_directory/.hidden_file.txt
---
Contents of .hidden_file.txt
---
...
```
If you run `files-to-prompt my_directory --ignore "*.log"`, the output will exclude `temp.log`:
```
my_directory/file1.txt
---
Contents of file1.txt
---
my_directory/file2.txt
---
Contents of file2.txt
---
my_directory/subdirectory/file3.txt
---
Contents of file3.txt
---
```
If you run `files-to-prompt my_directory --ignore "sub*"`, the output will exclude all files in `subdirectory/` (unless you also specify `--ignore-files-only`):
```
my_directory/file1.txt
---
Contents of file1.txt
---
my_directory/file2.txt
---
Contents of file2.txt
---
```
### Reading from stdin
The tool can also read paths from standard input. This can be used to pipe in the output of another command:
```bash
# Find files modified in the last day
find . -mtime -1 | files-to-prompt
```
When using the `--null` (or `-0`) option, paths are expected to be NUL-separated (useful when dealing with filenames containing spaces):
```bash
find . -name "*.txt" -print0 | files-to-prompt --null
```
You can mix and match paths from command line arguments and stdin:
```bash
# Include files modified in the last day, and also include README.md
find . -mtime -1 | files-to-prompt README.md
```
### Claude XML Output
Anthropic has provided [specific guidelines](https://docs.anthropic.com/claude/docs/long-context-window-tips) for optimally structuring prompts to take advantage of Claude's extended context window.
To structure the output in this way, use the optional `--cxml` flag, which will produce output like this:
```xml
<documents>
<document index="1">
<source>my_directory/file1.txt</source>
<document_content>
Contents of file1.txt
</document_content>
</document>
<document index="2">
<source>my_directory/file2.txt</source>
<document_content>
Contents of file2.txt
</document_content>
</document>
</documents>
```
## --markdown fenced code block output
The `--markdown` option will output the files as fenced code blocks, which can be useful for pasting into Markdown documents.
```bash
files-to-prompt path/to/directory --markdown
```
The language tag will be guessed based on the filename.
If the code itself contains triple backticks the wrapper around it will use one additional backtick.
Example output:
`````
myfile.py
```python
def my_function():
return "Hello, world!"
```
other.js
```javascript
function myFunction() {
return "Hello, world!";
}
```
file_with_triple_backticks.md
````markdown
This file has its own
```
fenced code blocks
```
Inside it.
````
`````
## Development
To contribute to this tool, first checkout the code. Then create a new virtual environment:
```bash
cd files-to-prompt
python -m venv venv
source venv/bin/activate
```
Now install the dependencies and test dependencies:
```bash
pip install -e '.[test]'
```
To run the tests:
```bash
pytest
```
", Assign "at most 3 tags" to the expected json: {"id":"13766","tags":[]} "only from the tags list I provide: [{"id":77,"name":"3d"},{"id":89,"name":"agent"},{"id":17,"name":"ai"},{"id":54,"name":"algorithm"},{"id":24,"name":"api"},{"id":44,"name":"authentication"},{"id":3,"name":"aws"},{"id":27,"name":"backend"},{"id":60,"name":"benchmark"},{"id":72,"name":"best-practices"},{"id":39,"name":"bitcoin"},{"id":37,"name":"blockchain"},{"id":1,"name":"blog"},{"id":45,"name":"bundler"},{"id":58,"name":"cache"},{"id":21,"name":"chat"},{"id":49,"name":"cicd"},{"id":4,"name":"cli"},{"id":64,"name":"cloud-native"},{"id":48,"name":"cms"},{"id":61,"name":"compiler"},{"id":68,"name":"containerization"},{"id":92,"name":"crm"},{"id":34,"name":"data"},{"id":47,"name":"database"},{"id":8,"name":"declarative-gui "},{"id":9,"name":"deploy-tool"},{"id":53,"name":"desktop-app"},{"id":6,"name":"dev-exp-lib"},{"id":59,"name":"dev-tool"},{"id":13,"name":"ecommerce"},{"id":26,"name":"editor"},{"id":66,"name":"emulator"},{"id":62,"name":"filesystem"},{"id":80,"name":"finance"},{"id":15,"name":"firmware"},{"id":73,"name":"for-fun"},{"id":2,"name":"framework"},{"id":11,"name":"frontend"},{"id":22,"name":"game"},{"id":81,"name":"game-engine "},{"id":23,"name":"graphql"},{"id":84,"name":"gui"},{"id":91,"name":"http"},{"id":5,"name":"http-client"},{"id":51,"name":"iac"},{"id":30,"name":"ide"},{"id":78,"name":"iot"},{"id":40,"name":"json"},{"id":83,"name":"julian"},{"id":38,"name":"k8s"},{"id":31,"name":"language"},{"id":10,"name":"learning-resource"},{"id":33,"name":"lib"},{"id":41,"name":"linter"},{"id":28,"name":"lms"},{"id":16,"name":"logging"},{"id":76,"name":"low-code"},{"id":90,"name":"message-queue"},{"id":42,"name":"mobile-app"},{"id":18,"name":"monitoring"},{"id":36,"name":"networking"},{"id":7,"name":"node-version"},{"id":55,"name":"nosql"},{"id":57,"name":"observability"},{"id":46,"name":"orm"},{"id":52,"name":"os"},{"id":14,"name":"parser"},{"id":74,"name":"react"},{"id":82,"name":"real-time"},{"id":56,"name":"robot"},{"id":65,"name":"runtime"},{"id":32,"name":"sdk"},{"id":71,"name":"search"},{"id":63,"name":"secrets"},{"id":25,"name":"security"},{"id":85,"name":"server"},{"id":86,"name":"serverless"},{"id":70,"name":"storage"},{"id":75,"name":"system-design"},{"id":79,"name":"terminal"},{"id":29,"name":"testing"},{"id":12,"name":"ui"},{"id":50,"name":"ux"},{"id":88,"name":"video"},{"id":20,"name":"web-app"},{"id":35,"name":"web-server"},{"id":43,"name":"webassembly"},{"id":69,"name":"workflow"},{"id":87,"name":"yaml"}]" returns me the "expected json"