-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #290 from ItzCrazyKns/canary
- Loading branch information
Showing
8 changed files
with
398 additions
and
20 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,81 @@ | ||
import axios from 'axios'; | ||
import { htmlToText } from 'html-to-text' | ||
import { RecursiveCharacterTextSplitter } from 'langchain/text_splitter'; | ||
import { Document } from '@langchain/core/documents'; | ||
import pdfParse from 'pdf-parse' | ||
|
||
export const getDocumentsFromLinks = async ({ links }: { links: string[] }) => { | ||
const splitter = new RecursiveCharacterTextSplitter(); | ||
|
||
let docs: Document[] = []; | ||
|
||
await Promise.all( | ||
links.map(async (link) => { | ||
link = | ||
link.startsWith('http://') || link.startsWith('https://') | ||
? link | ||
: `https://${link}`; | ||
|
||
const res = await axios.get(link, { | ||
responseType: 'arraybuffer', | ||
}); | ||
|
||
const isPdf = res.headers['content-type'] === 'application/pdf'; | ||
|
||
if (isPdf) { | ||
const pdfText = await pdfParse(res.data) | ||
const parsedText = pdfText.text | ||
.replace(/(\r\n|\n|\r)/gm, ' ') | ||
.replace(/\s+/g, ' ') | ||
.trim(); | ||
|
||
const splittedText = await splitter.splitText(parsedText); | ||
const title = 'PDF Document' | ||
|
||
const linkDocs = splittedText.map((text) => { | ||
return new Document({ | ||
pageContent: text, | ||
metadata: { | ||
title: title, | ||
url: link, | ||
}, | ||
}); | ||
}); | ||
|
||
docs.push(...linkDocs); | ||
return; | ||
} | ||
|
||
const parsedText = htmlToText(res.data.toString('utf8'), { | ||
selectors: [ | ||
{ | ||
selector: 'a', | ||
options: { | ||
ignoreHref: true, | ||
} | ||
}, | ||
] | ||
}) | ||
.replace(/(\r\n|\n|\r)/gm, ' ') | ||
.replace(/\s+/g, ' ') | ||
.trim(); | ||
|
||
const splittedText = await splitter.splitText(parsedText); | ||
const title = res.data.toString('utf8').match(/<title>(.*?)<\/title>/)?.[1]; | ||
|
||
const linkDocs = splittedText.map((text) => { | ||
return new Document({ | ||
pageContent: text, | ||
metadata: { | ||
title: title || link, | ||
url: link, | ||
}, | ||
}); | ||
}); | ||
|
||
docs.push(...linkDocs); | ||
}), | ||
); | ||
|
||
return docs; | ||
}; |
This file contains 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,46 @@ | ||
import { BaseOutputParser } from '@langchain/core/output_parsers'; | ||
|
||
interface LineOutputParserArgs { | ||
key?: string; | ||
} | ||
|
||
class LineOutputParser extends BaseOutputParser<string> { | ||
private key = 'questions'; | ||
|
||
constructor(args?: LineOutputParserArgs) { | ||
super(); | ||
this.key = args.key ?? this.key; | ||
} | ||
|
||
static lc_name() { | ||
return 'LineOutputParser'; | ||
} | ||
|
||
lc_namespace = ['langchain', 'output_parsers', 'line_output_parser']; | ||
|
||
async parse(text: string): Promise<string> { | ||
const regex = /^(\s*(-|\*|\d+\.\s|\d+\)\s|\u2022)\s*)+/; | ||
const startKeyIndex = text.indexOf(`<${this.key}>`); | ||
const endKeyIndex = text.indexOf(`</${this.key}>`); | ||
|
||
if (startKeyIndex === -1 || endKeyIndex === -1) { | ||
return ''; | ||
} | ||
|
||
const questionsStartIndex = | ||
startKeyIndex === -1 ? 0 : startKeyIndex + `<${this.key}>`.length; | ||
const questionsEndIndex = endKeyIndex === -1 ? text.length : endKeyIndex; | ||
const line = text | ||
.slice(questionsStartIndex, questionsEndIndex) | ||
.trim() | ||
.replace(regex, ''); | ||
|
||
return line; | ||
} | ||
|
||
getFormatInstructions(): string { | ||
throw new Error('Not implemented.'); | ||
} | ||
} | ||
|
||
export default LineOutputParser; |
This file contains 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 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.