modelfusion 0.116.1 → 0.117.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,46 @@
1
1
  # Changelog
2
2
 
3
+ ## v0.117.0 - 2024-01-06
4
+
5
+ ### Added
6
+
7
+ - Predefined Llama.cpp GBNF grammars:
8
+
9
+ - `llamacpp.grammar.json`: Restricts the output to JSON.
10
+ - `llamacpp.grammar.jsonArray`: Restricts the output to a JSON array.
11
+ - `llamacpp.grammar.list`: Restricts the output to a newline-separated list where each line starts with `- `.
12
+
13
+ - Llama.cpp structure generation support:
14
+
15
+ ```ts
16
+ const structure = await generateStructure(
17
+ llamacpp
18
+ .TextGenerator({
19
+ // run openhermes-2.5-mistral-7b.Q4_K_M.gguf in llama.cpp
20
+ maxGenerationTokens: 1024,
21
+ temperature: 0,
22
+ })
23
+ .withTextPromptTemplate(ChatMLPrompt.instruction()) // needed for jsonStructurePrompt.text()
24
+ .asStructureGenerationModel(jsonStructurePrompt.text()), // automatically restrict the output to JSON
25
+
26
+ zodSchema(
27
+ z.object({
28
+ characters: z.array(
29
+ z.object({
30
+ name: z.string(),
31
+ class: z
32
+ .string()
33
+ .describe("Character class, e.g. warrior, mage, or thief."),
34
+ description: z.string(),
35
+ })
36
+ ),
37
+ })
38
+ ),
39
+
40
+ "Generate 3 character descriptions for a fantasy role playing game. "
41
+ );
42
+ ```
43
+
3
44
  ## v0.116.0 - 2024-01-05
4
45
 
5
46
  ### Added
package/README.md CHANGED
@@ -152,7 +152,7 @@ const sentiment = await generateStructure(
152
152
  );
153
153
  ```
154
154
 
155
- Providers: [OpenAI](https://modelfusion.dev/integration/model-provider/openai), [Ollama](https://modelfusion.dev//integration/model-provider/ollama)
155
+ Providers: [OpenAI](https://modelfusion.dev/integration/model-provider/openai), [Ollama](https://modelfusion.dev//integration/model-provider/ollama), [Llama.cpp](https://modelfusion.dev//integration/model-provider/llama.cpp)
156
156
 
157
157
  #### streamStructure
158
158
 
@@ -198,7 +198,7 @@ for await (const part of structureStream) {
198
198
  }
199
199
  ```
200
200
 
