@@ -13,10 +13,12 @@ import {documentationPageUrls} from "../../../../config.js";
1313import withOra from "../../../../utils/withOra.js" ;
1414import { resolveModelDestination } from "../../../../utils/resolveModelDestination.js" ;
1515import { printModelDestination } from "../../../utils/printModelDestination.js" ;
16+ import { getGgufMetadataKeyValue } from "../../../../gguf/utils/getGgufMetadataKeyValue.js" ;
1617
1718type InspectGgufCommand = {
1819 modelPath : string ,
1920 header ?: string [ ] ,
21+ key ?: string ,
2022 noSplice : boolean ,
2123 fullTensorInfo : boolean ,
2224 fullMetadataArrays : boolean ,
@@ -46,6 +48,12 @@ export const InspectGgufCommand: CommandModule<object, InspectGgufCommand> = {
4648 description : "Headers to use when reading a model file from a URL, in the format `key: value`. You can pass this option multiple times to add multiple headers." ,
4749 group : "Optional:"
4850 } )
51+ . option ( "key" , {
52+ alias : [ "k" ] ,
53+ type : "string" ,
54+ description : "A single metadata key to print the value of. If not provided, all metadata will be printed" ,
55+ group : "Optional:"
56+ } )
4957 . option ( "noSplice" , {
5058 alias : "s" ,
5159 type : "boolean" ,
@@ -80,7 +88,7 @@ export const InspectGgufCommand: CommandModule<object, InspectGgufCommand> = {
8088 } ) ;
8189 } ,
8290 async handler ( {
83- modelPath : ggufPath , header : headerArg , noSplice, fullTensorInfo, fullMetadataArrays, plainJson, outputToJsonFile
91+ modelPath : ggufPath , header : headerArg , key , noSplice, fullTensorInfo, fullMetadataArrays, plainJson, outputToJsonFile
8492 } : InspectGgufCommand ) {
8593 const resolvedModelDestination = resolveModelDestination ( ggufPath ) ;
8694 const resolvedGgufPath = resolvedModelDestination . type == "file"
@@ -116,16 +124,30 @@ export const InspectGgufCommand: CommandModule<object, InspectGgufCommand> = {
116124 const fileTypeName = getGgufFileTypeName ( parsedMetadata . metadata . general ?. file_type ) ;
117125
118126 if ( plainJson || outputToJsonFile != null ) {
119- const outputJson = JSON . stringify ( {
120- splicedParts : parsedMetadata . splicedParts ,
121- version : parsedMetadata . version ,
122- fileType : fileTypeName ,
123- tensorCount : parsedMetadata . totalTensorCount ,
124- metadataSize : parsedMetadata . totalMetadataSize ,
125- tensorInfoSize : parsedMetadata . totalTensorInfoSize ,
126- metadata : parsedMetadata . metadata ,
127- tensorInfo : parsedMetadata . fullTensorInfo
128- } , undefined , 4 ) ;
127+ const getOutputJson = ( ) => {
128+ if ( key != null ) {
129+ const keyValue = getGgufMetadataKeyValue ( parsedMetadata . metadata , key ) ;
130+ if ( keyValue === undefined ) {
131+ console . log ( `Key not found: ${ key } ` ) ;
132+ process . exit ( 1 ) ;
133+ }
134+
135+ return JSON . stringify ( keyValue , undefined , 4 ) ;
136+ }
137+
138+ return JSON . stringify ( {
139+ splicedParts : parsedMetadata . splicedParts ,
140+ version : parsedMetadata . version ,
141+ fileType : fileTypeName ,
142+ tensorCount : parsedMetadata . totalTensorCount ,
143+ metadataSize : parsedMetadata . totalMetadataSize ,
144+ tensorInfoSize : parsedMetadata . totalTensorInfoSize ,
145+ metadata : parsedMetadata . metadata ,
146+ tensorInfo : parsedMetadata . fullTensorInfo
147+ } , undefined , 4 ) ;
148+ } ;
149+
150+ const outputJson = getOutputJson ( ) ;
129151
130152 if ( outputToJsonFile != null ) {
131153 const filePath = path . resolve ( process . cwd ( ) , outputToJsonFile ) ;
@@ -134,6 +156,27 @@ export const InspectGgufCommand: CommandModule<object, InspectGgufCommand> = {
134156 } else {
135157 console . info ( outputJson ) ;
136158 }
159+ } else if ( key != null ) {
160+ const keyValue = getGgufMetadataKeyValue ( parsedMetadata . metadata , key ) ;
161+ if ( keyValue === undefined ) {
162+ console . log ( `${ chalk . red ( "Metadata key not found:" ) } ${ key } ` ) ;
163+ process . exit ( 1 ) ;
164+ }
165+
166+ const metadataPrettyPrintOptions : PrettyPrintObjectOptions = {
167+ maxArrayValues : fullMetadataArrays
168+ ? undefined
169+ : 10 ,
170+ useNumberGrouping : true ,
171+ maxArrayItemsWidth : process . stdout . columns - 1
172+ } ;
173+
174+ console . info ( `${ chalk . yellow ( "Metadata key:" ) } ${ prettyPrintObject ( key ) } ` ) ;
175+ console . info ( `${ chalk . yellow ( "Metadata:" ) } ${
176+ typeof keyValue === "string"
177+ ? keyValue
178+ : prettyPrintObject ( keyValue , undefined , metadataPrettyPrintOptions )
179+ } `) ;
137180 } else {
138181 const metadataPrettyPrintOptions : PrettyPrintObjectOptions = {
139182 maxArrayValues : fullMetadataArrays
0 commit comments