2024-02-02 22:01:16 +05:30
|
|
|
import { BaseDocumentLoader } from "langchain/document_loaders/base"
|
2024-02-03 17:51:11 +05:30
|
|
|
import { Document } from "@langchain/core/documents"
|
2024-02-02 22:01:16 +05:30
|
|
|
import { compile } from "html-to-text"
|
2024-02-25 00:12:46 +05:30
|
|
|
import { chromeRunTime } from "~libs/runtime"
|
2024-03-08 00:45:28 +05:30
|
|
|
import { YtTranscript } from "yt-transcript"
|
|
|
|
|
|
|
|
|
|
const YT_REGEX =
|
|
|
|
|
/(?:https?:\/\/)?(?:www\.)?(?:youtube\.com|youtu\.be)\/(?:watch\?v=)?([a-zA-Z0-9_-]+)/
|
|
|
|
|
|
|
|
|
|
const isYoutubeLink = (url: string) => {
|
|
|
|
|
return YT_REGEX.test(url)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const getTranscript = async (url: string) => {
|
|
|
|
|
const ytTranscript = new YtTranscript({ url })
|
|
|
|
|
return await ytTranscript.getTranscript()
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-02 22:01:16 +05:30
|
|
|
|
|
|
|
|
export interface WebLoaderParams {
|
|
|
|
|
html: string
|
|
|
|
|
url: string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class PageAssistHtmlLoader
|
|
|
|
|
extends BaseDocumentLoader
|
2024-02-25 18:44:47 +05:30
|
|
|
implements WebLoaderParams {
|
2024-02-02 22:01:16 +05:30
|
|
|
html: string
|
|
|
|
|
url: string
|
|
|
|
|
|
|
|
|
|
constructor({ html, url }: WebLoaderParams) {
|
|
|
|
|
super()
|
|
|
|
|
this.html = html
|
|
|
|
|
this.url = url
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async load(): Promise<Document<Record<string, any>>[]> {
|
2024-03-08 00:45:28 +05:30
|
|
|
if (isYoutubeLink(this.url)) {
|
|
|
|
|
const transcript = await getTranscript(this.url)
|
|
|
|
|
if (!transcript) {
|
|
|
|
|
throw new Error("Transcript not found for this video.")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let text = ""
|
|
|
|
|
|
|
|
|
|
transcript.forEach((item) => {
|
|
|
|
|
text += item.text + " "
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
{
|
|
|
|
|
metadata: {
|
|
|
|
|
source: this.url,
|
|
|
|
|
audio: { chunks: transcript }
|
|
|
|
|
},
|
|
|
|
|
pageContent: text
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}
|
2024-02-02 22:01:16 +05:30
|
|
|
const htmlCompiler = compile({
|
|
|
|
|
wordwrap: false
|
|
|
|
|
})
|
|
|
|
|
const text = htmlCompiler(this.html)
|
|
|
|
|
const metadata = { source: this.url }
|
|
|
|
|
return [new Document({ pageContent: text, metadata })]
|
|
|
|
|
}
|
2024-02-25 18:44:47 +05:30
|
|
|
|
|
|
|
|
async loadByURL(): Promise<Document<Record<string, any>>[]> {
|
2024-03-08 00:45:28 +05:30
|
|
|
if (isYoutubeLink(this.url)) {
|
|
|
|
|
const transcript = await getTranscript(this.url)
|
|
|
|
|
if (!transcript) {
|
|
|
|
|
throw new Error("Transcript not found for this video.")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let text = ""
|
|
|
|
|
|
|
|
|
|
transcript.forEach((item) => {
|
|
|
|
|
text += item.text + " "
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
{
|
|
|
|
|
metadata: {
|
|
|
|
|
source: this.url,
|
|
|
|
|
audio: { chunks: transcript }
|
|
|
|
|
},
|
|
|
|
|
pageContent: text
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}
|
2024-02-25 18:44:47 +05:30
|
|
|
await chromeRunTime(this.url)
|
|
|
|
|
const fetchHTML = await fetch(this.url)
|
|
|
|
|
const html = await fetchHTML.text()
|
|
|
|
|
const htmlCompiler = compile({
|
|
|
|
|
wordwrap: false,
|
|
|
|
|
selectors: [
|
|
|
|
|
{ selector: "img", format: "skip" },
|
|
|
|
|
{ selector: "script", format: "skip" }
|
|
|
|
|
]
|
|
|
|
|
})
|
|
|
|
|
const text = htmlCompiler(html)
|
2024-02-25 21:17:27 +05:30
|
|
|
const metadata = { url: this.url }
|
2024-02-25 18:44:47 +05:30
|
|
|
return [new Document({ pageContent: text, metadata })]
|
|
|
|
|
}
|
2024-02-02 22:01:16 +05:30
|
|
|
}
|