modelfusion 0.68.1 → 0.70.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.
Files changed (95) hide show
  1. package/README.md +22 -9
  2. package/core/schema/JSONParseError.cjs +1 -1
  3. package/core/schema/JSONParseError.d.ts +1 -1
  4. package/core/schema/JSONParseError.js +1 -1
  5. package/model-function/ModelCallEvent.d.ts +3 -2
  6. package/model-function/generate-structure/StructureValidationError.cjs +11 -0
  7. package/model-function/generate-structure/StructureValidationError.d.ts +9 -0
  8. package/model-function/generate-structure/StructureValidationError.js +11 -0
  9. package/model-function/generate-text/index.cjs +1 -8
  10. package/model-function/generate-text/index.d.ts +1 -8
  11. package/model-function/generate-text/index.js +1 -8
  12. package/model-function/generate-text/prompt-format/AlpacaPromptFormat.cjs +31 -3
  13. package/model-function/generate-text/prompt-format/AlpacaPromptFormat.d.ts +29 -1
  14. package/model-function/generate-text/prompt-format/AlpacaPromptFormat.js +29 -1
  15. package/model-function/generate-text/prompt-format/ChatMLPromptFormat.cjs +79 -0
  16. package/model-function/generate-text/prompt-format/ChatMLPromptFormat.d.ts +31 -0
  17. package/model-function/generate-text/prompt-format/ChatMLPromptFormat.js +74 -0
  18. package/model-function/generate-text/prompt-format/ChatPrompt.d.ts +28 -23
  19. package/model-function/generate-text/prompt-format/ChatPromptValidationError.cjs +17 -0
  20. package/model-function/generate-text/prompt-format/ChatPromptValidationError.d.ts +8 -0
  21. package/model-function/generate-text/prompt-format/ChatPromptValidationError.js +13 -0
  22. package/model-function/generate-text/prompt-format/Llama2PromptFormat.cjs +41 -27
  23. package/model-function/generate-text/prompt-format/Llama2PromptFormat.d.ts +20 -2
  24. package/model-function/generate-text/prompt-format/Llama2PromptFormat.js +38 -24
  25. package/model-function/generate-text/prompt-format/TextPromptFormat.cjs +27 -30
  26. package/model-function/generate-text/prompt-format/TextPromptFormat.d.ts +7 -5
  27. package/model-function/generate-text/prompt-format/TextPromptFormat.js +24 -27
  28. package/model-function/generate-text/prompt-format/VicunaPromptFormat.cjs +21 -29
  29. package/model-function/generate-text/prompt-format/VicunaPromptFormat.d.ts +2 -2
  30. package/model-function/generate-text/prompt-format/VicunaPromptFormat.js +19 -27
  31. package/model-function/generate-text/prompt-format/index.cjs +39 -0
  32. package/model-function/generate-text/prompt-format/index.d.ts +10 -0
  33. package/model-function/generate-text/prompt-format/index.js +10 -0
  34. package/model-function/generate-text/prompt-format/trimChatPrompt.cjs +17 -22
  35. package/model-function/generate-text/prompt-format/trimChatPrompt.js +17 -22
  36. package/model-function/generate-text/prompt-format/validateChatPrompt.cjs +12 -24
  37. package/model-function/generate-text/prompt-format/validateChatPrompt.d.ts +0 -3
  38. package/model-function/generate-text/prompt-format/validateChatPrompt.js +10 -21
  39. package/model-function/generate-tool-call/ToolCallDefinition.cjs +2 -0
  40. package/model-function/generate-tool-call/ToolCallDefinition.d.ts +7 -0
  41. package/model-function/generate-tool-call/ToolCallDefinition.js +1 -0
  42. package/model-function/generate-tool-call/ToolCallGenerationError.cjs +35 -0
  43. package/model-function/generate-tool-call/ToolCallGenerationError.d.ts +15 -0
  44. package/model-function/generate-tool-call/ToolCallGenerationError.js +31 -0
  45. package/model-function/generate-tool-call/ToolCallGenerationEvent.cjs +2 -0
  46. package/model-function/generate-tool-call/ToolCallGenerationEvent.d.ts +23 -0
  47. package/model-function/generate-tool-call/ToolCallGenerationEvent.js +1 -0
  48. package/model-function/generate-tool-call/ToolCallGenerationModel.cjs +2 -0
  49. package/model-function/generate-tool-call/ToolCallGenerationModel.d.ts +19 -0
  50. package/model-function/generate-tool-call/ToolCallGenerationModel.js +1 -0
  51. package/model-function/generate-tool-call/ToolCallParametersValidationError.cjs +44 -0
  52. package/model-function/generate-tool-call/ToolCallParametersValidationError.d.ts +18 -0
  53. package/model-function/generate-tool-call/ToolCallParametersValidationError.js +40 -0
  54. package/model-function/generate-tool-call/generateToolCall.cjs +58 -0
  55. package/model-function/generate-tool-call/generateToolCall.d.ts +20 -0
  56. package/model-function/generate-tool-call/generateToolCall.js +54 -0
  57. package/model-function/generate-tool-call/index.cjs +21 -0
  58. package/model-function/generate-tool-call/index.d.ts +5 -0
  59. package/model-function/generate-tool-call/index.js +5 -0
  60. package/model-function/index.cjs +1 -0
  61. package/model-function/index.d.ts +1 -0
  62. package/model-function/index.js +1 -0
  63. package/model-provider/anthropic/AnthropicPromptFormat.cjs +22 -26
  64. package/model-provider/anthropic/AnthropicPromptFormat.d.ts +4 -2
  65. package/model-provider/anthropic/AnthropicPromptFormat.js +19 -23
  66. package/model-provider/anthropic/AnthropicTextGenerationModel.cjs +2 -2
  67. package/model-provider/anthropic/AnthropicTextGenerationModel.js +3 -3
  68. package/model-provider/anthropic/index.cjs +14 -2
  69. package/model-provider/anthropic/index.d.ts +1 -1
  70. package/model-provider/anthropic/index.js +1 -1
  71. package/model-provider/automatic1111/Automatic1111ImageGenerationModel.d.ts +3 -3
  72. package/model-provider/cohere/CohereTextGenerationModel.cjs +3 -3
  73. package/model-provider/cohere/CohereTextGenerationModel.d.ts +1 -1
  74. package/model-provider/cohere/CohereTextGenerationModel.js +4 -4
  75. package/model-provider/llamacpp/{mapInstructionPromptToBakLLaVA1ForLlamaCppFormat.cjs → LlamaCppBakLLaVA1Format.cjs} +4 -4
  76. package/model-provider/llamacpp/{mapInstructionPromptToBakLLaVA1ForLlamaCppFormat.d.ts → LlamaCppBakLLaVA1Format.d.ts} +2 -2
  77. package/model-provider/llamacpp/{mapInstructionPromptToBakLLaVA1ForLlamaCppFormat.js → LlamaCppBakLLaVA1Format.js} +2 -2
  78. package/model-provider/llamacpp/index.cjs +14 -2
  79. package/model-provider/llamacpp/index.d.ts +1 -1
  80. package/model-provider/llamacpp/index.js +1 -1
  81. package/model-provider/openai/OpenAICompletionModel.cjs +4 -4
  82. package/model-provider/openai/OpenAICompletionModel.d.ts +1 -1
  83. package/model-provider/openai/OpenAICompletionModel.js +5 -5
  84. package/model-provider/openai/chat/OpenAIChatModel.cjs +50 -4
  85. package/model-provider/openai/chat/OpenAIChatModel.d.ts +192 -20
  86. package/model-provider/openai/chat/OpenAIChatModel.js +51 -5
  87. package/model-provider/openai/chat/OpenAIChatPromptFormat.cjs +22 -34
  88. package/model-provider/openai/chat/OpenAIChatPromptFormat.d.ts +2 -2
  89. package/model-provider/openai/chat/OpenAIChatPromptFormat.js +19 -31
  90. package/model-provider/openai/chat/OpenAIChatStreamIterable.cjs +10 -0
  91. package/model-provider/openai/chat/OpenAIChatStreamIterable.js +10 -0
  92. package/model-provider/openai/index.cjs +14 -2
  93. package/model-provider/openai/index.d.ts +1 -1
  94. package/model-provider/openai/index.js +1 -1
  95. package/package.json +3 -3
