withcatai / node-llama-cpp

Run AI models locally on your machine with node.js bindings for llama.cpp. Force a JSON schema on the model output on the generation level
https://withcatai.github.io/node-llama-cpp/
MIT License
736 stars 63 forks source link

Integrate TS compiler to parse types to grammar #210

Closed jazelly closed 2 months ago

jazelly commented 2 months ago

Feature Description

The GBNF works as a charm. Considering the consumer end are mostly typed, wouldn't it be better to support the conversion from type to grammar?

The Solution

A simple solution is just to use TS compiler api to parse the AST to grammar.

Considered Alternatives

Or can simply use gbnfgen

Additional Context

I am raising this while I was playing with this

Related Features to This Feature Request

Are you willing to resolve this issue by submitting a Pull Request?

Yes, I have the time, and I know how to start.

giladgd commented 2 months ago

I've considered this in the past and decided not to support it, since converting TS types to grammar requires either:

Also, loading the source of a .ts file without using it in your TypeScript code by importing it like any other code won't use the types inside of it to ensure your code is valid, so you lose all the type safety that TypeScript offers.

The JSON schema grammar provided by node-llama-cpp is type-safe and makes use of TypeScript properly to provide type validations against the output that a model generates.

I think gbnfgen is cool, and thus I've mentioned it in the documentation, but I haven't found it practical enough for TypeScript projects to actually integrate something like that directly into node-llama-cpp.

I have used the TypeScript compiler API in the past to read and modify codebases, so I'm well aware of its capabilities and how to use it to do things like that, so my main reason not to add built-in support for something like that in node-llama-cpp is due to it adding complexity to people's projects and not making good use of TypeScript's type safety system.

Anyway, thanks for the suggestion :)