modelfusion 0.93.0 → 0.94.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/README.md CHANGED
@@ -581,6 +581,7 @@ const textStream = await streamText(
581
581
  | Anthropic | ✅ | ✅ | ✅ |
582
582
  | Llama 2 | ✅ | ✅ | ✅ |
583
583
  | ChatML | ✅ | ✅ | ✅ |
584
+ | NeuralChat | ✅ | ✅ | ✅ |
584
585
  | Alpaca | ✅ | ✅ | ❌ |
585
586
  | Vicuna | ❌ | ❌ | ✅ |
586
587
  | Generic Text | ✅ | ✅ | ✅ |
@@ -4,11 +4,11 @@ exports.chat = exports.instruction = exports.text = void 0;
4
4
  const ChatPrompt_js_1 = require("./ChatPrompt.cjs");
5
5
  const START_SEGMENT = "<|im_start|>";
6
6
  const END_SEGMENT = "<|im_end|>";
7
- function chatMLStart(role) {
7
+ function segmentStart(role) {
8
8
  return `${START_SEGMENT}${role}\n`;
9
9
  }
10
- function chatMLSegment(role, text) {
11
- return text == null ? "" : `${chatMLStart(role)}${text}${END_SEGMENT}\n`;
10
+ function segment(role, text) {
11
+ return text == null ? "" : `${segmentStart(role)}${text}${END_SEGMENT}\n`;
12
12
  }
13
13
  /**
14
14
  * Formats a text prompt using the ChatML format.
@@ -18,7 +18,7 @@ function text() {
18
18
  stopSequences: [END_SEGMENT],
19
19
  format(prompt) {
20
20
  // prompt and then prefix start of assistant response:
21
- return chatMLSegment("user", prompt) + chatMLStart("assistant");
21
+ return segment("user", prompt) + segmentStart("assistant");
22
22
  },
23
23
  };
24
24
  }
@@ -40,9 +40,9 @@ function instruction() {
40
40
  return {
41
41
  stopSequences: [END_SEGMENT],
42
42
  format(prompt) {
43
- return (chatMLSegment("system", prompt.system) +
44
- chatMLSegment("user", prompt.instruction) +
45
- chatMLStart("assistant") +
43
+ return (segment("system", prompt.system) +
44
+ segment("user", prompt.instruction) +
45
+ segmentStart("assistant") +
46
46
  (prompt.responsePrefix ?? ""));
47
47
  },
48
48
  };
@@ -65,15 +65,15 @@ function chat() {
65
65
  return {
66
66
  format(prompt) {
67
67
  (0, ChatPrompt_js_1.validateChatPrompt)(prompt);
68
- let text = prompt.system != null ? chatMLSegment("system", prompt.system) : "";
68
+ let text = prompt.system != null ? segment("system", prompt.system) : "";
69
69
  for (const { role, content } of prompt.messages) {
70
70
  switch (role) {
71
71
  case "user": {
72
- text += chatMLSegment("user", content);
72
+ text += segment("user", content);
73
73
  break;
74
74
  }
75
75
  case "assistant": {
76
- text += chatMLSegment("assistant", content);
76
+ text += segment("assistant", content);
77
77
  break;
78
78
  }
79
79
  default: {
@@ -83,7 +83,7 @@ function chat() {
83
83
  }
84
84
  }
85
85
  // prefix start of assistant response:
86
- text += chatMLStart("assistant");
86
+ text += segmentStart("assistant");
87
87
  return text;
88
88
  },
89
89
  stopSequences: [END_SEGMENT],
@@ -1,11 +1,11 @@
1
1
  import { validateChatPrompt } from "./ChatPrompt.js";
2
2
  const START_SEGMENT = "<|im_start|>";
3
3
  const END_SEGMENT = "<|im_end|>";
4
- function chatMLStart(role) {
4
+ function segmentStart(role) {
5
5
  return `${START_SEGMENT}${role}\n`;
6
6
  }
7
- function chatMLSegment(role, text) {
8
- return text == null ? "" : `${chatMLStart(role)}${text}${END_SEGMENT}\n`;
7
+ function segment(role, text) {
8
+ return text == null ? "" : `${segmentStart(role)}${text}${END_SEGMENT}\n`;
9
9
  }
10
10
  /**
11
11
  * Formats a text prompt using the ChatML format.
@@ -15,7 +15,7 @@ export function text() {
15
15
  stopSequences: [END_SEGMENT],
16
16
  format(prompt) {
17
17
  // prompt and then prefix start of assistant response:
18
- return chatMLSegment("user", prompt) + chatMLStart("assistant");
18
+ return segment("user", prompt) + segmentStart("assistant");
19
19
  },
20
20
  };
21
21
  }
@@ -36,9 +36,9 @@ export function instruction() {
36
36
  return {
37
37
  stopSequences: [END_SEGMENT],
38
38
  format(prompt) {
39
- return (chatMLSegment("system", prompt.system) +
40
- chatMLSegment("user", prompt.instruction) +
41
- chatMLStart("assistant") +
39
+ return (segment("system", prompt.system) +
40
+ segment("user", prompt.instruction) +
41
+ segmentStart("assistant") +
42
42
  (prompt.responsePrefix ?? ""));
43
43
  },
44
44
  };
@@ -60,15 +60,15 @@ export function chat() {
60
60
  return {
61
61
  format(prompt) {
62
62
  validateChatPrompt(prompt);
63
- let text = prompt.system != null ? chatMLSegment("system", prompt.system) : "";
63
+ let text = prompt.system != null ? segment("system", prompt.system) : "";
64
64
  for (const { role, content } of prompt.messages) {
65
65
  switch (role) {
66
66
  case "user": {
67
- text += chatMLSegment("user", content);
67
+ text += segment("user", content);
68
68
  break;
69
69
  }
70
70
  case "assistant": {
71
- text += chatMLSegment("assistant", content);
71
+ text += segment("assistant", content);
72
72
  break;
73
73
  }
74
74
  default: {
@@ -78,7 +78,7 @@ export function chat() {
78
78
  }
79
79
  }
80
80
  // prefix start of assistant response:
81
- text += chatMLStart("assistant");
81
+ text += segmentStart("assistant");
82
82
  return text;
83
83
  },
84
84
  stopSequences: [END_SEGMENT],
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.chat = exports.instruction = exports.text = void 0;
4
+ const ChatPrompt_js_1 = require("./ChatPrompt.cjs");
5
+ const roleNames = {
6
+ system: "System",
7
+ user: "User",
8
+ assistant: "Assistant",
9
+ };
10
+ function segmentStart(role) {
11
+ return `### ${roleNames[role]}:\n`;
12
+ }
13
+ function segment(role, text) {
14
+ return text == null ? "" : `${segmentStart(role)}${text}\n`;
15
+ }
16
+ /**
17
+ * Formats a text prompt as a neural chat prompt.
18
+ *
19
+ * @see https://huggingface.co/Intel/neural-chat-7b-v3-1#prompt-template
20
+ */
21
+ function text() {
22
+ return {
23
+ stopSequences: [],
24
+ format(prompt) {
25
+ // prompt and then prefix start of assistant response:
26
+ return segment("user", prompt) + segmentStart("assistant");
27
+ },
28
+ };
29
+ }
30
+ exports.text = text;
31
+ /**
32
+ * Formats an instruction prompt as a neural chat prompt.
33
+ *
34
+ * @see https://huggingface.co/Intel/neural-chat-7b-v3-1#prompt-template
35
+ */
36
+ const instruction = () => ({
37
+ stopSequences: [],
38
+ format(prompt) {
39
+ return (segment("system", prompt.system) +
40
+ segment("user", prompt.instruction) +
41
+ segmentStart("assistant") +
42
+ (prompt.responsePrefix ?? ""));
43
+ },
44
+ });
45
+ exports.instruction = instruction;
46
+ /**
47
+ * Formats a chat prompt as a basic text prompt.
48
+ *
49
+ * @param user The label of the user in the chat. Default to "user".
50
+ * @param assistant The label of the assistant in the chat. Default to "assistant".
51
+ * @param system The label of the system in the chat. Optional, defaults to no prefix.
52
+ */
53
+ function chat() {
54
+ return {
55
+ format(prompt) {
56
+ (0, ChatPrompt_js_1.validateChatPrompt)(prompt);
57
+ let text = prompt.system != null ? segment("system", prompt.system) : "";
58
+ for (const { role, content } of prompt.messages) {
59
+ switch (role) {
60
+ case "user": {
61
+ text += segment("user", content);
62
+ break;
63
+ }
64
+ case "assistant": {
65
+ text += segment("assistant", content);
66
+ break;
67
+ }
68
+ default: {
69
+ const _exhaustiveCheck = role;
70
+ throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
71
+ }
72
+ }
73
+ }
74
+ // prefix start of assistant response:
75
+ text += segmentStart("assistant");
76
+ return text;
77
+ },
78
+ stopSequences: [`\n${roleNames.user}:`],
79
+ };
80
+ }
81
+ exports.chat = chat;
@@ -0,0 +1,23 @@
1
+ import { TextGenerationPromptTemplate } from "../TextGenerationPromptTemplate.js";
2
+ import { TextChatPrompt } from "./ChatPrompt.js";
3
+ import { TextInstructionPrompt } from "./InstructionPrompt.js";
4
+ /**
5
+ * Formats a text prompt as a neural chat prompt.
6
+ *
7
+ * @see https://huggingface.co/Intel/neural-chat-7b-v3-1#prompt-template
8
+ */
9
+ export declare function text(): TextGenerationPromptTemplate<string, string>;
10
+ /**
11
+ * Formats an instruction prompt as a neural chat prompt.
12
+ *
13
+ * @see https://huggingface.co/Intel/neural-chat-7b-v3-1#prompt-template
14
+ */
15
+ export declare const instruction: () => TextGenerationPromptTemplate<TextInstructionPrompt, string>;
16
+ /**
17
+ * Formats a chat prompt as a basic text prompt.
18
+ *
19
+ * @param user The label of the user in the chat. Default to "user".
20
+ * @param assistant The label of the assistant in the chat. Default to "assistant".
21
+ * @param system The label of the system in the chat. Optional, defaults to no prefix.
22
+ */
23
+ export declare function chat(): TextGenerationPromptTemplate<TextChatPrompt, string>;
@@ -0,0 +1,75 @@
1
+ import { validateChatPrompt } from "./ChatPrompt.js";
2
+ const roleNames = {
3
+ system: "System",
4
+ user: "User",
5
+ assistant: "Assistant",
6
+ };
7
+ function segmentStart(role) {
8
+ return `### ${roleNames[role]}:\n`;
9
+ }
10
+ function segment(role, text) {
11
+ return text == null ? "" : `${segmentStart(role)}${text}\n`;
12
+ }
13
+ /**
14
+ * Formats a text prompt as a neural chat prompt.
15
+ *
16
+ * @see https://huggingface.co/Intel/neural-chat-7b-v3-1#prompt-template
17
+ */
18
+ export function text() {
19
+ return {
20
+ stopSequences: [],
21
+ format(prompt) {
22
+ // prompt and then prefix start of assistant response:
23
+ return segment("user", prompt) + segmentStart("assistant");
24
+ },
25
+ };
26
+ }
27
+ /**
28
+ * Formats an instruction prompt as a neural chat prompt.
29
+ *
30
+ * @see https://huggingface.co/Intel/neural-chat-7b-v3-1#prompt-template
31
+ */
32
+ export const instruction = () => ({
33
+ stopSequences: [],
34
+ format(prompt) {
35
+ return (segment("system", prompt.system) +
36
+ segment("user", prompt.instruction) +
37
+ segmentStart("assistant") +
38
+ (prompt.responsePrefix ?? ""));
39
+ },
40
+ });
41
+ /**
42
+ * Formats a chat prompt as a basic text prompt.
43
+ *
44
+ * @param user The label of the user in the chat. Default to "user".
45
+ * @param assistant The label of the assistant in the chat. Default to "assistant".
46
+ * @param system The label of the system in the chat. Optional, defaults to no prefix.
47
+ */
48
+ export function chat() {
49
+ return {
50
+ format(prompt) {
51
+ validateChatPrompt(prompt);
52
+ let text = prompt.system != null ? segment("system", prompt.system) : "";
53
+ for (const { role, content } of prompt.messages) {
54
+ switch (role) {
55
+ case "user": {
56
+ text += segment("user", content);
57
+ break;
58
+ }
59
+ case "assistant": {
60
+ text += segment("assistant", content);
61
+ break;
62
+ }
63
+ default: {
64
+ const _exhaustiveCheck = role;
65
+ throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
66
+ }
67
+ }
68
+ }
69
+ // prefix start of assistant response:
70
+ text += segmentStart("assistant");
71
+ return text;
72
+ },
73
+ stopSequences: [`\n${roleNames.user}:`],
74
+ };
75
+ }
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const NeuralChatPromptTemplate_js_1 = require("./NeuralChatPromptTemplate.cjs");
4
+ describe("text prompt", () => {
5
+ it("should format prompt", () => {
6
+ const prompt = (0, NeuralChatPromptTemplate_js_1.text)().format("prompt");
7
+ expect(prompt).toMatchSnapshot();
8
+ });
9
+ });
10
+ describe("instruction prompt", () => {
11
+ it("should format prompt with instruction", () => {
12
+ const prompt = (0, NeuralChatPromptTemplate_js_1.instruction)().format({
13
+ instruction: "instruction",
14
+ });
15
+ expect(prompt).toMatchSnapshot();
16
+ });
17
+ it("should format prompt with system and instruction", () => {
18
+ const prompt = (0, NeuralChatPromptTemplate_js_1.instruction)().format({
19
+ system: "system",
20
+ instruction: "instruction",
21
+ });
22
+ expect(prompt).toMatchSnapshot();
23
+ });
24
+ it("should format prompt with instruction and response prefix", () => {
25
+ const prompt = (0, NeuralChatPromptTemplate_js_1.instruction)().format({
26
+ instruction: "instruction",
27
+ responsePrefix: "response prefix",
28
+ });
29
+ expect(prompt).toMatchSnapshot();
30
+ });
31
+ });
32
+ describe("chat prompt", () => {
33
+ it("should format prompt with user message", () => {
34
+ const prompt = (0, NeuralChatPromptTemplate_js_1.chat)().format({
35
+ messages: [{ role: "user", content: "user message" }],
36
+ });
37
+ expect(prompt).toMatchSnapshot();
38
+ });
39
+ it("should format prompt with user-assistant-user messages", () => {
40
+ const prompt = (0, NeuralChatPromptTemplate_js_1.chat)().format({
41
+ messages: [
42
+ { role: "user", content: "1st user message" },
43
+ { role: "assistant", content: "assistant message" },
44
+ { role: "user", content: "2nd user message" },
45
+ ],
46
+ });
47
+ expect(prompt).toMatchSnapshot();
48
+ });
49
+ });
@@ -0,0 +1,47 @@
1
+ import { chat, instruction, text } from "./NeuralChatPromptTemplate.js";
2
+ describe("text prompt", () => {
3
+ it("should format prompt", () => {
4
+ const prompt = text().format("prompt");
5
+ expect(prompt).toMatchSnapshot();
6
+ });
7
+ });
8
+ describe("instruction prompt", () => {
9
+ it("should format prompt with instruction", () => {
10
+ const prompt = instruction().format({
11
+ instruction: "instruction",
12
+ });
13
+ expect(prompt).toMatchSnapshot();
14
+ });
15
+ it("should format prompt with system and instruction", () => {
16
+ const prompt = instruction().format({
17
+ system: "system",
18
+ instruction: "instruction",
19
+ });
20
+ expect(prompt).toMatchSnapshot();
21
+ });
22
+ it("should format prompt with instruction and response prefix", () => {
23
+ const prompt = instruction().format({
24
+ instruction: "instruction",
25
+ responsePrefix: "response prefix",
26
+ });
27
+ expect(prompt).toMatchSnapshot();
28
+ });
29
+ });
30
+ describe("chat prompt", () => {
31
+ it("should format prompt with user message", () => {
32
+ const prompt = chat().format({
33
+ messages: [{ role: "user", content: "user message" }],
34
+ });
35
+ expect(prompt).toMatchSnapshot();
36
+ });
37
+ it("should format prompt with user-assistant-user messages", () => {
38
+ const prompt = chat().format({
39
+ messages: [
40
+ { role: "user", content: "1st user message" },
41
+ { role: "assistant", content: "assistant message" },
42
+ { role: "user", content: "2nd user message" },
43
+ ],
44
+ });
45
+ expect(prompt).toMatchSnapshot();
46
+ });
47
+ });
@@ -20,9 +20,9 @@ const instruction = () => ({
20
20
  if (prompt.system != null) {
21
21
  text += `${prompt.system}\n\n`;
22
22
  }
23
- text += prompt.instruction;
23
+ text += `${prompt.instruction}\n\n`;
24
24
  if (prompt.responsePrefix != null) {
25
- text += `\n\n${prompt.responsePrefix}`;
25
+ text += prompt.responsePrefix;
26
26
  }
27
27
  return text;
28
28
  },
@@ -16,9 +16,9 @@ export const instruction = () => ({
16
16
  if (prompt.system != null) {
17
17
  text += `${prompt.system}\n\n`;
18
18
  }
19
- text += prompt.instruction;
19
+ text += `${prompt.instruction}\n\n`;
20
20
  if (prompt.responsePrefix != null) {
21
- text += `\n\n${prompt.responsePrefix}`;
21
+ text += prompt.responsePrefix;
22
22
  }
23
23
  return text;
24
24
  },
@@ -26,14 +26,15 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
26
26
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.VicunaPrompt = exports.TextPrompt = exports.Llama2Prompt = exports.ChatMLPrompt = exports.AlpacaPrompt = void 0;
29
+ exports.VicunaPrompt = exports.TextPrompt = exports.NeuralChatPrompt = exports.Llama2Prompt = exports.ChatMLPrompt = exports.AlpacaPrompt = void 0;
30
30
  exports.AlpacaPrompt = __importStar(require("./AlpacaPromptTemplate.cjs"));
31
31
  exports.ChatMLPrompt = __importStar(require("./ChatMLPromptTemplate.cjs"));
32
32
  __exportStar(require("./ChatPrompt.cjs"), exports);
33
33
  __exportStar(require("./Content.cjs"), exports);
34
34
  __exportStar(require("./InstructionPrompt.cjs"), exports);
35
- exports.Llama2Prompt = __importStar(require("./Llama2PromptTemplate.cjs"));
36
35
  __exportStar(require("./InvalidPromptError.cjs"), exports);
36
+ exports.Llama2Prompt = __importStar(require("./Llama2PromptTemplate.cjs"));
37
+ exports.NeuralChatPrompt = __importStar(require("./NeuralChatPromptTemplate.cjs"));
37
38
  exports.TextPrompt = __importStar(require("./TextPromptTemplate.cjs"));
38
39
  exports.VicunaPrompt = __importStar(require("./VicunaPromptTemplate.cjs"));
39
40
  __exportStar(require("./trimChatPrompt.cjs"), exports);
@@ -3,8 +3,9 @@ export * as ChatMLPrompt from "./ChatMLPromptTemplate.js";
3
3
  export * from "./ChatPrompt.js";
4
4
  export * from "./Content.js";
5
5
  export * from "./InstructionPrompt.js";
6
- export * as Llama2Prompt from "./Llama2PromptTemplate.js";
7
6
  export * from "./InvalidPromptError.js";
7
+ export * as Llama2Prompt from "./Llama2PromptTemplate.js";
8
+ export * as NeuralChatPrompt from "./NeuralChatPromptTemplate.js";
8
9
  export * as TextPrompt from "./TextPromptTemplate.js";
9
10
  export * as VicunaPrompt from "./VicunaPromptTemplate.js";
10
11
  export * from "./trimChatPrompt.js";
@@ -3,8 +3,9 @@ export * as ChatMLPrompt from "./ChatMLPromptTemplate.js";
3
3
  export * from "./ChatPrompt.js";
4
4
  export * from "./Content.js";
5
5
  export * from "./InstructionPrompt.js";
6
- export * as Llama2Prompt from "./Llama2PromptTemplate.js";
7
6
  export * from "./InvalidPromptError.js";
7
+ export * as Llama2Prompt from "./Llama2PromptTemplate.js";
8
+ export * as NeuralChatPrompt from "./NeuralChatPromptTemplate.js";
8
9
  export * as TextPrompt from "./TextPromptTemplate.js";
9
10
  export * as VicunaPrompt from "./VicunaPromptTemplate.js";
10
11
  export * from "./trimChatPrompt.js";
@@ -26,11 +26,11 @@ var __importStar = (this && this.__importStar) || function (mod) {
26
26
  return result;
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.AnthropicPromptTemplate = exports.anthropic = exports.anthropicErrorDataSchema = exports.AnthropicError = void 0;
29
+ exports.AnthropicPrompt = exports.anthropic = exports.anthropicErrorDataSchema = exports.AnthropicError = void 0;
30
30
  __exportStar(require("./AnthropicApiConfiguration.cjs"), exports);
31
31
  var AnthropicError_js_1 = require("./AnthropicError.cjs");
32
32
  Object.defineProperty(exports, "AnthropicError", { enumerable: true, get: function () { return AnthropicError_js_1.AnthropicError; } });
33
33
  Object.defineProperty(exports, "anthropicErrorDataSchema", { enumerable: true, get: function () { return AnthropicError_js_1.anthropicErrorDataSchema; } });
34
34
  exports.anthropic = __importStar(require("./AnthropicFacade.cjs"));
35
- exports.AnthropicPromptTemplate = __importStar(require("./AnthropicPromptTemplate.cjs"));
35
+ exports.AnthropicPrompt = __importStar(require("./AnthropicPromptTemplate.cjs"));
36
36
  __exportStar(require("./AnthropicTextGenerationModel.cjs"), exports);
@@ -1,5 +1,5 @@
1
1
  export * from "./AnthropicApiConfiguration.js";
2
2
  export { AnthropicError, anthropicErrorDataSchema } from "./AnthropicError.js";
3
3
  export * as anthropic from "./AnthropicFacade.js";
4
- export * as AnthropicPromptTemplate from "./AnthropicPromptTemplate.js";
4
+ export * as AnthropicPrompt from "./AnthropicPromptTemplate.js";
5
5
  export * from "./AnthropicTextGenerationModel.js";
@@ -1,5 +1,5 @@
1
1
  export * from "./AnthropicApiConfiguration.js";
2
2
  export { AnthropicError, anthropicErrorDataSchema } from "./AnthropicError.js";
3
3
  export * as anthropic from "./AnthropicFacade.js";
4
- export * as AnthropicPromptTemplate from "./AnthropicPromptTemplate.js";
4
+ export * as AnthropicPrompt from "./AnthropicPromptTemplate.js";
5
5
  export * from "./AnthropicTextGenerationModel.js";
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "modelfusion",
3
3
  "description": "The TypeScript library for building multi-modal AI applications.",
4
- "version": "0.93.0",
4
+ "version": "0.94.0",
5
5
  "author": "Lars Grammel",
6
6
  "license": "MIT",
7
7
  "keywords": [