Skip to content

Commit 31bac8b

Browse files
committed
refactor options
1 parent 5f547dd commit 31bac8b

File tree

3 files changed

+17
-16
lines changed

3 files changed

+17
-16
lines changed

packages/gguf/src/gguf.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ export async function gguf(
273273
offset += tensorCount.length;
274274
const numKv = readVersionedSize(r.view, offset, version, littleEndian);
275275
offset += numKv.length;
276-
const metadata: GGUFMetadata = {
276+
const metadata: GGUFMetadata<{ strict: false }> = {
277277
version,
278278
tensor_count: tensorCount.value,
279279
kv_count: numKv.value,
@@ -308,9 +308,6 @@ export async function gguf(
308308
}
309309
}
310310
offset += valueResult.length;
311-
/// TODO(fix typing)
312-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
313-
// @ts-ignore
314311
metadata[keyResult.value] = valueResult.value;
315312
}
316313

packages/gguf/src/types.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
import { describe, it } from "vitest";
22
import type { gguf } from "./gguf";
3-
import type { GGUFMetadata, GGUFParseOutput, GGUFType } from "./types";
3+
import type { GGUFMetadata, GGUFParseOutput } from "./types";
44

55
describe("gguf-types", () => {
66
it("gguf() type can be casted between STRICT and NON_STRICT (at compile time)", async () => {
77
// eslint-disable-next-line @typescript-eslint/no-explicit-any
88
const result: Awaited<ReturnType<typeof gguf>> = null as any;
9-
const strictType = result as GGUFParseOutput<GGUFType.STRICT>;
9+
const strictType = result as GGUFParseOutput<{ strict: true }>;
1010
// @ts-expect-error because the key "abc" does not exist
1111
strictType.metadata.abc = 123;
12-
const nonStrictType = result as GGUFParseOutput<GGUFType.NON_STRICT>;
12+
const nonStrictType = result as GGUFParseOutput<{ strict: false }>;
1313
nonStrictType.metadata.abc = 123; // PASS, because it can be anything
1414
// @ts-expect-error because ArrayBuffer is not a MetadataValue
1515
nonStrictType.metadata.fff = ArrayBuffer;
1616
});
1717

1818
it("GGUFType.NON_STRICT should be correct (at compile time)", async () => {
1919
// eslint-disable-next-line @typescript-eslint/no-explicit-any
20-
const model: GGUFMetadata<GGUFType.NON_STRICT> = null as any;
20+
const model: GGUFMetadata<{ strict: false }> = null as any;
2121
model.kv_count = 123n;
2222
model.abc = 456; // PASS, because it can be anything
2323
});
2424

2525
it("GGUFType.STRICT should be correct (at compile time)", async () => {
2626
// eslint-disable-next-line @typescript-eslint/no-explicit-any
27-
const model: GGUFMetadata<GGUFType.STRICT> = null as any;
27+
const model: GGUFMetadata<{ strict: true }> = null as any;
2828

2929
if (model["general.architecture"] === "whisper") {
3030
model["encoder.whisper.block_count"] = 0;

packages/gguf/src/types.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,21 @@ export type Whisper = GGUFGeneralInfo<"whisper"> &
110110

111111
/// Types for parse output
112112

113-
export enum GGUFType {
114-
STRICT,
115-
NON_STRICT,
113+
export interface GGUFMetadataOptions {
114+
/**
115+
* Enable strict type for known GGUF fields.
116+
*
117+
* @default true
118+
*/
119+
strict: boolean;
116120
}
117121

118-
export type GGUFMetadata<TGGUFType extends GGUFType = GGUFType.STRICT> = {
122+
export type GGUFMetadata<Options extends GGUFMetadataOptions = { strict: true }> = {
119123
version: Version;
120124
tensor_count: bigint;
121125
kv_count: bigint;
122126
} & GGUFModelKV &
123-
(TGGUFType extends GGUFType.STRICT ? unknown : Record<string, MetadataValue>);
127+
(Options extends { strict: true } ? unknown : Record<string, MetadataValue>);
124128

125129
export type GGUFModelKV = (NoModelMetadata | ModelMetadata) & (NoTokenizer | Tokenizer);
126130

@@ -132,7 +136,7 @@ export interface GGUFTensorInfo {
132136
offset: bigint;
133137
}
134138

135-
export interface GGUFParseOutput<TGGUFType extends GGUFType = GGUFType.STRICT> {
136-
metadata: GGUFMetadata<TGGUFType>;
139+
export interface GGUFParseOutput<Options extends GGUFMetadataOptions = { strict: true }> {
140+
metadata: GGUFMetadata<Options>;
137141
tensorInfos: GGUFTensorInfo[];
138142
}

0 commit comments

Comments
 (0)