add plain text doc loader
This commit is contained in:
parent
8a12dc5ff4
commit
14f2dcae6a
|
|
@ -0,0 +1,88 @@
|
||||||
|
import { INode, INodeData, INodeParams } from '../../../src/Interface'
|
||||||
|
import { TextSplitter } from 'langchain/text_splitter'
|
||||||
|
import { Document } from 'langchain/document'
|
||||||
|
|
||||||
|
class PlainText_DocumentLoaders implements INode {
|
||||||
|
label: string
|
||||||
|
name: string
|
||||||
|
version: number
|
||||||
|
description: string
|
||||||
|
type: string
|
||||||
|
icon: string
|
||||||
|
category: string
|
||||||
|
baseClasses: string[]
|
||||||
|
inputs: INodeParams[]
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this.label = 'Plain Text'
|
||||||
|
this.name = 'plainText'
|
||||||
|
this.version = 1.0
|
||||||
|
this.type = 'Document'
|
||||||
|
this.icon = 'plaintext.svg'
|
||||||
|
this.category = 'Document Loaders'
|
||||||
|
this.description = `Load data from plain text`
|
||||||
|
this.baseClasses = [this.type]
|
||||||
|
this.inputs = [
|
||||||
|
{
|
||||||
|
label: 'Text',
|
||||||
|
name: 'text',
|
||||||
|
type: 'string',
|
||||||
|
rows: 4,
|
||||||
|
placeholder:
|
||||||
|
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua...'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'Text Splitter',
|
||||||
|
name: 'textSplitter',
|
||||||
|
type: 'TextSplitter',
|
||||||
|
optional: true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'Metadata',
|
||||||
|
name: 'metadata',
|
||||||
|
type: 'json',
|
||||||
|
optional: true,
|
||||||
|
additionalParams: true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
async init(nodeData: INodeData): Promise<any> {
|
||||||
|
const textSplitter = nodeData.inputs?.textSplitter as TextSplitter
|
||||||
|
const text = nodeData.inputs?.text as string
|
||||||
|
const metadata = nodeData.inputs?.metadata
|
||||||
|
|
||||||
|
let alldocs: Document<Record<string, any>>[] = []
|
||||||
|
|
||||||
|
if (textSplitter) {
|
||||||
|
const docs = await textSplitter.createDocuments([text])
|
||||||
|
alldocs.push(...docs)
|
||||||
|
} else {
|
||||||
|
alldocs.push(
|
||||||
|
new Document({
|
||||||
|
pageContent: text
|
||||||
|
})
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (metadata) {
|
||||||
|
const parsedMetadata = typeof metadata === 'object' ? metadata : JSON.parse(metadata)
|
||||||
|
let finaldocs: Document<Record<string, any>>[] = []
|
||||||
|
for (const doc of alldocs) {
|
||||||
|
const newdoc = {
|
||||||
|
...doc,
|
||||||
|
metadata: {
|
||||||
|
...doc.metadata,
|
||||||
|
...parsedMetadata
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finaldocs.push(newdoc)
|
||||||
|
}
|
||||||
|
return finaldocs
|
||||||
|
}
|
||||||
|
|
||||||
|
return alldocs
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { nodeClass: PlainText_DocumentLoaders }
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-highlight" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||||||
|
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||||||
|
<path d="M3 19h4l10.5 -10.5a2.828 2.828 0 1 0 -4 -4l-10.5 10.5v4"></path>
|
||||||
|
<path d="M12.5 5.5l4 4"></path>
|
||||||
|
<path d="M4.5 13.5l4 4"></path>
|
||||||
|
<path d="M21 15v4h-8l4 -4z"></path>
|
||||||
|
</svg>
|
||||||
|
After Width: | Height: | Size: 482 B |
Loading…
Reference in New Issue