201
- Providers: [OpenAI](https://modelfusion.dev/integration/model-provider/openai), [Ollama](https://modelfusion.dev//integration/model-provider/ollama)
201
+ Providers: [OpenAI](https://modelfusion.dev/integration/model-provider/openai), [Ollama](https://modelfusion.dev//integration/model-provider/ollama), [Llama.cpp](https://modelfusion.dev//integration/model-provider/llama.cpp)
202
202
 
203
203
  ### [Generate Image](https://modelfusion.dev/guide/function/generate-image)
204
204
 
@@ -563,6 +563,8 @@ modelfusion.setLogFormat("detailed-object"); // log full events
563
563
  - [Split Text](https://modelfusion.dev/guide/text-chunk/split)
564
564
  - [Utilities](https://modelfusion.dev/guide/util/)
565
565
  - [API Configuration](https://modelfusion.dev/guide/util/api-configuration)
566
+ - [Base URL](https://modelfusion.dev/guide/util/api-configuration/base-url)
567
+ - [Headers](https://modelfusion.dev/guide/util/api-configuration/headers)
566
568
  - [Retry strategies](https://modelfusion.dev/guide/util/api-configuration/retry)
567
569
  - [Throttling strategies](https://modelfusion.dev/guide/util/api-configuration/throttle)
568
570
  - [Logging](https://modelfusion.dev/guide/util/logging)
@@ -15,6 +15,7 @@ const parseEventSourceStream_js_1 = require("../../util/streaming/parseEventSour
15
15
  const LlamaCppApiConfiguration_js_1 = require("./LlamaCppApiConfiguration.cjs");
16
16
  const LlamaCppError_js_1 = require("./LlamaCppError.cjs");
17
17
  const LlamaCppTokenizer_js_1 = require("./LlamaCppTokenizer.cjs");
18
+ const LlamaCppGrammars_js_1 = require("./LlamaCppGrammars.cjs");
18
19
  class LlamaCppCompletionModel extends AbstractModel_js_1.AbstractModel {
19
20
  constructor(settings = {}) {
20
21
  super({ settings });
@@ -167,6 +168,12 @@ class LlamaCppCompletionModel extends AbstractModel_js_1.AbstractModel {
167
168
  extractTextDelta(delta) {
168
169
  return delta.content;
169
170
  }
171
+ withJsonOutput() {
172
+ // don't override the grammar if it's already set (to support more restrictive grammars)
173
+ return this.settings.grammar == null
174
+ ? this.withSettings({ grammar: LlamaCppGrammars_js_1.json })
175
+ : this;
176
+ }
170
177
  withTextPrompt() {
171
178
  return this.withPromptTemplate({
172
179
  format(prompt) {
@@ -376,6 +376,7 @@ export declare class LlamaCppCompletionModel<CONTEXT_WINDOW_SIZE extends number
376
376
  content: string;
377
377
  }>>>;
378
378
  extractTextDelta(delta: unknown): string;
379
+ withJsonOutput(): this;
379
380
  withTextPrompt(): PromptTemplateTextStreamingModel<string, LlamaCppCompletionPrompt, LlamaCppCompletionModelSettings<CONTEXT_WINDOW_SIZE>, this>;
380
381
  /**
381
382
  * Maps the prompt for a text version of the Llama.cpp prompt template (without image support).
@@ -12,6 +12,7 @@ import { parseEventSourceStream } from "../../util/streaming/parseEventSourceStr
12
12
  import { LlamaCppApiConfiguration } from "./LlamaCppApiConfiguration.js";
13
13
  import { failedLlamaCppCallResponseHandler } from "./LlamaCppError.js";
14
14
  import { LlamaCppTokenizer } from "./LlamaCppTokenizer.js";
15
+ import { json } from "./LlamaCppGrammars.js";
15
16
  export class LlamaCppCompletionModel extends AbstractModel {
16
17
  constructor(settings = {}) {
17
18
  super({ settings });
@@ -164,6 +165,12 @@ export class LlamaCppCompletionModel extends AbstractModel {
164
165
  extractTextDelta(delta) {
165
166
  return delta.content;
166
167
  }
168
+ withJsonOutput() {
169
+ // don't override the grammar if it's already set (to support more restrictive grammars)
170
+ return this.settings.grammar == null
171
+ ? this.withSettings({ grammar: json })
172
+ : this;
173
+ }
167
174
  withTextPrompt() {
168
175
  return this.withPromptTemplate({
169
176
  format(prompt) {
@@ -1,6 +1,29 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
2
25
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Tokenizer = exports.TextEmbedder = exports.TextGenerator = exports.Api = void 0;
26
+ exports.grammar = exports.Tokenizer = exports.TextEmbedder = exports.TextGenerator = exports.Api = void 0;
4
27
  const LlamaCppApiConfiguration_js_1 = require("./LlamaCppApiConfiguration.cjs");
5
28
  const LlamaCppCompletionModel_js_1 = require("./LlamaCppCompletionModel.cjs");
6
29
  const LlamaCppTextEmbeddingModel_js_1 = require("./LlamaCppTextEmbeddingModel.cjs");
@@ -25,3 +48,7 @@ function Tokenizer(api = new LlamaCppApiConfiguration_js_1.LlamaCppApiConfigurat
25
48
  return new LlamaCppTokenizer_js_1.LlamaCppTokenizer(api);
26
49
  }
27
50
  exports.Tokenizer = Tokenizer;
51
+ /**
52
+ * GBNF grammars. You can use them in the `grammar` option of the `TextGenerator` model.
53
+ */
54
+ exports.grammar = __importStar(require("./LlamaCppGrammars.cjs"));
@@ -12,3 +12,7 @@ export declare function Api(settings: PartialBaseUrlPartsApiConfigurationOptions
12
12
  export declare function TextGenerator<CONTEXT_WINDOW_SIZE extends number>(settings?: LlamaCppCompletionModelSettings<CONTEXT_WINDOW_SIZE>): LlamaCppCompletionModel<CONTEXT_WINDOW_SIZE>;
13
13
  export declare function TextEmbedder(settings?: LlamaCppTextEmbeddingModelSettings): LlamaCppTextEmbeddingModel;
14
14
  export declare function Tokenizer(api?: ApiConfiguration): LlamaCppTokenizer;
15
+ /**
16
+ * GBNF grammars. You can use them in the `grammar` option of the `TextGenerator` model.
17
+ */
18
+ export * as grammar from "./LlamaCppGrammars.js";
@@ -18,3 +18,7 @@ export function TextEmbedder(settings = {}) {
18
18
  export function Tokenizer(api = new LlamaCppApiConfiguration()) {
19
19
  return new LlamaCppTokenizer(api);
20
20
  }
21
+ /**
22
+ * GBNF grammars. You can use them in the `grammar` option of the `TextGenerator` model.
23
+ */
24
+ export * as grammar from "./LlamaCppGrammars.js";
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.list = exports.jsonArray = exports.json = void 0;
4
+ /**
5
+ * GBNF grammar for JSON.
6
+ *
7
+ * @see https://github.com/ggerganov/llama.cpp/blob/master/grammars/json.gbnf
8
+ */
9
+ exports.json = `
10
+ root ::= object
11
+ value ::= object | array | string | number | ("true" | "false" | "null") ws
12
+
13
+ object ::=
14
+ "{" ws (
15
+ string ":" ws value
16
+ ("," ws string ":" ws value)*
17
+ )? "}" ws
18
+
19
+ array ::=
20
+ "[" ws (
21
+ value
22
+ ("," ws value)*
23
+ )? "]" ws
24
+
25
+ string ::=
26
+ "\\"" (
27
+ [^"\\\\] |
28
+ "\\\\" (["\\\\/bfnrt] | "u" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F]) # escapes
29
+ )* "\\"" ws
30
+
31
+ number ::= ("-"? ([0-9] | [1-9] [0-9]*)) ("." [0-9]+)? ([eE] [-+]? [0-9]+)? ws
32
+
33
+ # Optional space: by convention, applied in this grammar after literal chars when allowed
34
+ ws ::= ([ \t\n] ws)?
35
+ `;
36
+ /**
37
+ * GBNF grammar for JSON array outputs. Restricts whitespace at the end of the array.
38
+ *
39
+ * @see https://github.com/ggerganov/llama.cpp/blob/master/grammars/json_arr.gbnf
40
+ */
41
+ exports.jsonArray = `
42
+ root ::= arr
43
+ value ::= object | array | string | number | ("true" | "false" | "null") ws
44
+
45
+ arr ::=
46
+ "[\n" ws (
47
+ value
48
+ (",\n" ws value)*
49
+ )? "]"
50
+
51
+ object ::=
52
+ "{" ws (
53
+ string ":" ws value
54
+ ("," ws string ":" ws value)*
55
+ )? "}" ws
56
+
57
+ array ::=
58
+ "[" ws (
59
+ value
60
+ ("," ws value)*
61
+ )? "]" ws
62
+
63
+ string ::=
64
+ "\\"" (
65
+ [^"\\\\] |
66
+ "\\\\" (["\\\\/bfnrt] | "u" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F]) # escapes
67
+ )* "\\"" ws
68
+
69
+ number ::= ("-"? ([0-9] | [1-9] [0-9]*)) ("." [0-9]+)? ([eE] [-+]? [0-9]+)? ws
70
+
71
+ # Optional space: by convention, applied in this grammar after literal chars when allowed
72
+ ws ::= ([ \t\n] ws)?
73
+ `;
74
+ /**
75
+ * GBNF grammar for list outputs. List items are separated by newlines and start with `- `.
76
+ *
77
+ * @see https://github.com/ggerganov/llama.cpp/blob/master/grammars/list.gbnf
78
+ */
79
+ exports.list = `
80
+ root ::= item+
81
+
82
+ # Excludes various line break characters
83
+ item ::= "- " [^\r\n\x0b\x0c\x85\u2028\u2029]+ "\n"
84
+ `;
@@ -0,0 +1,18 @@
1
+ /**
2
+ * GBNF grammar for JSON.
3
+ *
4
+ * @see https://github.com/ggerganov/llama.cpp/blob/master/grammars/json.gbnf
5
+ */
6
+ export declare const json: string;
7
+ /**
8
+ * GBNF grammar for JSON array outputs. Restricts whitespace at the end of the array.
9
+ *
10
+ * @see https://github.com/ggerganov/llama.cpp/blob/master/grammars/json_arr.gbnf
11
+ */
12
+ export declare const jsonArray: string;
13
+ /**
14
+ * GBNF grammar for list outputs. List items are separated by newlines and start with `- `.
15
+ *
16
+ * @see https://github.com/ggerganov/llama.cpp/blob/master/grammars/list.gbnf
17
+ */
18
+ export declare const list: string;
@@ -0,0 +1,81 @@
1
+ /**
2
+ * GBNF grammar for JSON.
3
+ *
4
+ * @see https://github.com/ggerganov/llama.cpp/blob/master/grammars/json.gbnf
5
+ */
6
+ export const json = `
7
+ root ::= object
8
+ value ::= object | array | string | number | ("true" | "false" | "null") ws
9
+
10
+ object ::=
11
+ "{" ws (
12
+ string ":" ws value
13
+ ("," ws string ":" ws value)*
14
+ )? "}" ws
15
+
16
+ array ::=
17
+ "[" ws (
18
+ value
19
+ ("," ws value)*
20
+ )? "]" ws
21
+
22
+ string ::=
23
+ "\\"" (
24
+ [^"\\\\] |
25
+ "\\\\" (["\\\\/bfnrt] | "u" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F]) # escapes
26
+ )* "\\"" ws
27
+
28
+ number ::= ("-"? ([0-9] | [1-9] [0-9]*)) ("." [0-9]+)? ([eE] [-+]? [0-9]+)? ws
29
+
30
+ # Optional space: by convention, applied in this grammar after literal chars when allowed
31
+ ws ::= ([ \t\n] ws)?
32
+ `;
33
+ /**
34
+ * GBNF grammar for JSON array outputs. Restricts whitespace at the end of the array.
35
+ *
36
+ * @see https://github.com/ggerganov/llama.cpp/blob/master/grammars/json_arr.gbnf
37
+ */
38
+ export const jsonArray = `
39
+ root ::= arr
40
+ value ::= object | array | string | number | ("true" | "false" | "null") ws
41
+
42
+ arr ::=
43
+ "[\n" ws (
44
+ value
45
+ (",\n" ws value)*
46
+ )? "]"
47
+
48
+ object ::=
49
+ "{" ws (
50
+ string ":" ws value
51
+ ("," ws string ":" ws value)*
52
+ )? "}" ws
53
+
54
+ array ::=
55
+ "[" ws (
56
+ value
57
+ ("," ws value)*
58
+ )? "]" ws
59
+
60
+ string ::=
61
+ "\\"" (
62
+ [^"\\\\] |
63
+ "\\\\" (["\\\\/bfnrt] | "u" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F]) # escapes
64
+ )* "\\"" ws
65
+
66
+ number ::= ("-"? ([0-9] | [1-9] [0-9]*)) ("." [0-9]+)? ([eE] [-+]? [0-9]+)? ws
67
+
68
+ # Optional space: by convention, applied in this grammar after literal chars when allowed
69
+ ws ::= ([ \t\n] ws)?
70
+ `;
71
+ /**
72
+ * GBNF grammar for list outputs. List items are separated by newlines and start with `- `.
73
+ *
74
+ * @see https://github.com/ggerganov/llama.cpp/blob/master/grammars/list.gbnf
75
+ */
76
+ export const list = `
77
+ root ::= item+
78
+
79
+ # Excludes various line break characters
80
+ item ::= "- " [^\r\n\x0b\x0c\x85\u2028\u2029]+ "\n"
81
+ `;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "modelfusion",
3
3
  "description": "The TypeScript library for building AI applications.",
4
- "version": "0.116.1",
4
+ "version": "0.117.0",
5
5
  "author": "Lars Grammel",
6
6
  "license": "MIT",
7
7
  "keywords": [