| |
| import { NodeHtmlMarkdown } from "node-html-markdown"; |
| import { HuggingFaceTransformersEmbeddings } from "langchain/embeddings/hf_transformers"; |
| import * as cheerio from "cheerio"; |
| import { promises as fs } from "fs"; |
| import path from "path"; |
|
|
| export default async function RUN() { |
| const htmlText = await fs.readFile( |
| path.join(process.cwd(), "data", "swiss-code-of-obligations.html"), |
| "utf8" |
| ); |
|
|
| const $ = cheerio.load(htmlText); |
|
|
| const articles = []; |
| const $articles = $("article"); |
|
|
| $articles |
| |
| |
| .each((_, article) => { |
| const $article = $(article); |
|
|
| const parentHeading = $article.parent().parent().find(".heading").first(); |
| const parentHeadingLevel = Number(parentHeading.attr("aria-level")); |
|
|
| const headings = []; |
| let parent = $article; |
|
|
| for (let index = parentHeadingLevel; index >= 1; index--) { |
| parent = parent.parent().parent(); |
|
|
| const heading = parent.find(".heading").first(); |
|
|
| headings.push( |
| heading |
| .text() |
| .replace(/[\n\s]+/g, " ") |
| .trim() |
| ); |
| } |
|
|
| const structuredData = { |
| headings: headings.reverse(), |
| article: $article |
| .find(".heading > a") |
| .text() |
| .trim() |
| .replace(/\s+/g, " "), |
| link: $article.find("[routerlink]").attr("href") || "", |
| content: NodeHtmlMarkdown.translate( |
| $article.find(".collapseable").html() || "" |
| ).trim(), |
| contentHTML: $article.find(".collapseable").html() || "", |
| }; |
| articles.push(structuredData); |
| }); |
|
|
| const embeddings = new HuggingFaceTransformersEmbeddings({ |
| modelName: "Xenova/paraphrase-multilingual-mpnet-base-v2", |
| |
| }); |
|
|
| |
|
|
| const articleChunks = chunkArray(articles, 3); |
|
|
| let index = 0; |
|
|
| for (const articles of articleChunks) { |
| const articlesText = articles.map( |
| (x) => |
| x.headings |
| .map((heading, i) => { |
| if (i < 5) { |
| const headingMarkup = "#".repeat(i); |
| return headingMarkup + " " + heading; |
| } |
|
|
| return `**${heading}**`; |
| }) |
| .join("\n") + |
| "\n" + |
| htmlToMarkdown(x.contentHTML).trim() |
| ); |
|
|
| const documentEmbeddings = await embeddings.embedDocuments(articlesText); |
|
|
| const points = documentEmbeddings.map((vector, i) => { |
| index++; |
| const title = articles[i].headings.slice(-1)[0]; |
|
|
| const { contentHTML: _, ...payload } = articles[i]; |
|
|
| articles[i].vector = vector; |
|
|
| return { |
| id: index, |
| vector, |
| payload: { |
| title, |
| ...payload, |
| }, |
| }; |
| }); |
|
|
| |
| |
| |
| |
| |
| } |
|
|
| return articles |
| .map(({ contentHTML: _, ...a }) => a) |
| .map((x) => |
| JSON.stringify( |
| x |
| |
| ) |
| ) |
| .join("\n"); |
| } |
|
|
| function chunkArray(array = [], chunkSize) { |
| const chunks = []; |
| for (let i = 0; i < array.length; i += chunkSize) { |
| chunks.push(array.slice(i, i + chunkSize)); |
| } |
| return chunks; |
| } |
|
|
| const result = await RUN(); |
|
|
| console.log(result); |
|
|
| export function htmlToMarkdown(htmlContent) { |
| return ( |
| NodeHtmlMarkdown.translate(htmlContent, { |
| maxConsecutiveNewlines: 3, |
| useLinkReferenceDefinitions: true, |
| }) |
| |
| .replace(/!\[([^\]]*)\]\([^)]+\)\n*/g, (match, altText) => altText) |
| .replace(/[ ]+/g, " ") |
| .replace(/\[([^\]]+)\]\[\d+\]/g, (_, x) => x.trim()) |
| .replace(/\[[\d]+\]: .*\n*/g, "") |
| ); |
| } |
|
|