( text: string, chunkSize: number, overlap: number, )
| 141 | } |
| 142 | |
| 143 | export function chunkString( |
| 144 | text: string, |
| 145 | chunkSize: number, |
| 146 | overlap: number, |
| 147 | ): string[] { |
| 148 | /** Splits a long string into chunks of length `chunkSize`, with overlap `overlap` |
| 149 | * Useful for splitting up long strings for GPT. **/ |
| 150 | const encoding = tokenizer.encode(text); |
| 151 | console.log("Chunking string. Number of tokens = " + encoding.length); |
| 152 | |
| 153 | const chunks = []; |
| 154 | let i = 0; |
| 155 | let start, |
| 156 | end = 0; |
| 157 | while (end < encoding.length) { |
| 158 | start = i * (chunkSize - overlap); |
| 159 | end = start + chunkSize; |
| 160 | |
| 161 | chunks.push(tokenizer.decode(encoding.slice(start, end))); |
| 162 | i++; |
| 163 | } |
| 164 | console.log("Outputting " + chunks.length + " chunks of length " + chunkSize); |
| 165 | return chunks; |
| 166 | } |
| 167 | |
| 168 | export function objectNotEmpty(obj: Object): boolean { |
| 169 | return Object.keys(obj).length > 0; |
no outgoing calls
no test coverage detected