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:
# 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.
search
def search( pat, txt, flags:int=0):
Dictionary of matched groups in pat within txt
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 links
link ='- [FastHTML quick start](https://fastht.ml/docs/tutorials/quickstart_for_web_devs.html.md): A brief overview of FastHTML features'
title = named_re('title', r'[^\]]+')pat =fr'-\s*\[{title}\]'search(pat, samp)
'# 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.'
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