Skip to content
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

feat(nx-dev): add embeddings for community plugins #19168

Merged
merged 1 commit into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions nx-dev/util-ai/src/lib/chat-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,15 @@ export function getListOfSources(
return false;
})
.map((section) => {
const url = new URL('https://nx.dev');
url.pathname = section.url_partial as string;
if (section.slug) {
url.hash = section.slug;
let url: URL;
if (section.url_partial?.startsWith('https://')) {
url = new URL(section.url_partial);
} else {
url = new URL('https://nx.dev');
url.pathname = section.url_partial as string;
if (section.slug) {
url.hash = section.slug;
}
}
return {
heading: section.heading,
Expand Down
36 changes: 31 additions & 5 deletions tools/documentation/create-embeddings/src/main.mts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import manifestsExtending from '../../../../docs/generated/manifests/extending-n
import manifestsNx from '../../../../docs/generated/manifests/nx.json' assert { type: 'json' };
import manifestsPackages from '../../../../docs/generated/manifests/packages.json' assert { type: 'json' };
import manifestsTags from '../../../../docs/generated/manifests/tags.json' assert { type: 'json' };
import communityPlugins from '../../../../community/approved-plugins.json' assert { type: 'json' };

let identityMap = {};

Expand Down Expand Up @@ -125,14 +126,16 @@ class MarkdownEmbeddingSource extends BaseEmbeddingSource {
constructor(
source: string,
public filePath: string,
public url_partial: string
public url_partial: string,
public fileContent?: string
) {
const path = filePath.replace(/^docs/, '').replace(/\.md?$/, '');
super(source, path, url_partial);
}

async load() {
const contents = await readFile(this.filePath, 'utf8');
const contents =
this.fileContent ?? (await readFile(this.filePath, 'utf8'));

const { checksum, sections } = processMdxForSearch(contents);

Expand Down Expand Up @@ -209,6 +212,14 @@ async function generateEmbeddings() {
entry.url_partial
);
}),
...createMarkdownForCommunityPlugins().map((content, index) => {
return new MarkdownEmbeddingSource(
'community-plugins',
'/community/approved-plugins.json#' + index,
content.url,
content.text
);
}),
];

console.log(`Discovered ${embeddingSources.length} pages`);
Expand Down Expand Up @@ -307,7 +318,10 @@ async function generateEmbeddings() {

const [responseData] = embeddingResponse.data;

const longer_heading = createLongerHeading(heading, url_partial);
const longer_heading =
source !== 'community-plugins'
? createLongerHeading(heading, url_partial)
: heading;

const { error: insertPageSectionError, data: pageSection } =
await supabaseClient
Expand Down Expand Up @@ -446,22 +460,34 @@ function createLongerHeading(
if (url_partial?.length) {
if (heading?.length && heading !== null && heading !== 'null') {
return `${heading}${` - ${
url_partial.split('/')?.[1]?.[0].toUpperCase() +
url_partial.split('/')?.[1]?.[0]?.toUpperCase() +
url_partial.split('/')?.[1]?.slice(1)
}`}`;
} else {
return url_partial
.split('#')[0]
.split('/')
.map((part) =>
part?.length ? part[0].toUpperCase() + part.slice(1) + ' - ' : ''
part?.length ? part[0]?.toUpperCase() + part.slice(1) + ' - ' : ''
)
.join('')
.slice(0, -3);
}
}
}

function createMarkdownForCommunityPlugins(): {
text: string;
url: string;
}[] {
return communityPlugins.map((plugin) => {
return {
text: `## ${plugin.name} plugin\n\nThere is a ${plugin.name} community plugin.\n\nHere is the description for it: ${plugin.description}\n\nHere is the link to it: [${plugin.url}](${plugin.url})\n\nHere is the list of all the plugins that exist for Nx: https://nx.dev/extending-nx/registry`,
url: plugin.url,
};
});
}

async function main() {
await generateEmbeddings();
}
Expand Down