stolt / llms-txt-php
A library for writing, reading, and validating llms.txt Markdown files.
Requires
- php: >=8.1
- ext-curl: *
- symfony/console: ^7.3
Requires (Dev)
- dg/bypass-finals: ^1.9
- friendsofphp/php-cs-fixer: ^3.70.1
- peckphp/peck: ^0.1.2
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^11.4.4
- stolt/lean-package-validator: ^4.4
- zenstruck/console-test: ^1.7
README
This library supports you in creating, reading, and validating llms.txt Markdown files via PHP.
A good example llms.txt
file is the one from the uv project.
What's llms.txt?
Think of it like robots.txt
for LLMs. The evolving spec is available over here.
For the structure of a llms.txt
file you can also have a look at this repository's llms.txt file.
Installation and usage
composer require stolt/llms-txt-php
Creating a llms.txt file
use Stolt\LlmsTxt\LlmsTxt; use Stolt\LlmsTxt\Section; use Stolt\LlmsTxt\Section\Link; $section1 = (new Section())->name('Section name') ->addLink((new Link())->urlTitle('Link title') ->url('https://link_url')->urlDetails('Optional link details') ); $section2 = (new Section())->name('Optional') ->addLink((new Link())->urlTitle('Link title') ->url('https://link_url') ); $llmsTxt = (new LlmsTxt())->title('Test title') ->description('Test description') ->details('Test details') ->addSection($section1) // OR ->addSections([$section1, $section2]) ->addSection($section2) ->toString(); // OR ->toFile('/path/to/llmsTxtToBe.md');
Validating and reading a llms.txt file and its parts
use Stolt\LlmsTxt\LlmsTxt; $llmsText = (new LlmsTxt())->parse('/path/to/llmsTxt.md'); if ($llmsText->validate()) { $title = $llmsText->getTitle(); $description = $llmsText->getDescription(); $details = $llmsText->getDetails(); $sections = $llmsText->getSections(); }
For more usage examples, have a look at the tests i.e. LlmsTxtTest.php.
Validating a llms.txt file via the included CLI
bin/llms-txt validate tests/fixtures/uv.llms.md The provided llms txt file tests/fixtures/uv.llms.md is valid.
or
bin/llms-txt validate https://docs.astral.sh/uv/ The delivered llms txt file from https://docs.astral.sh/uv/ is valid.
A llms.txt
file is considered valid, when a title, description, details, and at least one section
with at least one link are available.
Initialising a llms.txt file via the included CLI
bin/llms-txt init [<llms-txt-file>] Created llms txt file llms.txt. cat llms.txt # Init title > Init description Init details ## Init section - [Init URL](http://init.org)
If no llms-txt-file
name is provided, it defaults to llms.txt
.
Running tests
composer test
License
This library is licensed under the MIT license. Please see LICENSE.md for more details.
Changelog
Please see CHANGELOG.md for more details.
Contributing
Please see CONTRIBUTING.md for more details.