@@ -12,6 +12,7 @@ export function getTokenCount(input: string): number {
1212 return encode ( input ) . length
1313}
1414
15+
1516export function splitPrompt (
1617 maxTokens : number ,
1718 prompt : string
@@ -22,24 +23,24 @@ export function splitPrompt(
2223 const promptPieces : string [ ] = [ ]
2324 let remainingPrompt = prompt
2425 while ( remainingPrompt . length > 0 ) {
25- if ( remainingPrompt . length > maxTokens && ! remainingPrompt . includes ( ' ' ) ) {
26- const piece = remainingPrompt . substring ( 0 , maxTokens ) . trim ( ) ;
27- promptPieces . push ( piece ) ;
28- remainingPrompt = remainingPrompt . substring ( maxTokens ) . trim ( ) ;
29- }
30- const lastSpaceIndex = remainingPrompt . lastIndexOf ( ' ' , maxTokens )
31- if ( lastSpaceIndex >= 0 ) {
32- // Split at the last space
33- const piece = remainingPrompt . substring ( 0 , lastSpaceIndex ) . trim ( )
34- promptPieces . push ( piece )
35- remainingPrompt = remainingPrompt . substring ( lastSpaceIndex ) . trim ( )
26+ let piece ;
27+ if ( remainingPrompt . length > maxTokens && ! remainingPrompt . includes ( ' ' ) ) {
28+ piece = remainingPrompt . substring ( 0 , maxTokens ) . trim ( ) ;
3629 } else {
37- // If no space found in the next `maxTokens` characters, split at `maxTokens` directly
38- const piece = remainingPrompt . substring ( 0 , maxTokens ) . trim ( )
39- promptPieces . push ( piece )
40- remainingPrompt = remainingPrompt . substring ( maxTokens ) . trim ( )
30+ const lastSpaceIndex = remainingPrompt . lastIndexOf ( ' ' , maxTokens )
31+ if ( lastSpaceIndex >= 0 ) {
32+ // Split at the last space
33+ piece = remainingPrompt . substring ( 0 , lastSpaceIndex ) . trim ( )
34+ } else {
35+ // If no space found in the next `maxTokens` characters, split at `maxTokens` directly
36+ piece = remainingPrompt . substring ( 0 , maxTokens ) . trim ( )
37+ }
4138 }
39+ promptPieces . push ( piece )
40+ remainingPrompt = remainingPrompt . substring ( piece . length ) . trim ( )
4241 }
4342
4443 return promptPieces
4544}
45+
46+
0 commit comments