-
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 all 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,51 @@ | ||
import { readFile, writeFile } from 'node:fs/promises'; | ||
import { join } from 'node:path'; | ||
|
||
import { buildApiDocLink } from './utils/buildApiDocLink.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 apiDocsLinks = entries | ||
// Filter non top-level headings | ||
.filter(entry => entry.heading.depth === 1) | ||
.map(entry => `- ${buildApiDocLink(entry)}`) | ||
.join('\n'); | ||
|
||
const filledTemplate = `${template}${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,7 @@ | ||
# 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. | ||
|
||
Below are the sections of the API documentation. Look out especially towards the links that point towards guidance/introductioon to the structure of this documentation. | ||
|
||
## API Documentations |
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,49 @@ | ||
import { BASE_URL } from '../../../constants.mjs'; | ||
import { transformNodeToString } from '../../../utils/unist.mjs'; | ||
|
||
/** | ||
* Retrieves the description of a given API doc entry. It first checks whether | ||
* the entry has a llm_description property. If not, it extracts the first | ||
* paragraph from the entry's content. | ||
* | ||
* @param {ApiDocMetadataEntry} entry | ||
* @returns {string} | ||
*/ | ||
const getEntryDescription = entry => { | ||
if (entry.llm_description) { | ||
return entry.llm_description; | ||
} | ||
|
||
const descriptionNode = entry.content.children.find( | ||
child => child.type === 'paragraph' | ||
); | ||
|
||
if (!descriptionNode) { | ||
return ''; | ||
} | ||
|
||
return ( | ||
transformNodeToString(descriptionNode) | ||
// Remove newlines and extra spaces | ||
.replace(/[\r\n]+/g, '') | ||
); | ||
}; | ||
|
||
/** | ||
* 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; | ||
|
||
const path = entry.api_doc_source.replace(/^doc\//, '/docs/latest/'); | ||
const url = new URL(path, BASE_URL); | ||
|
||
const link = `[${title}](${url})`; | ||
|
||
const description = getEntryDescription(entry); | ||
|
||
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
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,47 @@ | ||
import { LINT_MESSAGES } from '../constants.mjs'; | ||
|
||
/** | ||
* Checks if a top-level entry is missing a llm_description field or a paragraph | ||
* node. | ||
* | ||
* @param {ApiDocMetadataEntry[]} entries | ||
* @returns {Array<import('../types.d.ts').LintIssue>} | ||
*/ | ||
export const missingLlmDescription = entries => { | ||
return entries | ||
.filter(entry => { | ||
// Only process top-level headings | ||
if (entry.heading.depth !== 1) { | ||
return false; | ||
} | ||
|
||
// Skip entries that have an llm_description property | ||
if (entry.llm_description !== undefined) { | ||
return false; | ||
} | ||
|
||
const hasParagraph = entry.content.children.some( | ||
node => node.type === 'paragraph' | ||
); | ||
|
||
// Skip entries that contain a paragraph that can be used as a fallback. | ||
if (hasParagraph) { | ||
return false; | ||
} | ||
|
||
return true; | ||
}) | ||
.map(entry => mapToMissingEntryWarning(entry)); | ||
}; | ||
|
||
/** | ||
* Maps a entry to a warning for missing llm description. | ||
* | ||
* @param {ApiDocMetadataEntry} entry | ||
* @returns {import('../types.d.ts').LintIssue} | ||
*/ | ||
const mapToMissingEntryWarning = entry => ({ | ||
level: 'warn', | ||
message: LINT_MESSAGES.missingLlmDescription, | ||
location: { path: entry.api_doc_source }, | ||
}); |
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
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.