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

(EAI-375): Ingest snooty docs facets and meta #558

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions examples/quick-start/packages/ingest/src/ingest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,10 @@ export default {

return [mongodbChatbotFrameworkSource];
},
concurrencyOptions: () => ({
embed: {
createChunks: 5,
processPages: 2,
},
}),
} satisfies Config;
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { createInterface } from "readline";
import { Page, PageFormat, logger } from "mongodb-rag-core";
import fetch from "node-fetch";
import { DataSource, ProjectBase } from "mongodb-rag-core/dataSources";
import { snootyAstToMd, getTitleFromSnootyAst } from "./snootyAstToMd";
import {
snootyAstToMd,
getTitleFromSnootyAst,
getMetadataFromSnootyAst,
} from "./snootyAstToMd";
import {
getTitleFromSnootyOpenApiSpecAst,
snootyAstToOpenApiSpec,
Expand Down Expand Up @@ -49,6 +53,35 @@ export type SnootyTextNode = SnootyNode & {
value: string;
};

export type SnootyFacetNode = SnootyNode & {
type: "directive";
name: "facet";
children: never;
options?: {
name: string;
values: string;
};
};

export type SnootyMetaNode = SnootyNode & {
type: "directive";
name: "meta";
children: never;
options?: {
/**
List of relevant keywords for the page, comma separated.
@example "code example, node.js, analyze, array"
*/
keywords?: string;

/**
High-level description of the page.
*/
description: string;
[key: string]: string | undefined;
};
};

/**
A page in the Snooty manifest.
*/
Expand Down Expand Up @@ -328,6 +361,7 @@ export const handlePage = async (
body = snootyAstToMd(page.ast);
title = getTitleFromSnootyAst(page.ast);
}
const metadata = getMetadataFromSnootyAst(page.ast);

return {
url: new URL(pagePath, baseUrl.replace(/\/?$/, "/")).href.replace(
Expand All @@ -339,6 +373,7 @@ export const handlePage = async (
body,
format,
metadata: {
...metadata,
tags,
productName,
version,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import Path from "path";
import fs from "fs";
import { snootyAstToMd, getTitleFromSnootyAst } from "./snootyAstToMd";
import {
snootyAstToMd,
getTitleFromSnootyAst,
getMetadataFromSnootyAst,
} from "./snootyAstToMd";
import { SnootyNode } from "./SnootyDataSource";
import { rstToSnootyAst } from "./rstToSnootyAst";

Expand Down Expand Up @@ -300,3 +304,33 @@ describe("getTitleFromSnootyAst", () => {
);
});
});

describe("getMetadataFromSnootyAst", () => {
const sampleMetadataPage = JSON.parse(
fs.readFileSync(
Path.resolve(SRC_ROOT, "../testData/samplePageWithMetadata.json"),
{
encoding: "utf-8",
}
)
);
it("extracts meta directives", () => {
const metadata = getMetadataFromSnootyAst(sampleMetadataPage.data.ast);
expect(metadata).toMatchObject({
pageDescription: expect.any(String),
});
});
it("extracts meta.keyword directives as string[]", () => {
const metadata = getMetadataFromSnootyAst(sampleMetadataPage.data.ast);
expect(metadata).toMatchObject({
pageKeywords: expect.arrayContaining([expect.any(String)]),
});
});
it("extracts facet directives", () => {
const metadata = getMetadataFromSnootyAst(sampleMetadataPage.data.ast);
expect(metadata).toMatchObject({
pageGenre: "tutorial",
pageFoo: "bar",
});
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { SnootyNode, SnootyTextNode } from "./SnootyDataSource";
import {
SnootyFacetNode,
SnootyMetaNode,
SnootyNode,
SnootyTextNode,
} from "./SnootyDataSource";
import { strict as assert } from "assert";
import { renderSnootyTable } from "./renderSnootyTable";

Expand Down Expand Up @@ -204,3 +209,59 @@ export const getTitleFromSnootyAst = (node: SnootyNode): string | undefined => {
) as SnootyTextNode[];
return textNodes.map(({ value }) => value).join("");
};

export const getMetadataFromSnootyAst = (
node: SnootyNode
): Record<string, unknown> => {
const facetAndMetaNodes = findAll(
node,
({ name }) => name === "facet" || name === "meta"
) as (SnootyFacetNode | SnootyMetaNode)[];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works for now. Longer term if we plan to maintain this Snooty AST glue code then it might be nice to move to Zod parsing for these instead of type casts.


const facetNodes = facetAndMetaNodes.filter(
(n) => n.name === "facet"
) as SnootyFacetNode[];
const metaNodes = facetAndMetaNodes.filter(
(n) => n.name === "meta"
) as SnootyMetaNode[];

const keyPrefix = "page";

const facets = facetNodes.reduce((acc, facetNode) => {
if (!facetNode.options) {
return acc;
}
const { name, values } = facetNode.options;
if (!name || !values) {
return acc;
}
acc[createKeyName(name, keyPrefix)] = values;
return acc;
}, {} as Record<string, string>);

const meta = metaNodes.reduce((acc, metaNode) => {
if (!metaNode.options) {
return acc;
}
const metaEntries = Object.entries(metaNode.options);
for (const [key, value] of metaEntries) {
if (key === "keywords" && value) {
acc[createKeyName(key, keyPrefix)] = value
.split(",")
.map((s) => s.trim());
} else if (key === "description" && value) {
acc[createKeyName(key, keyPrefix)] = value;
}
}

return acc;
}, {} as Record<string, string | string[]>);
return {
...facets,
...meta,
};
};

function createKeyName(key: string, prefix = "") {
return prefix + key.charAt(0).toUpperCase() + key.slice(1);
}
Loading