Skip to content

Commit d321fe3

Browse files
authored
feat: improve grammar support (#215)
* feat: improve grammar support * feat: improve JSON schema grammar
1 parent c6a80c0 commit d321fe3

32 files changed

+591
-148
lines changed

.config/typedoc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@
2323
"propertiesFormat": "list",
2424
"enumMembersFormat": "table",
2525
"typeDeclarationFormat": "list",
26-
"hideInPageTOC": true,
26+
"sort": ["source-order"],
2727
"docsRoot": "../docs"
2828
}

.releaserc.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ export default {
4040
}
4141
}],
4242
"@semantic-release/npm",
43-
"@semantic-release/github",
43+
["@semantic-release/github", {
44+
"discussionCategoryName": "Releases"
45+
}],
4446
["@semantic-release/exec", {
4547
"publishCmd": "echo \"${nextRelease.version}\" > .semanticRelease.npmPackage.deployedVersion.txt"
4648
}]

.vitepress/config.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,10 @@ export default defineConfig({
137137
...(await fs.readJSON(path.join(__dirname, "..", "tsconfig.json"))).compilerOptions,
138138
moduleResolution: undefined,
139139
paths: {
140-
"node-llama-cpp": [path.resolve(__dirname, "..", "src", "index.ts")]
140+
"node-llama-cpp": [
141+
path.resolve(__dirname, "..", "dist", "index.d.ts"),
142+
path.resolve(__dirname, "..", "src", "index.ts")
143+
]
141144
},
142145
typeRoots: [
143146
path.resolve(__dirname, "..", "node_modules"),
@@ -157,7 +160,7 @@ export default defineConfig({
157160
},
158161
nav: [
159162
{text: "Guide", link: "/guide/", activeMatch: "/guide/"},
160-
{text: "API Reference", link: "/api/classes/LlamaModel", activeMatch: "/api/"},
163+
{text: "API Reference", link: "/api/functions/getLlama", activeMatch: "/api/"},
161164
{
162165
text: packageVersion,
163166
items: [{

package-lock.json

Lines changed: 15 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
"dev:build": "npm run build && node ./dist/cli/cli.js build --noUsageExample",
6363
"clean": "rm -rf ./node_modules ./dist ./tsconfig.tsbuildinfo ./test/.models ./docs/api ./docs/api-overrides",
6464
"docs:generateTypedoc": "typedoc && rimraf ./docs/api/index.md ./docs/api/globals.md ./docs/api/functions/LlamaText.md && npm run docs:generateTypedoc:overrides",
65-
"docs:generateTypedoc:overrides": "typedoc --entryPoints ./src/apiDocsOverrides.ts --out ./docs/api-overrides && copyfiles --flat \"./docs/api-overrides/classes/*.md\" ./docs/api/classes && rimraf ./docs/api-overrides",
65+
"docs:generateTypedoc:overrides": "typedoc --entryPoints ./src/apiDocsOverrides.ts --out ./docs/api-overrides && copyfiles --flat \"./docs/api-overrides/classes/LlamaText.md\" ./docs/api/classes && rimraf ./docs/api-overrides",
6666
"docs:dev": "npm run docs:generateTypedoc && vitepress dev",
6767
"docs:build": "npm run docs:generateTypedoc && vitepress build",
6868
"docs:preview": "npm run docs:generateTypedoc && vitepress preview"
@@ -139,10 +139,10 @@
139139
"rimraf": "^5.0.1",
140140
"semantic-release": "^22.0.8",
141141
"tslib": "^2.6.1",
142-
"typedoc": "^0.25.3",
142+
"typedoc": "^0.25.13",
143143
"typedoc-plugin-markdown": "^4.0.0-next.55",
144-
"typedoc-plugin-mdn-links": "^3.1.19",
145-
"typedoc-vitepress-theme": "^1.0.0-next.10",
144+
"typedoc-plugin-mdn-links": "^3.1.24",
145+
"typedoc-vitepress-theme": "1.0.0-next.10",
146146
"typescript": "^5.2.2",
147147
"vite-node": "^1.4.0",
148148
"vitepress": "^1.1.4",

src/apiDocsOverrides.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
/** @internal */
2-
import {_LlamaText} from "./utils/LlamaText.js";
2+
import {Tokenizer} from "./types.js";
3+
/** @internal */
4+
import {
5+
_LlamaText,
6+
type LlamaTextJSON,
7+
type LlamaTextJSONValue,
8+
type LlamaTextSpecialTokenJSON,
9+
type LlamaTextSpecialTokensTextJSON,
10+
type LlamaTextValue,
11+
type LlamaTextInputValue
12+
} from "./utils/LlamaText.js";
313

414
/** @internal */
5-
export {_LlamaText as LlamaText};
15+
export {
16+
_LlamaText as LlamaText,
17+
type Tokenizer,
18+
type LlamaTextJSON,
19+
type LlamaTextJSONValue,
20+
type LlamaTextSpecialTokensTextJSON,
21+
type LlamaTextSpecialTokenJSON,
22+
type LlamaTextValue,
23+
type LlamaTextInputValue
24+
};

src/bindings/Llama.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import {DisposedError, EventRelay, withLock} from "lifecycle-utils";
33
import {getConsoleLogPrefix} from "../utils/getConsoleLogPrefix.js";
44
import {LlamaModel, LlamaModelOptions} from "../evaluator/LlamaModel.js";
55
import {DisposeGuard} from "../utils/DisposeGuard.js";
6+
import {GbnfJsonSchema} from "../utils/gbnfJson/types.js";
7+
import {LlamaJsonSchemaGrammar} from "../evaluator/LlamaJsonSchemaGrammar.js";
8+
import {LlamaGrammar, LlamaGrammarOptions} from "../evaluator/LlamaGrammar.js";
69
import {BindingModule} from "./AddonTypes.js";
710
import {BuildGpu, BuildMetadataFile, LlamaLocks, LlamaLogLevel} from "./types.js";
811
import {MemoryOrchestrator, MemoryReservation} from "./utils/MemoryOrchestrator.js";
@@ -229,6 +232,18 @@ export class Llama {
229232
});
230233
}
231234

235+
public async createGrammarForJsonSchema<const T extends Readonly<GbnfJsonSchema>>(schema: T) {
236+
return new LlamaJsonSchemaGrammar<T>(this, schema);
237+
}
238+
239+
public async getGrammarFor(type: Parameters<typeof LlamaGrammar.getFor>[1]) {
240+
return await LlamaGrammar.getFor(this, type);
241+
}
242+
243+
public async createGrammar(options: LlamaGrammarOptions) {
244+
return new LlamaGrammar(this, options);
245+
}
246+
232247
/** @internal */
233248
public async _init() {
234249
await this._bindings.init();

src/bindings/getLlama.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ const defaultBuildOption: Exclude<LlamaOptions["build"], undefined> = runningInE
169169
: "auto";
170170

171171
/**
172-
* Get a llama.cpp binding.
172+
* Get a `llama.cpp` binding.
173173
* Defaults to prefer a prebuilt binary, and fallbacks to building from source if a prebuilt binary is not found.
174174
* Pass `"lastCliBuild"` to default to use the last successful build created using the `download` or `build` CLI commands if one exists.
175175
*/

src/evaluator/LlamaChat/LlamaChat.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ export class LlamaChat {
657657
pendingTokens.push(...streamRegulator.popFreeChunkTokens());
658658

659659
const triggeredStops = functionSyntaxStartDetector.getTriggeredStops();
660-
const partiallyFreeTokens = streamRegulator.getPartiallyFreeChunk();
660+
const partiallyFreeTokens = streamRegulator.getPartiallyFreeChunk(model.tokenizer);
661661

662662
const queuedTokensBeforeStopTrigger = getQueuedTokensBeforeStopTrigger(
663663
triggeredStops,
@@ -775,10 +775,15 @@ export class LlamaChat {
775775
if (stopGenerationDetector.hasTriggeredStops || customStopGenerationTriggersDetector.hasTriggeredStops ||
776776
model.isEogToken(token)
777777
) {
778+
stopGenerationDetector.clearInProgressStops();
779+
customStopGenerationTriggersDetector.clearInProgressStops();
780+
pendingTokens.push(...streamRegulator.popFreeChunkTokens());
781+
778782
const triggeredStops = stopGenerationDetector.hasTriggeredStops
779783
? stopGenerationDetector.getTriggeredStops()
780784
: customStopGenerationTriggersDetector.getTriggeredStops();
781-
const partiallyFreeTokens = streamRegulator.getPartiallyFreeChunk();
785+
786+
const partiallyFreeTokens = streamRegulator.getPartiallyFreeChunk(model.tokenizer);
782787

783788
const queuedTokensBeforeStopTrigger = getQueuedTokensBeforeStopTrigger(
784789
triggeredStops,

src/evaluator/LlamaChat/utils/FunctionCallGrammar.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ export class FunctionCallGrammar<const Functions extends ChatModelFunctions> ext
2020
public constructor(llama: Llama, functions: Functions, chatWrapper: ChatWrapper, initialFunctionCallEngaged: boolean) {
2121
const grammar = getGbnfGrammarForFunctionCalls(functions, chatWrapper, initialFunctionCallEngaged);
2222

23-
super({
24-
llama,
23+
super(llama, {
2524
grammar,
2625
stopGenerationTriggers: [LlamaText(chatWrapper.settings.functions.call.suffix, "\n".repeat(4))],
2726
trimWhitespaceSuffix: true

0 commit comments

Comments
 (0)