Skip to content

Commit

Permalink
LLM Context
Browse files Browse the repository at this point in the history
  • Loading branch information
Isaac-Flath committed Dec 24, 2024
1 parent 6d92ea0 commit 52d03a0
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/llms.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Update LLM Contexts

on:
push:
branches:
- main # or whatever your default branch is named

jobs:
update-docs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.x'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
- name: Run update script
run: |
chmod +x llms.py
python llms.py
- name: Commit changes
run: |
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git add nbs/llms-ctx.txt nbs/apilist.txt
git commit -m "Auto-update API lists and context files" || echo "No changes to commit"
git push
67 changes: 67 additions & 0 deletions llms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import httpx
from pathlib import Path

def llms_txt():
"""Creates llms.txt containing index of example FastHTML apps"""
# Get all app.py files and their categories
examples = Path.cwd()/'examples'

# Group files by category
by_cat = {}
for f in examples.rglob('app.py'):
cat = f.relative_to(examples).parts[0]
by_cat.setdefault(cat,[]).append((f.parent.name, f.relative_to(examples)))

# Create content with categories and examples
lines = ['# FastHTML Gallery Examples\n',
'> FastHTML Gallery bring minimal examples of FastHTML apps to allow you to get started with FastHTML more easily\n']
for cat in sorted(by_cat):
lines.append(f'\n## {cat.replace("_"," ").title()}')
for name,path in sorted(by_cat[cat]):
# Create raw github link
raw_link = f'https://github.com/AnswerDotAI/fasthtml-gallery/blob/main/examples/{path}'
lines.append(f'- {name.replace("_"," ").title()}({raw_link})')

# Write to file
(examples.parent/'llms.txt').write_text('\n'.join(lines))


def llms_ctx_txt():
"""Creates llms_ctx.txt containing XML-formatted index of example FastHTML apps with source code"""
# Get all app.py files and their categories
examples = Path.cwd()/'examples'

# Group files by category
by_cat = {}
for f in examples.rglob('app.py'):
cat = f.relative_to(examples).parts[0]
by_cat.setdefault(cat,[]).append((f.parent.name, f.relative_to(examples)))

# Create XML-style content
lines = ['<document>',
'<title>FastHTML Gallery Examples</title>',
'<description>FastHTML Gallery bring minimal examples of FastHTML apps to allow you to get started with FastHTML more easily</description>']

for cat in sorted(by_cat):
lines.append(f'<category name="{cat.replace("_"," ").title()}">')
for name, path in sorted(by_cat[cat]):
# Get raw github content
raw_url = f'https://raw.githubusercontent.com/AnswerDotAI/fasthtml-gallery/main/examples/{path}'
r = httpx.get(raw_url)
source_code = r.text

lines.append(f' <example name="{name.replace("_"," ").title()}">')
lines.append(source_code)
lines.append(' </example>')
lines.append('</category>')

lines.append('</document>')

# Write to file
(examples.parent/'llms_ctx.txt').write_text('\n'.join(lines))



if __name__ == '__main__':
llms_txt()
llms_ctx_txt()

0 comments on commit 52d03a0

Please sign in to comment.