File size: 2,898 Bytes
4114d85 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
import { INode, INodeData, INodeParams } from '../../../src/Interface'
import { getBaseClasses } from '../../../src/utils'
import { TokenTextSplitter, TokenTextSplitterParams } from 'langchain/text_splitter'
import { TiktokenEncoding } from '@dqbd/tiktoken'
class TokenTextSplitter_TextSplitters implements INode {
label: string
name: string
description: string
type: string
icon: string
category: string
baseClasses: string[]
inputs: INodeParams[]
constructor() {
this.label = 'Token Text Splitter'
this.name = 'tokenTextSplitter'
this.type = 'TokenTextSplitter'
this.icon = 'tiktoken.svg'
this.category = 'Text Splitters'
this.description = `Splits a raw text string by first converting the text into BPE tokens, then split these tokens into chunks and convert the tokens within a single chunk back into text.`
this.baseClasses = [this.type, ...getBaseClasses(TokenTextSplitter)]
this.inputs = [
{
label: 'Encoding Name',
name: 'encodingName',
type: 'options',
options: [
{
label: 'gpt2',
name: 'gpt2'
},
{
label: 'r50k_base',
name: 'r50k_base'
},
{
label: 'p50k_base',
name: 'p50k_base'
},
{
label: 'p50k_edit',
name: 'p50k_edit'
},
{
label: 'cl100k_base',
name: 'cl100k_base'
}
],
default: 'gpt2'
},
{
label: 'Chunk Size',
name: 'chunkSize',
type: 'number',
default: 1000,
optional: true
},
{
label: 'Chunk Overlap',
name: 'chunkOverlap',
type: 'number',
optional: true
}
]
}
async init(nodeData: INodeData): Promise<any> {
const encodingName = nodeData.inputs?.encodingName as string
const chunkSize = nodeData.inputs?.chunkSize as string
const chunkOverlap = nodeData.inputs?.chunkOverlap as string
const obj = {} as TokenTextSplitterParams
obj.encodingName = encodingName as TiktokenEncoding
if (chunkSize) obj.chunkSize = parseInt(chunkSize, 10)
if (chunkOverlap) obj.chunkOverlap = parseInt(chunkOverlap, 10)
const splitter = new TokenTextSplitter(obj)
return splitter
}
}
module.exports = { nodeClass: TokenTextSplitter_TextSplitters }
|