package/README.md CHANGED
@@ -26,7 +26,7 @@
26
26
  ## Quick Install
27
27
 
28
28
  > [!NOTE]
29
- > ModelFusion is in its initial development phase. The main API is now mostly stable, but until version 1.0 there may be minor breaking changes. Feedback and suggestions are welcome.
29
+ > ModelFusion is in its initial development phase. The main API is now mostly stable, but until version 1.0 there may be breaking changes. Feedback and suggestions are welcome.
30
30
 
31
31
  ```sh
32
32
  npm install modelfusion
@@ -470,13 +470,14 @@ Prompt formats let you use higher level prompt structures (such as instruction o
470
470
  #### [Text Generation Prompt Formats](https://modelfusion.dev/guide/function/generate-text#prompt-format)
471
471
 
472
472
  ```ts
473
+ // example assumes you are running https://huggingface.co/TheBloke/Llama-2-7B-GGUF with llama.cpp
473
474
  const text = await generateText(
474
475
  new LlamaCppTextGenerationModel({
475
476
  contextWindowSize: 4096, // Llama 2 context window size
476
477
  maxCompletionTokens: 1000,
477
478
  })
478
- .withTextPrompt()
479
- .withPromptFormat(mapInstructionPromptToLlama2Format()),
479
+ .withTextPrompt() // pure text prompt (no images)
480
+ .withPromptFormat(Llama2PromptFormat.instruction()),
480
481
  {
481
482
  system: "You are a story writer.",
482
483
  instruction: "Write a short story about a robot learning to love.",
@@ -491,12 +492,23 @@ const textStream = await streamText(
491
492
  new OpenAIChatModel({
492
493
  model: "gpt-3.5-turbo",
493
494
  }).withChatPrompt(),
494
- [
495
- { system: "You are a celebrated poet." },
496
- { user: "Write a short story about a robot learning to love." },
497
- { ai: "Once upon a time, there was a robot who learned to love." },
498
- { user: "That's a great start!" },
499
- ]
495
+ {
496
+ system: "You are a celebrated poet.",
497
+ messages: [
498
+ {
499
+ role: "user",
500
+ content: "Suggest a name for a robot.",
501
+ },
502
+ {
503
+ role: "assistant",
504
+ content: "I suggest the name Robbie",
505
+ },
506
+ {
507
+ role: "user",
508
+ content: "Write a short story about Robbie learning to love",
509
+ },
510
+ ],
511
+ }
500
512
  );
501
513
  ```
502
514
 
@@ -505,6 +517,7 @@ const textStream = await streamText(
505
517
  | OpenAI Chat | ✅ | ✅ |
506
518
  | Anthropic | ✅ | ✅ |
507
519
  | Llama 2 | ✅ | ✅ |
520
+ | ChatML | ✅ | ✅ |
508
521
  | Alpaca | ✅ | ❌ |
509
522
  | Vicuna | ❌ | ✅ |
510
523
  | Generic Text | ✅ | ✅ |
@@ -34,8 +34,8 @@ class JSONParseError extends Error {
34
34
  name: this.name,
35
35
  message: this.message,
36
36
  cause: this.cause,
37
- valueText: this.text,
38
37
  stack: this.stack,
38
+ valueText: this.text,
39
39
  };
40
40
  }
41
41
  }
@@ -10,7 +10,7 @@ export declare class JSONParseError extends Error {
10
10
  name: string;
11
11
  message: string;
12
12
  cause: unknown;
13
- valueText: string;
14
13
  stack: string | undefined;
14
+ valueText: string;
15
15
  };
16
16
  }
@@ -31,8 +31,8 @@ export class JSONParseError extends Error {
31
31
  name: this.name,
32
32
  message: this.message,
33
33
  cause: this.cause,
34
- valueText: this.text,
35
34
  stack: this.stack,
35
+ valueText: this.text,
36
36
  };
37
37
  }
38
38
  }
@@ -6,6 +6,7 @@ import { SpeechGenerationFinishedEvent, SpeechGenerationStartedEvent, SpeechStre
6
6
  import { StructureGenerationFinishedEvent, StructureGenerationStartedEvent } from "./generate-structure/StructureGenerationEvent.js";
7
7
  import { StructureStreamingFinishedEvent, StructureStreamingStartedEvent } from "./generate-structure/StructureStreamingEvent.js";
8
8
  import { TextGenerationFinishedEvent, TextGenerationStartedEvent, TextStreamingFinishedEvent, TextStreamingStartedEvent } from "./generate-text/TextGenerationEvent.js";
9
+ import { ToolCallGenerationFinishedEvent, ToolCallGenerationStartedEvent } from "./generate-tool-call/ToolCallGenerationEvent.js";
9
10
  import { TranscriptionFinishedEvent, TranscriptionStartedEvent } from "./generate-transcription/TranscriptionEvent.js";
10
11
  export interface BaseModelCallStartedEvent extends BaseFunctionStartedEvent {
11
12
  model: ModelInformation;
@@ -47,5 +48,5 @@ export interface BaseModelCallFinishedEvent extends BaseFunctionFinishedEvent {
47
48
  */
48
49
  result: BaseModelCallFinishedEventResult;
49
50
  }
50
- export type ModelCallStartedEvent = EmbeddingStartedEvent | ImageGenerationStartedEvent | SpeechGenerationStartedEvent | SpeechStreamingStartedEvent | StructureGenerationStartedEvent | StructureStreamingStartedEvent | TextGenerationStartedEvent | TextStreamingStartedEvent | TranscriptionStartedEvent;
51
- export type ModelCallFinishedEvent = EmbeddingFinishedEvent | ImageGenerationFinishedEvent | SpeechGenerationFinishedEvent | SpeechStreamingFinishedEvent | StructureGenerationFinishedEvent | StructureStreamingFinishedEvent | TextGenerationFinishedEvent | TextStreamingFinishedEvent | TranscriptionFinishedEvent;
51
+ export type ModelCallStartedEvent = EmbeddingStartedEvent | ImageGenerationStartedEvent | SpeechGenerationStartedEvent | SpeechStreamingStartedEvent | StructureGenerationStartedEvent | StructureStreamingStartedEvent | TextGenerationStartedEvent | TextStreamingStartedEvent | ToolCallGenerationStartedEvent | TranscriptionStartedEvent;
52
+ export type ModelCallFinishedEvent = EmbeddingFinishedEvent | ImageGenerationFinishedEvent | SpeechGenerationFinishedEvent | SpeechStreamingFinishedEvent | StructureGenerationFinishedEvent | StructureStreamingFinishedEvent | TextGenerationFinishedEvent | TextStreamingFinishedEvent | ToolCallGenerationFinishedEvent | TranscriptionFinishedEvent;
@@ -37,5 +37,16 @@ class StructureValidationError extends Error {
37
37
  this.value = value;
38
38
  this.valueText = valueText;
39
39
  }
40
+ toJSON() {
41
+ return {
42
+ name: this.name,
43
+ message: this.message,
44
+ cause: this.cause,
45
+ stack: this.stack,
46
+ structureName: this.structureName,
47
+ value: this.value,
48
+ valueText: this.valueText,
49
+ };
50
+ }
40
51
  }
41
52
  exports.StructureValidationError = StructureValidationError;
@@ -9,4 +9,13 @@ export declare class StructureValidationError extends Error {
9
9
  valueText: string;
10
10
  cause: unknown;
11
11
  });
12
+ toJSON(): {
13
+ name: string;
14
+ message: string;
15
+ cause: unknown;
16
+ stack: string | undefined;
17
+ structureName: string;
18
+ value: unknown;
19
+ valueText: string;
20
+ };
12
21
  }
@@ -34,4 +34,15 @@ export class StructureValidationError extends Error {
34
34
  this.value = value;
35
35
  this.valueText = valueText;
36
36
  }
37
+ toJSON() {
38
+ return {
39
+ name: this.name,
40
+ message: this.message,
41
+ cause: this.cause,
42
+ stack: this.stack,
43
+ structureName: this.structureName,
44
+ value: this.value,
45
+ valueText: this.valueText,
46
+ };
47
+ }
37
48
  }
@@ -20,12 +20,5 @@ __exportStar(require("./TextGenerationEvent.cjs"), exports);
20
20
  __exportStar(require("./TextGenerationModel.cjs"), exports);
21
21
  __exportStar(require("./TextGenerationPromptFormat.cjs"), exports);
22
22
  __exportStar(require("./generateText.cjs"), exports);
23
- __exportStar(require("./prompt-format/AlpacaPromptFormat.cjs"), exports);
24
- __exportStar(require("./prompt-format/ChatPrompt.cjs"), exports);
25
- __exportStar(require("./prompt-format/InstructionPrompt.cjs"), exports);
26
- __exportStar(require("./prompt-format/Llama2PromptFormat.cjs"), exports);
27
- __exportStar(require("./prompt-format/TextPromptFormat.cjs"), exports);
28
- __exportStar(require("./prompt-format/VicunaPromptFormat.cjs"), exports);
29
- __exportStar(require("./prompt-format/trimChatPrompt.cjs"), exports);
30
- __exportStar(require("./prompt-format/validateChatPrompt.cjs"), exports);
23
+ __exportStar(require("./prompt-format/index.cjs"), exports);
31
24
  __exportStar(require("./streamText.cjs"), exports);
@@ -4,12 +4,5 @@ export * from "./TextGenerationEvent.js";
4
4
  export * from "./TextGenerationModel.js";
5
5
  export * from "./TextGenerationPromptFormat.js";
6
6
  export * from "./generateText.js";
7
- export * from "./prompt-format/AlpacaPromptFormat.js";
8
- export * from "./prompt-format/ChatPrompt.js";
9
- export * from "./prompt-format/InstructionPrompt.js";
10
- export * from "./prompt-format/Llama2PromptFormat.js";
11
- export * from "./prompt-format/TextPromptFormat.js";
12
- export * from "./prompt-format/VicunaPromptFormat.js";
13
- export * from "./prompt-format/trimChatPrompt.js";
14
- export * from "./prompt-format/validateChatPrompt.js";
7
+ export * from "./prompt-format/index.js";
15
8
  export * from "./streamText.js";
@@ -4,12 +4,5 @@ export * from "./TextGenerationEvent.js";
4
4
  export * from "./TextGenerationModel.js";
5
5
  export * from "./TextGenerationPromptFormat.js";
6
6
  export * from "./generateText.js";
7
- export * from "./prompt-format/AlpacaPromptFormat.js";
8
- export * from "./prompt-format/ChatPrompt.js";
9
- export * from "./prompt-format/InstructionPrompt.js";
10
- export * from "./prompt-format/Llama2PromptFormat.js";
11
- export * from "./prompt-format/TextPromptFormat.js";
12
- export * from "./prompt-format/VicunaPromptFormat.js";
13
- export * from "./prompt-format/trimChatPrompt.js";
14
- export * from "./prompt-format/validateChatPrompt.js";
7
+ export * from "./prompt-format/index.js";
15
8
  export * from "./streamText.js";
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.mapInstructionPromptToAlpacaFormat = void 0;
3
+ exports.instruction = void 0;
4
4
  const DEFAULT_SYSTEM_PROMPT_INPUT = "Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.";
5
5
  const DEFAULT_SYSTEM_PROMPT_NO_INPUT = "Below is an instruction that describes a task. Write a response that appropriately completes the request.";
6
6
  /**
@@ -9,9 +9,37 @@ const DEFAULT_SYSTEM_PROMPT_NO_INPUT = "Below is an instruction that describes a
9
9
  * If the instruction has a system prompt, it overrides the default system prompt
10
10
  * (which can impact the results, because the model may be trained on the default system prompt).
11
11
  *
12
+ * Prompt format with input:
13
+ * ```
14
+ * Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
15
+ *
16
+ * ### Instruction:
17
+ *
18
+ * {instruction}
19
+ *
20
+ * ### Input:
21
+ *
22
+ * {input}
23
+ *
24
+ * ### Response:
25
+ *
26
+ * ```
27
+ *
28
+ * Prompt format without input:
29
+ * ```
30
+ * Below is an instruction that describes a task. Write a response that appropriately completes the request.
31
+ *
32
+ * ### Instruction:
33
+ *
34
+ * {instruction}
35
+ *
36
+ * ### Response:
37
+ *
38
+ * ```
39
+ *
12
40
  * @see https://github.com/tatsu-lab/stanford_alpaca#data-release
13
41
  */
14
- function mapInstructionPromptToAlpacaFormat() {
42
+ function instruction() {
15
43
  return {
16
44
  stopSequences: [],
17
45
  format: (instruction) => {
@@ -32,4 +60,4 @@ function mapInstructionPromptToAlpacaFormat() {
32
60
  },
33
61
  };
34
62
  }
35
- exports.mapInstructionPromptToAlpacaFormat = mapInstructionPromptToAlpacaFormat;
63
+ exports.instruction = instruction;
@@ -6,6 +6,34 @@ import { TextGenerationPromptFormat } from "../TextGenerationPromptFormat.js";
6
6
  * If the instruction has a system prompt, it overrides the default system prompt
7
7
  * (which can impact the results, because the model may be trained on the default system prompt).
8
8
  *
9
+ * Prompt format with input:
10
+ * ```
11
+ * Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
12
+ *
13
+ * ### Instruction:
14
+ *
15
+ * {instruction}
16
+ *
17
+ * ### Input:
18
+ *
19
+ * {input}
20
+ *
21
+ * ### Response:
22
+ *
23
+ * ```
24
+ *
25
+ * Prompt format without input:
26
+ * ```
27
+ * Below is an instruction that describes a task. Write a response that appropriately completes the request.
28
+ *
29
+ * ### Instruction:
30
+ *
31
+ * {instruction}
32
+ *
33
+ * ### Response:
34
+ *
35
+ * ```
36
+ *
9
37
  * @see https://github.com/tatsu-lab/stanford_alpaca#data-release
10
38
  */
11
- export declare function mapInstructionPromptToAlpacaFormat(): TextGenerationPromptFormat<InstructionPrompt, string>;
39
+ export declare function instruction(): TextGenerationPromptFormat<InstructionPrompt, string>;
@@ -6,9 +6,37 @@ const DEFAULT_SYSTEM_PROMPT_NO_INPUT = "Below is an instruction that describes a
6
6
  * If the instruction has a system prompt, it overrides the default system prompt
7
7
  * (which can impact the results, because the model may be trained on the default system prompt).
8
8
  *
9
+ * Prompt format with input:
10
+ * ```
11
+ * Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
12
+ *
13
+ * ### Instruction:
14
+ *
15
+ * {instruction}
16
+ *
17
+ * ### Input:
18
+ *
19
+ * {input}
20
+ *
21
+ * ### Response:
22
+ *
23
+ * ```
24
+ *
25
+ * Prompt format without input:
26
+ * ```
27
+ * Below is an instruction that describes a task. Write a response that appropriately completes the request.
28
+ *
29
+ * ### Instruction:
30
+ *
31
+ * {instruction}
32
+ *
33
+ * ### Response:
34
+ *
35
+ * ```
36
+ *
9
37
  * @see https://github.com/tatsu-lab/stanford_alpaca#data-release
10
38
  */
11
- export function mapInstructionPromptToAlpacaFormat() {
39
+ export function instruction() {
12
40
  return {
13
41
  stopSequences: [],
14
42
  format: (instruction) => {
@@ -0,0 +1,79 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.chat = exports.instruction = void 0;
4
+ const validateChatPrompt_js_1 = require("./validateChatPrompt.cjs");
5
+ const START_SEGMENT = "<|im_start|>";
6
+ const END_SEGMENT = "<|im_end|>";
7
+ function chatMLStart(role) {
8
+ return `${START_SEGMENT}${role}\n`;
9
+ }
10
+ function chatMLSegment(role, text) {
11
+ return text == null ? "" : `${chatMLStart(role)}${text}${END_SEGMENT}\n`;
12
+ }
13
+ /**
14
+ * Formats an instruction prompt using the ChatML format.
15
+ *
16
+ * ChatML prompt template:
17
+ * ```
18
+ * <|im_start|>system
19
+ * You are a helpful assistant that answers questions about the world.<|im_end|>
20
+ * <|im_start|>user
21
+ * What is the capital of France?<|im_end|>
22
+ * <|im_start|>assistant
23
+ * Paris<|im_end|>
24
+ * ```
25
+ */
26
+ function instruction() {
27
+ return {
28
+ stopSequences: [END_SEGMENT],
29
+ format: (instruction) => chatMLSegment("system", instruction.system) +
30
+ chatMLSegment("user", instruction.instruction + instruction.input != null
31
+ ? `\n\n${instruction.input}`
32
+ : ""),
33
+ };
34
+ }
35
+ exports.instruction = instruction;
36
+ /**
37
+ * Formats a chat prompt using the ChatML format.
38
+ *
39
+ * ChatML prompt template:
40
+ * ```
41
+ * <|im_start|>system
42
+ * You are a helpful assistant that answers questions about the world.<|im_end|>
43
+ * <|im_start|>user
44
+ * What is the capital of France?<|im_end|>
45
+ * <|im_start|>assistant
46
+ * Paris<|im_end|>
47
+ * ```
48
+ */
49
+ function chat() {
50
+ return {
51
+ format: (chatPrompt) => {
52
+ (0, validateChatPrompt_js_1.validateChatPrompt)(chatPrompt);
53
+ let text = chatPrompt.system != null
54
+ ? chatMLSegment("system", chatPrompt.system)
55
+ : "";
56
+ for (const { role, content } of chatPrompt.messages) {
57
+ switch (role) {
58
+ case "user": {
59
+ text += chatMLSegment("user", content);
60
+ break;
61
+ }
62
+ case "assistant": {
63
+ text += chatMLSegment("assistant", content);
64
+ break;
65
+ }
66
+ default: {
67
+ const _exhaustiveCheck = role;
68
+ throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
69
+ }
70
+ }
71
+ }
72
+ // prefix start of assistant response:
73
+ text += chatMLStart("assistant");
74
+ return text;
75
+ },
76
+ stopSequences: [END_SEGMENT],
77
+ };
78
+ }
79
+ exports.chat = chat;
@@ -0,0 +1,31 @@
1
+ import { ChatPrompt } from "./ChatPrompt.js";
2
+ import { InstructionPrompt } from "./InstructionPrompt.js";
3
+ import { TextGenerationPromptFormat } from "../TextGenerationPromptFormat.js";
4
+ /**
5
+ * Formats an instruction prompt using the ChatML format.
6
+ *
7
+ * ChatML prompt template:
8
+ * ```
9
+ * <|im_start|>system
10
+ * You are a helpful assistant that answers questions about the world.<|im_end|>
11
+ * <|im_start|>user
12
+ * What is the capital of France?<|im_end|>
13
+ * <|im_start|>assistant
14
+ * Paris<|im_end|>
15
+ * ```
16
+ */
17
+ export declare function instruction(): TextGenerationPromptFormat<InstructionPrompt, string>;
18
+ /**
19
+ * Formats a chat prompt using the ChatML format.
20
+ *
21
+ * ChatML prompt template:
22
+ * ```
23
+ * <|im_start|>system
24
+ * You are a helpful assistant that answers questions about the world.<|im_end|>
25
+ * <|im_start|>user
26
+ * What is the capital of France?<|im_end|>
27
+ * <|im_start|>assistant
28
+ * Paris<|im_end|>
29
+ * ```
30
+ */
31
+ export declare function chat(): TextGenerationPromptFormat<ChatPrompt, string>;
@@ -0,0 +1,74 @@
1
+ import { validateChatPrompt } from "./validateChatPrompt.js";
2
+ const START_SEGMENT = "<|im_start|>";
3
+ const END_SEGMENT = "<|im_end|>";
4
+ function chatMLStart(role) {
5
+ return `${START_SEGMENT}${role}\n`;
6
+ }
7
+ function chatMLSegment(role, text) {
8
+ return text == null ? "" : `${chatMLStart(role)}${text}${END_SEGMENT}\n`;
9
+ }
10
+ /**
11
+ * Formats an instruction prompt using the ChatML format.
12
+ *
13
+ * ChatML prompt template:
14
+ * ```
15
+ * <|im_start|>system
16
+ * You are a helpful assistant that answers questions about the world.<|im_end|>
17
+ * <|im_start|>user
18
+ * What is the capital of France?<|im_end|>
19
+ * <|im_start|>assistant
20
+ * Paris<|im_end|>
21
+ * ```
22
+ */
23
+ export function instruction() {
24
+ return {
25
+ stopSequences: [END_SEGMENT],
26
+ format: (instruction) => chatMLSegment("system", instruction.system) +
27
+ chatMLSegment("user", instruction.instruction + instruction.input != null
28
+ ? `\n\n${instruction.input}`
29
+ : ""),
30
+ };
31
+ }
32
+ /**
33
+ * Formats a chat prompt using the ChatML format.
34
+ *
35
+ * ChatML prompt template:
36
+ * ```
37
+ * <|im_start|>system
38
+ * You are a helpful assistant that answers questions about the world.<|im_end|>
39
+ * <|im_start|>user
40
+ * What is the capital of France?<|im_end|>
41
+ * <|im_start|>assistant
42
+ * Paris<|im_end|>
43
+ * ```
44
+ */
45
+ export function chat() {
46
+ return {
47
+ format: (chatPrompt) => {
48
+ validateChatPrompt(chatPrompt);
49
+ let text = chatPrompt.system != null
50
+ ? chatMLSegment("system", chatPrompt.system)
51
+ : "";
52
+ for (const { role, content } of chatPrompt.messages) {
53
+ switch (role) {
54
+ case "user": {
55
+ text += chatMLSegment("user", content);
56
+ break;
57
+ }
58
+ case "assistant": {
59
+ text += chatMLSegment("assistant", content);
60
+ break;
61
+ }
62
+ default: {
63
+ const _exhaustiveCheck = role;
64
+ throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
65
+ }
66
+ }
67
+ }
68
+ // prefix start of assistant response:
69
+ text += chatMLStart("assistant");
70
+ return text;
71
+ },
72
+ stopSequences: [END_SEGMENT],
73
+ };
74
+ }
@@ -1,33 +1,38 @@
1
1
  /**
2
- * A chat prompt is a sequence of messages with the following structure:
2
+ * A chat prompt is a combination of a system message and a list of messages with the following constraints:
3
3
  *
4
- * - A chat prompt can optionally start with a system message.
5
- * - After the optional system message, the first message of the chat must be a user message.
6
- * - Then it must be alternating between an ai message and a user message.
7
- * - The last message must always be a user message.
4
+ * - A chat prompt can optionally have a system message.
5
+ * - The first message of the chat must be a user message.
6
+ * - Then it must be alternating between an assistant message and a user message.
7
+ * - The last message must always be a user message (when submitting to a model).
8
8
  *
9
- * The type checking is done at runtime, because there a no good ways to do it statically.
9
+ * You can use a ChatPrompt without an final user message when you e.g. want to display the current state of a conversation.
10
+ *
11
+ * The type checking is done at runtime when you submit a chat prompt to a model with a prompt format.
10
12
  *
11
13
  * @example
12
14
  * ```ts
13
- * [
14
- * { system: "You are a celebrated poet." },
15
- * { user: "Write a short story about a robot learning to love." },
16
- * { ai: "Once upon a time, there was a robot who learned to love." },
17
- * { user: "That's a great start!" },
18
- * ]
15
+ * const chatPrompt: ChatPrompt = {
16
+ * system: "You are a celebrated poet.",
17
+ * messages: [
18
+ * { role: "user", content: "Write a short story about a robot learning to love." },
19
+ * { role: "assistant", content: "Once upon a time, there was a robot who learned to love." },
20
+ * { role: "user", content: "That's a great start!" },
21
+ * ],
22
+ * };
19
23
  * ```
20
24
  *
21
25
  * @see validateChatPrompt
22
26
  */
23
- export type ChatPrompt = [...({
24
- user: string;
25
- } | {
26
- ai: string;
27
- })[]] | [{
28
- system: string;
29
- }, ...({
30
- user: string;
31
- } | {
32
- ai: string;
33
- })[]];
27
+ export type ChatPrompt = {
28
+ system?: string;
29
+ messages: Array<ChatMessage>;
30
+ };
31
+ /**
32
+ * A message in a chat prompt.
33
+ * @see ChatPrompt
34
+ */
35
+ export type ChatMessage = {
36
+ role: "user" | "assistant";
37
+ content: string;
38
+ };
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ChatPromptValidationError = void 0;
4
+ class ChatPromptValidationError extends Error {
5
+ constructor(message) {
6
+ super(message);
7
+ this.name = "ChatPromptValidationError";
8
+ }
9
+ toJSON() {
10
+ return {
11
+ name: this.name,
12
+ message: this.message,
13
+ stack: this.stack,
14
+ };
15
+ }
16
+ }
17
+ exports.ChatPromptValidationError = ChatPromptValidationError;
@@ -0,0 +1,8 @@
1
+ export declare class ChatPromptValidationError extends Error {
2
+ constructor(message: string);
3
+ toJSON(): {
4
+ name: string;
5
+ message: string;
6
+ stack: string | undefined;
7
+ };
8
+ }
@@ -0,0 +1,13 @@
1
+ export class ChatPromptValidationError extends Error {
2
+ constructor(message) {
3
+ super(message);
4
+ this.name = "ChatPromptValidationError";
5
+ }
6
+ toJSON() {
7
+ return {
8
+ name: this.name,
9
+ message: this.message,
10
+ stack: this.stack,
11
+ };
12
+ }
13
+ }