Skip to content

Commit fe9c63a

Browse files
authored
Merge pull request FlowiseAI#1011 from FlowiseAI/feature/PlainTextDocLoader
Feature/PlainText DocLoader
2 parents 99dbe52 + 305b365 commit fe9c63a

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { INode, INodeData, INodeParams } from '../../../src/Interface'
2+
import { TextSplitter } from 'langchain/text_splitter'
3+
import { Document } from 'langchain/document'
4+
5+
class PlainText_DocumentLoaders implements INode {
6+
label: string
7+
name: string
8+
version: number
9+
description: string
10+
type: string
11+
icon: string
12+
category: string
13+
baseClasses: string[]
14+
inputs: INodeParams[]
15+
16+
constructor() {
17+
this.label = 'Plain Text'
18+
this.name = 'plainText'
19+
this.version = 1.0
20+
this.type = 'Document'
21+
this.icon = 'plaintext.svg'
22+
this.category = 'Document Loaders'
23+
this.description = `Load data from plain text`
24+
this.baseClasses = [this.type]
25+
this.inputs = [
26+
{
27+
label: 'Text',
28+
name: 'text',
29+
type: 'string',
30+
rows: 4,
31+
placeholder:
32+
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua...'
33+
},
34+
{
35+
label: 'Text Splitter',
36+
name: 'textSplitter',
37+
type: 'TextSplitter',
38+
optional: true
39+
},
40+
{
41+
label: 'Metadata',
42+
name: 'metadata',
43+
type: 'json',
44+
optional: true,
45+
additionalParams: true
46+
}
47+
]
48+
}
49+
50+
async init(nodeData: INodeData): Promise<any> {
51+
const textSplitter = nodeData.inputs?.textSplitter as TextSplitter
52+
const text = nodeData.inputs?.text as string
53+
const metadata = nodeData.inputs?.metadata
54+
55+
let alldocs: Document<Record<string, any>>[] = []
56+
57+
if (textSplitter) {
58+
const docs = await textSplitter.createDocuments([text])
59+
alldocs.push(...docs)
60+
} else {
61+
alldocs.push(
62+
new Document({
63+
pageContent: text
64+
})
65+
)
66+
}
67+
68+
if (metadata) {
69+
const parsedMetadata = typeof metadata === 'object' ? metadata : JSON.parse(metadata)
70+
let finaldocs: Document<Record<string, any>>[] = []
71+
for (const doc of alldocs) {
72+
const newdoc = {
73+
...doc,
74+
metadata: {
75+
...doc.metadata,
76+
...parsedMetadata
77+
}
78+
}
79+
finaldocs.push(newdoc)
80+
}
81+
return finaldocs
82+
}
83+
84+
return alldocs
85+
}
86+
}
87+
88+
module.exports = { nodeClass: PlainText_DocumentLoaders }
Loading

0 commit comments

Comments
 (0)