File tree Expand file tree Collapse file tree 1 file changed +15
-4
lines changed
Expand file tree Collapse file tree 1 file changed +15
-4
lines changed Original file line number Diff line number Diff line change 44 * SPDX-License-Identifier: Apache-2.0
55 */
66
7+ import { readFileSync } from 'node:fs' ;
78import { parseArgs } from 'node:util' ;
89
910import { GoogleGenAI } from '@google/genai' ;
@@ -16,18 +17,28 @@ const {values, positionals} = parseArgs({
1617 type : 'string' ,
1718 default : 'gemini-2.5-flash' ,
1819 } ,
20+ file : {
21+ type : 'string' ,
22+ short : 'f' ,
23+ } ,
1924 } ,
2025 allowPositionals : true ,
2126} ) ;
2227
23- if ( ! positionals [ 0 ] ) {
24- console . error ( 'Usage: npm run count-tokens -- -- <text>' ) ;
28+ let contents = positionals [ 0 ] ;
29+
30+ if ( values . file ) {
31+ contents = readFileSync ( values . file , 'utf8' ) ;
32+ }
33+
34+ if ( ! contents ) {
35+ console . error ( 'Usage: npm run count-tokens -- [-f <file>] [<text>]' ) ;
2536 process . exit ( 1 ) ;
2637}
2738
2839const response = await ai . models . countTokens ( {
2940 model : values . model ,
30- contents : positionals [ 0 ] ,
41+ contents,
3142} ) ;
32- console . log ( `Input: ${ positionals [ 0 ] } ` ) ;
43+ console . log ( `Input: ${ values . file || positionals [ 0 ] } ` ) ;
3344console . log ( `Tokens: ${ response . totalTokens } ` ) ;
You can’t perform that action at this time.
0 commit comments