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.
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.
composer require stolt/llms-txt-php
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')
->link((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])
->section($section2) // alias method
->toString(); // OR ->toFile('/path/to/llmsTxtToBe.md');
use Stolt\LlmsTxt\LlmsTxt;
$llmsText = (new LlmsTxt())->parse('/path/to/llmsTxt.md'); // OR parse('markdown-string')
if ($llmsText->validate()) {
$title = $llmsText->getTitle();
$description = $llmsText->getDescription();
$details = $llmsText->getDetails();
$sections = $llmsText->getSections();
}
Tip
To interact with llms.txt
files from the console, the complement package llms-txt-php-cli might come in handy.
Vercel proposed a non-formal
standard for inlining LLM instructions in HTML, based on the llms.txt
standard.
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')
->link((new Link())->urlTitle('Link title')
->url('https://link_url')
);
$llmsTxtContent = (new LlmsTxt())->title('Test title')
->description('Test description')
->details('Test details')
->sections([$section1, $section2])
->asScriptTag(); // OR ->toEmbeddedInScriptTag()
Value of $llmsTxtContent
:
<script type="text/llms.txt">
<!-- programmatically assembled llms.txt content -->
</script>
For more usage examples, have a look at the tests i.e. LlmsTxtTest.php.
use Stolt\LlmsTxt\Extractor;
$html = <<<HTML
<html>
<body>
<script type="text/llms.txt"># first llms.txt content</script>
Some other content.
<p>And some more content.</p>
<br />
<script type="text/llms.txt"># second llms.txt content</script>
</body>
</html>
HTML;
$llmsTxts = (new Extractor())->extractFromHtml($html); // OR ->extractFromFile('/path/to/file.html')
Value of $llmsTxts
:
array(2) {
[0]=>
string(11) "#first llms.txt content"
[1]=>
string(12) "#second llms.txt content"
}
composer test
This library is licensed under the MIT license. Please see LICENSE.md for more details.
Please see CHANGELOG.md for more details.
Please see CONTRIBUTING.md for more details.