add JSONLines Loader

This commit is contained in:
ivalkshfoeif 2023-06-21 18:56:37 -07:00
parent 8c880199cd
commit 7ce0f71e2f
2 changed files with 121 additions and 0 deletions

View File

@ -0,0 +1,105 @@
import { INode, INodeData, INodeParams } from '../../../src/Interface'
import { TextSplitter } from 'langchain/text_splitter'
import { JSONLinesLoader } from 'langchain/document_loaders/fs/json'
class Jsonlines_DocumentLoaders implements INode {
label: string
name: string
description: string
type: string
icon: string
category: string
baseClasses: string[]
inputs: INodeParams[]
constructor() {
this.label = 'Json Lines File'
this.name = 'jsonlinesFile'
this.type = 'Document'
this.icon = 'jsonlines.svg'
this.category = 'Document Loaders'
this.description = `Load data from JSON Lines files`
this.baseClasses = [this.type]
this.inputs = [
{
label: 'Jsonlines File',
name: 'jsonlinesFile',
type: 'file',
fileType: '.jsonl'
},
{
label: 'Text Splitter',
name: 'textSplitter',
type: 'TextSplitter',
optional: true
},
{
label: 'Pointer Extraction',
name: 'pointerName',
type: 'string',
description: 'Extracting the pointer',
placeholder: 'Enter pointer name',
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 jsonLinesFileBase64 = nodeData.inputs?.jsonlinesFile as string
const pointerName = nodeData.inputs?.pointerName as string
const metadata = nodeData.inputs?.metadata
let alldocs = []
let files: string[] = []
if (jsonLinesFileBase64.startsWith('[') && jsonLinesFileBase64.endsWith(']')) {
files = JSON.parse(jsonLinesFileBase64)
} else {
files = [jsonLinesFileBase64]
}
for (const file of files) {
const splitDataURI = file.split(',')
splitDataURI.pop()
const bf = Buffer.from(splitDataURI.pop() || '', 'base64')
const blob = new Blob([bf])
const loader = new JSONLinesLoader(blob, pointerName)
if (textSplitter) {
const docs = await loader.loadAndSplit(textSplitter)
alldocs.push(...docs)
} else {
const docs = await loader.load()
alldocs.push(...docs)
}
}
if (metadata) {
const parsedMetadata = typeof metadata === 'object' ? metadata : JSON.parse(metadata)
let finaldocs = []
for (const doc of alldocs) {
const newdoc = {
...doc,
metadata: {
...doc.metadata,
...parsedMetadata
}
}
finaldocs.push(newdoc)
}
return finaldocs
}
return alldocs
}
}
module.exports = { nodeClass: Jsonlines_DocumentLoaders }

View File

@ -0,0 +1,16 @@
<svg width="24" height="24" xmlns="http://www.w3.org/2000/svg">
<!-- Created with Method Draw - http://github.com/duopixel/Method-Draw/ -->
<g>
<title>background</title>
<rect fill="none" id="canvas_background" height="26" width="26" y="-1" x="-1"/>
<g display="none" overflow="visible" y="0" x="0" height="100%" width="100%" id="canvasGrid">
<rect fill="url(#gridpattern)" stroke-width="0" y="0" x="0" height="100%" width="100%"/>
</g>
</g>
<g>
<title>Layer 1</title>
<text font-weight="bold" stroke="#000" transform="matrix(8.682896011956823,0,0,10.412942243751806,-30.866304860177404,-63.784276261342) " xml:space="preserve" text-anchor="start" font-family="Helvetica, Arial, sans-serif" font-size="1" id="svg_2" y="7.062874" x="3.579384" stroke-opacity="null" stroke-width="0" fill="#000000">JSON</text>
<text font-weight="bold" stroke="#000" transform="matrix(9.059566511875573,0,0,9.893934811310315,-1.3962337706973242,-106.08964247698567) " xml:space="preserve" text-anchor="start" font-family="Helvetica, Arial, sans-serif" font-size="1" id="svg_3" y="12.90427" x="0.172236" stroke-opacity="null" stroke-width="0" fill="#000000">Lines</text>
</g>
</svg>

After

Width:  |  Height:  |  Size: 1.2 KiB