-
Notifications
You must be signed in to change notification settings - Fork 9
feat: create llms.txt generator #254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
21bb6e2
feat: create llms.txt generator
araujogui 277f607
refactor: improvements
araujogui e585088
fix: typo
araujogui 32fb861
refactor: remove paragraphToString util
araujogui b074a7f
refactor: some improvements
araujogui 646b826
fix: doc api url path
araujogui 419dcd6
fix: doc api url path
araujogui ebc4828
refactor: some changes
araujogui 28aaa10
feat: add llm_description prop
araujogui f00fe97
test: add llm_description prop
araujogui 1bf3748
refacotr: remove template replace
araujogui 9904ecb
feat(linter): create llm description rule
araujogui ee63084
refactor(linter): remove for-of loop
araujogui c8d86c7
refactor(llms-txt): remove docs url suffix
araujogui b4dbc2d
fix: base url const
araujogui File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// These files are not part of the API documentation and are manually included | ||
// in the llms.txt file | ||
export const ENTRY_IGNORE_LIST = [ | ||
'doc/api/synopsis.md', | ||
'doc/api/documentation.md', | ||
]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { readFile, writeFile } from 'node:fs/promises'; | ||
import { join } from 'node:path'; | ||
|
||
import { buildApiDocLink } from './utils/buildApiDocLink.mjs'; | ||
import { ENTRY_IGNORE_LIST } from './constants.mjs'; | ||
import { getIntroLinks } from './utils/getIntroLinks.mjs'; | ||
|
||
/** | ||
* This generator generates a llms.txt file to provide information to LLMs at | ||
* inference time | ||
* | ||
* @typedef {Array<ApiDocMetadataEntry>} Input | ||
* | ||
* @type {GeneratorMetadata<Input, string>} | ||
*/ | ||
export default { | ||
name: 'llms-txt', | ||
|
||
version: '1.0.0', | ||
|
||
description: | ||
'Generates a llms.txt file to provide information to LLMs at inference time', | ||
|
||
dependsOn: 'ast', | ||
|
||
/** | ||
* Generates a llms.txt file | ||
* | ||
* @param {Input} entries | ||
* @param {Partial<GeneratorOptions>} options | ||
* @returns {Promise<void>} | ||
*/ | ||
async generate(entries, { output }) { | ||
const template = await readFile( | ||
join(import.meta.dirname, 'template.txt'), | ||
'utf-8' | ||
); | ||
|
||
const introLinks = getIntroLinks().join('\n'); | ||
|
||
const apiDocsLinks = entries | ||
.filter(entry => { | ||
// Filter non top-level headings and ignored entries | ||
return ( | ||
araujogui marked this conversation as resolved.
Show resolved
Hide resolved
|
||
entry.heading.depth === 1 || ENTRY_IGNORE_LIST.includes(entry.path) | ||
araujogui marked this conversation as resolved.
Show resolved
Hide resolved
|
||
); | ||
}) | ||
.map(entry => { | ||
araujogui marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const link = buildApiDocLink(entry); | ||
return `- ${link}`; | ||
}) | ||
.join('\n'); | ||
|
||
const filledTemplate = template | ||
.replace('__INTRODUCTION__', introLinks) | ||
.replace('__API_DOCS__', apiDocsLinks); | ||
|
||
if (output) { | ||
await writeFile(join(output, 'llms.txt'), filledTemplate); | ||
} | ||
|
||
return filledTemplate; | ||
}, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Node.js Documentation | ||
|
||
> Node.js is an open-source, cross-platform JavaScript runtime environment that executes JavaScript code outside a web browser. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient for building scalable network applications. | ||
|
||
## Introduction | ||
araujogui marked this conversation as resolved.
Show resolved
Hide resolved
|
||
__INTRODUCTION__ | ||
|
||
## API Documentations | ||
__API_DOCS__ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { LATEST_DOC_API_BASE_URL } from '../../../constants.mjs'; | ||
import { transformNodeToString } from '../../../utils/unist.mjs'; | ||
|
||
/** | ||
* Builds a markdown link for an API doc entry | ||
* | ||
* @param {ApiDocMetadataEntry} entry | ||
* @returns {string} | ||
*/ | ||
export const buildApiDocLink = entry => { | ||
araujogui marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const title = entry.heading.data.name; | ||
|
||
// Remove the leading doc/ from the path | ||
const path = entry.api_doc_source.replace(/^doc\//, ''); | ||
const url = new URL(path, LATEST_DOC_API_BASE_URL); | ||
|
||
const link = `[${title}](${url})`; | ||
|
||
// Find the first paragraph in the content | ||
const descriptionNode = entry.content.children.find( | ||
child => child.type === 'paragraph' | ||
); | ||
|
||
if (!descriptionNode) { | ||
return link; | ||
} | ||
|
||
const description = transformNodeToString(descriptionNode) | ||
// Remove newlines and extra spaces | ||
.replace(/[\r\n]+/g, ' '); | ||
|
||
return `${link}: ${description}`; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { LATEST_DOC_API_BASE_URL } from '../constants.mjs'; | ||
|
||
/** | ||
* Generates a list of introduction links for the llms.txt file | ||
* | ||
* @returns {string[]} | ||
*/ | ||
export const getIntroLinks = () => { | ||
araujogui marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const aboutDocUrl = new URL('/api/documentation.md', LATEST_DOC_API_BASE_URL); | ||
const usageExamplesUrl = new URL('/api/synopsis.md', LATEST_DOC_API_BASE_URL); | ||
|
||
const introLinks = [ | ||
`- [About this documentation](${aboutDocUrl})`, | ||
`- [Usage and examples](${usageExamplesUrl})`, | ||
]; | ||
|
||
return introLinks; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.