Skip to main content

Python source

Source code for llms_txt Python module, containing helpers to parse llms.txt files

Introduction

The llms.txt file spec is for files located in the path llms.txt of a website (or, optionally, in a subpath). llms-sample.txt is a simple example. A file following the spec contains the following sections as markdown, in the specific order:

  • An H1 with the name of the project or site. This is the only required section
  • A blockquote with a short summary of the project, containing key information necessary for understanding the rest of the file
  • An optional standalone Base URL: https://.../ line in the top section, setting the base URL for resolving relative links. Parsers append a trailing / if missing. When omitted, relative links resolve against the URL the llms.txt file was fetched from
  • Zero or more markdown blocks (paragraphs, lists, code blocks, or H3+ subheadings; H1 and H2 are reserved) containing more detailed information about the project and how to interpret the provided files
  • Zero or more markdown sections delimited by H2 headers, containing “file lists” of URLs where further detail is available
    • Each “file list” is a markdown list, containing a required markdown hyperlink [name](url) (relative to Base URL: or the file’s own URL, or an absolute URL), then optionally a : and notes about the file.

Here’s the start of a sample llms.txt file we’ll use for testing:

samp = Path('llms-sample.txt').read_text()
print(samp[:480])
# FastHTML

> FastHTML is a python library which brings together Starlette, Uvicorn, HTMX, and fastcore's `FT` "FastTags" into a library for creating server-rendered hypermedia applications.

Remember:

- Use `serve()` for running uvicorn (`if __name__ == "__main__"` is not needed since it's automatic)
- When a title is needed with a response, use `Titled`; note that that already wraps children in `Container`, and already includes both the meta title as well as the H1 element

Reading

We’ll implement parse_llms_file to pull out the sections of llms.txt into a simple data structure.



named_re

def named_re(
    nm, pat
):

Pattern to match pat in a named capture group


opt_re

def opt_re(
    s
):

Pattern to optionally match s

We’ll work “outside in” so we can test the innermost matches as we go.

Parse sections

sections = '''First bit.

## S1

-[foo](http://foo)
- [foo2](http://foo2): stuff

## S2

- [foo3](http://foo3)'''
start,*rest = re.split(r'^##\s+(.*?$)', sections, flags=re.MULTILINE)
start
'First bit.\n\n'
rest
['S1',
 '\n\n-[foo](http://foo)\n- [foo2](http://foo2): stuff\n\n',
 'S2',
 '\n\n- [foo3](http://foo3)']
d = dict(chunked(rest, 2))
d
{'S1': '\n\n-[foo](http://foo)\n- [foo2](http://foo2): stuff\n\n',
 'S2': '\n\n- [foo3](http://foo3)'}
links = d['S1']
links.strip()
'-[foo](http://foo)\n- [foo2](http://foo2): stuff'
_parse_links(links)
[{'title': 'foo', 'url': 'http://foo', 'desc': None},
 {'title': 'foo2', 'url': 'http://foo2', 'desc': 'stuff'}]
start, sects = _parse_llms(samp)
start
'# FastHTML\n\n> FastHTML is a python library which brings together Starlette, Uvicorn, HTMX, and fastcore\'s `FT` "FastTags" into a library for creating server-rendered hypermedia applications.\n\nRemember:\n\n- Use `serve()` for running uvicorn (`if __name__ == "__main__"` is not needed since it\'s automatic)\n- When a title is needed with a response, use `Titled`; note that that already wraps children in `Container`, and already includes both the meta title as well as the H1 element.'
title = named_re('title', r'.+?$')
summ = named_re('summary', '.+?$')
summ_pat = opt_re(fr"\n+^>\s*{summ}$")
base = named_re('base_url', r'https?://\S+')
base_pat = opt_re(fr"\n+^Base URL:\s*{base}[ \t]*$")
info = named_re('info', '.*')
pat = fr'^#\s*{title}{summ_pat}{base_pat}{opt_re(fr"\n+{info}")}'
search(pat, start, (re.MULTILINE|re.DOTALL))
{'title': 'FastHTML',
 'summary': 'FastHTML is a python library which brings together Starlette, Uvicorn, HTMX, and fastcore\'s `FT` "FastTags" into a library for creating server-rendered hypermedia applications.',
 'info': 'Remember:\n\n- Use `serve()` for running uvicorn (`if __name__ == "__main__"` is not needed since it\'s automatic)\n- When a title is needed with a response, use `Titled`; note that that already wraps children in `Container`, and already includes both the meta title as well as the H1 element.'}

parse_llms_file

def parse_llms_file(
    txt
):

Parse llms.txt file contents in txt to an AttrDict

llmsd = parse_llms_file(samp)
llmsd.summary
'FastHTML is a python library which brings together Starlette, Uvicorn, HTMX, and fastcore\'s `FT` "FastTags" into a library for creating server-rendered hypermedia applications.'
llmsd.sections.Examples
[{'title': 'Todo list application', 'url': 'https://raw.githubusercontent.com/AnswerDotAI/fasthtml/main/examples/adv_app.py', 'desc': 'Detailed walk-thru of a complete CRUD app in FastHTML showing idiomatic use of FastHTML and HTMX patterns.'}]

A standalone Base URL: line after the summary sets base_url with a trailing /, while ### subheadings stay in info and non-link lines such as HTML comments are skipped:

v21 = '# Demo\n\n> Short summary\n\nBase URL: https://example.com/docs\n\n### Notes\n\nKeep it brief.\n\n## Docs\n\n- [Guide](guide.md): Main guide\n\n<!-- Generated -->'
rd = parse_llms_file(v21)
test_eq(rd.base_url, 'https://example.com/docs/')
test_eq(rd.info, '### Notes\n\nKeep it brief.')
test_eq(rd.sections, {'Docs': [dict(title='Guide', url='guide.md', desc='Main guide')]})
rd