-
Notifications
You must be signed in to change notification settings - Fork 231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gguf: better type usage #655
Conversation
Also in the process, I noticed some details not being mentioned in gguf specs:
|
Very nice PR! And much needed improvement to the typing of gguf, especially type inference. |
packages/gguf/src/types.ts
Outdated
Partial<Model> & | ||
Record<string, MetadataValue>; | ||
} & GGUFModelKV & | ||
(TGGUFType extends GGUFType.strict ? unknown : Record<string, MetadataValue>); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI, the unknown
here allow casting between GGUFParseOutput<GGUFType.strict>
and GGUFParseOutput<GGUFType.nonStrict>
. Funny though, I have no idea why adding unknown
make it work. Maybe this is not the best way to do, so feel free to tell if you have other suggestions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks in good shape! do you want to take a quick look too @coyotte508?
Question regarding |
I believe that |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM !
@coyotte508 could you give a quick look and I will merge 🚀 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good!
I'm wondering about flat paths vs nested metadata (maybe it could be another option in the future 🤔) but LGTM with the 1/2 changes requested
packages/gguf/src/gguf.ts
Outdated
@@ -308,6 +308,9 @@ export async function gguf( | |||
} | |||
} | |||
offset += valueResult.length; | |||
/// TODO(fix typing) | |||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | |||
// @ts-ignore |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's use // @ts-expect-error
instead of // @ts-ignore
in general
(no need for eslint-disable this way)
Here I think you can change const metadata: GGUFMetadata
to const metadata: GGUFMetadata<GGUFType.NON_STRICT>
to remove the error (not sure if it's the best fix)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 31bac8b
packages/gguf/src/types.ts
Outdated
export enum GGUFType { | ||
STRICT, | ||
NON_STRICT, | ||
} | ||
|
||
export type GGUFMetadata<TGGUFType extends GGUFType = GGUFType.STRICT> = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably switch to something like https://github.com/sindresorhus/type-fest/blob/main/source/except.d.ts
interface GGUFMetadataOptions {
/**
* ...
*
* @default true
*/
strict: boolean;
}
export GGUFMetadata<Options extends GGUFMetadataOptions = { strict: true}> {
...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea 👍 I've never thought about this before. Implemented in 31bac8b
@coyotte508 I thought about using nested object, but that seems to be quite complicated atm, since part of the code/naming here is copied directly from cpp file (so it save us some headaches) But I'm also agree that having nested object is a reasonable feature, since gguf keys are "namespaced" anyway. Maybe we will re-visit this idea later on. |
alright let's merge! re. nested properties, that's what they did in https://github.com/ahoylabs/gguf.js (credited as one of the inspirations for this package) but I'm not sure it's worth it / I find it cleaner to just expose the raw data from the GGUF file. |
Follow up #655 and #656 (comment) Added some examples on how to use local file + strictly typed --------- Co-authored-by: Julien Chaumond <[email protected]> Co-authored-by: Mishig <[email protected]>
Follow up #640
Ref comments:
metadata["general.architecture"] === ...
to select the correct typeThe type system introduce in this PR allows type-checking at both compile time & runtime:
Type checks can be disable with
GGUFMetadata<GGUFType.NON_STRICT>