使用Python给自己网站生成llms.txt

本文提供了一个通过CLI和Python API来解析文件并从中创建llms.txt。输入文件应遵循以下格式:

markdown 复制代码
# FastHTML

> FastHTML is a python library which...

When writing FastHTML apps remember to:

- Thing to remember

## Docs

- [Surreal](https://host/README.md): Tiny jQuery alternative with Locality of Behavior
- [FastHTML quick start](https://host/quickstart.html.md): An overview of FastHTML features

## Examples

- [Todo app](https://host/adv_app.py)

## Optional

- [Starlette docs](https://host/starlette-sml.md): A subset of the Starlette docs

安装

复制代码
pip install llms-txt

如何使用

CLI

安装后,llms_txt2ctx在您的终端中可用。

要获取CLI的帮助:

复制代码
llms_txt2ctx -h

要将llms.txt文件到XML上下文并保存到llms.md:

复制代码
llms_txt2ctx llms.txt > llms.md

通过--optional True添加输入文件的 "选项" 部分。

Python模块

javascript 复制代码
from llms_txt import *
ini 复制代码
samp = Path('llms-sample.txt').read_text()

使用parse_llms_file使用llms.txt文件的部分创建数据结构 (还可以添加optional=True如果需要):

scss 复制代码
parsed = parse_llms_file(samp)
list(parsed)
css 复制代码
['title', 'summary', 'info', 'sections']
复制代码
parsed.title,parsed.summary
vbnet 复制代码
('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.')
scss 复制代码
list(parsed.sections)
css 复制代码
['Docs', 'Examples', 'Optional']
css 复制代码
parsed.sections.Optional[0]
rust 复制代码
{ 'desc': 'A subset of the Starlette documentation useful for FastHTML '
          'development.',
  'title': 'Starlette full documentation',
  'url': 'https://gist.githubusercontent.com/jph00/809e4a4808d4510be0e3dc9565e9cbd3/raw/9b717589ca44cedc8aaf00b2b8cacef922964c0f/starlette-sml.md'}

使用create_ctx创建一个包含XML部分的LLM上下文文件,适用于Claude等系统 (这是CLI在幕后调用的)。

ini 复制代码
ctx = create_ctx(samp)
scss 复制代码
print(ctx[:300])
vbnet 复制代码
<project title="FastHTML" summary='FastHTML is a python library which brings together Starlette, Uvicorn, HTMX, and fastcore&#39;s `FT` "FastTags" into a library for creating server-rendered hypermedia applications.'>
Remember:

- Use `serve()` for running uvicorn (`if __name__ == "__main__"` is not

测试和部署

显示解析有多简单llms.txt文件,这里是一个完整的解析器,在 <20行代码中,没有依赖关系:

python 复制代码
from pathlib import Path
import re,itertools

def chunked(it, chunk_sz):
    it = iter(it)
    return iter(lambda: list(itertools.islice(it, chunk_sz)), [])

def parse_llms_txt(txt):
    "Parse llms.txt file contents in `txt` to a `dict`"
    def _p(links):
        link_pat = '-\s*[(?P<title>[^]]+)]((?P<url>[^)]+))(?::\s*(?P<desc>.*))?'
        return [re.search(link_pat, l).groupdict()
                for l in re.split(r'\n+', links.strip()) if l.strip()]

    start,*rest = re.split(fr'^##\s*(.*?$)', txt, flags=re.MULTILINE)
    sects = {k: _p(v) for k,v in dict(chunked(rest, 2)).items()}
    pat = '^#\s*(?P<title>.+?$)\n+(?:^>\s*(?P<summary>.+?$)$)?\n+(?P<info>.*)'
    d = re.search(pat, start.strip(), (re.MULTILINE|re.DOTALL)).groupdict()
    d['sections'] = sects
    return d

我们提供了一个测试套件tests/test-parse.py并确认此实现通过了所有测试。

相关推荐
AKAMAI1 小时前
Akamai Cloud客户案例 | CloudMinister借助Akamai实现多云转型
人工智能·云计算
章豪Mrrey nical2 小时前
前后端分离工作详解Detailed Explanation of Frontend-Backend Separation Work
后端·前端框架·状态模式
小a杰.3 小时前
Flutter 与 AI 深度集成指南:从基础实现到高级应用
人工智能·flutter
colorknight3 小时前
数据编织-异构数据存储的自动化治理
数据仓库·人工智能·数据治理·数据湖·数据科学·数据编织·自动化治理
派大鑫wink4 小时前
【JAVA学习日志】SpringBoot 参数配置:从基础到实战,解锁灵活配置新姿势
java·spring boot·后端
Lun3866buzha4 小时前
篮球场景目标检测与定位_YOLO11-RFPN实现详解
人工智能·目标检测·计算机视觉
janefir4 小时前
LangChain框架下DirectoryLoader使用报错zipfile.BadZipFile
人工智能·langchain
程序员爱钓鱼4 小时前
Node.js 编程实战:文件读写操作
前端·后端·node.js
xUxIAOrUIII4 小时前
【Spring Boot】控制器Controller方法
java·spring boot·后端
Dolphin_Home4 小时前
从理论到实战:图结构在仓库关联业务中的落地(小白→中级,附完整代码)
java·spring boot·后端·spring cloud·database·广度优先·图搜索算法