modelfusion 0.103.0 → 0.104.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 +7 -0
- package/model-function/generate-text/prompt-template/AlpacaPromptTemplate.cjs +2 -1
- package/model-function/generate-text/prompt-template/AlpacaPromptTemplate.d.ts +2 -2
- package/model-function/generate-text/prompt-template/AlpacaPromptTemplate.js +2 -1
- package/model-function/generate-text/prompt-template/ChatMLPromptTemplate.cjs +5 -4
- package/model-function/generate-text/prompt-template/ChatMLPromptTemplate.d.ts +4 -4
- package/model-function/generate-text/prompt-template/ChatMLPromptTemplate.js +5 -4
- package/model-function/generate-text/prompt-template/ChatPrompt.cjs +0 -24
- package/model-function/generate-text/prompt-template/ChatPrompt.d.ts +11 -34
- package/model-function/generate-text/prompt-template/ChatPrompt.js +1 -22
- package/model-function/generate-text/prompt-template/Content.cjs +9 -0
- package/model-function/generate-text/prompt-template/Content.d.ts +9 -4
- package/model-function/generate-text/prompt-template/Content.js +7 -1
- package/model-function/generate-text/prompt-template/InstructionPrompt.d.ts +6 -22
- package/model-function/generate-text/prompt-template/Llama2PromptTemplate.cjs +36 -5
- package/model-function/generate-text/prompt-template/Llama2PromptTemplate.d.ts +16 -4
- package/model-function/generate-text/prompt-template/Llama2PromptTemplate.js +34 -4
- package/model-function/generate-text/prompt-template/NeuralChatPromptTemplate.cjs +5 -4
- package/model-function/generate-text/prompt-template/NeuralChatPromptTemplate.d.ts +4 -4
- package/model-function/generate-text/prompt-template/NeuralChatPromptTemplate.js +5 -4
- package/model-function/generate-text/prompt-template/TextPromptTemplate.cjs +3 -4
- package/model-function/generate-text/prompt-template/TextPromptTemplate.d.ts +4 -4
- package/model-function/generate-text/prompt-template/TextPromptTemplate.js +3 -4
- package/model-function/generate-text/prompt-template/VicunaPromptTemplate.cjs +3 -3
- package/model-function/generate-text/prompt-template/VicunaPromptTemplate.d.ts +2 -2
- package/model-function/generate-text/prompt-template/VicunaPromptTemplate.js +3 -3
- package/model-function/generate-text/prompt-template/trimChatPrompt.cjs +0 -2
- package/model-function/generate-text/prompt-template/trimChatPrompt.d.ts +4 -4
- package/model-function/generate-text/prompt-template/trimChatPrompt.js +0 -2
- package/model-provider/anthropic/AnthropicPromptTemplate.cjs +5 -4
- package/model-provider/anthropic/AnthropicPromptTemplate.d.ts +4 -4
- package/model-provider/anthropic/AnthropicPromptTemplate.js +5 -4
- package/model-provider/anthropic/AnthropicTextGenerationModel.d.ts +2 -2
- package/model-provider/automatic1111/Automatic1111ImageGenerationModel.d.ts +1 -1
- package/model-provider/cohere/CohereTextGenerationModel.d.ts +2 -2
- package/model-provider/llamacpp/LlamaCppBakLLaVA1PromptTemplate.cjs +23 -16
- package/model-provider/llamacpp/LlamaCppBakLLaVA1PromptTemplate.d.ts +4 -4
- package/model-provider/llamacpp/LlamaCppBakLLaVA1PromptTemplate.js +23 -16
- package/model-provider/mistral/MistralChatModel.d.ts +2 -2
- package/model-provider/mistral/MistralPromptTemplate.cjs +5 -4
- package/model-provider/mistral/MistralPromptTemplate.d.ts +4 -4
- package/model-provider/mistral/MistralPromptTemplate.js +5 -4
- package/model-provider/ollama/OllamaChatModel.d.ts +2 -2
- package/model-provider/ollama/OllamaChatPromptTemplate.cjs +9 -13
- package/model-provider/ollama/OllamaChatPromptTemplate.d.ts +4 -4
- package/model-provider/ollama/OllamaChatPromptTemplate.js +9 -13
- package/model-provider/openai/OpenAICompletionModel.d.ts +2 -2
- package/model-provider/openai/chat/OpenAIChatFunctionCallStructureGenerationModel.d.ts +2 -2
- package/model-provider/openai/chat/OpenAIChatMessage.d.ts +2 -2
- package/model-provider/openai/chat/OpenAIChatModel.d.ts +2 -2
- package/model-provider/openai/chat/OpenAIChatPromptTemplate.cjs +0 -2
- package/model-provider/openai/chat/OpenAIChatPromptTemplate.d.ts +4 -4
- package/model-provider/openai/chat/OpenAIChatPromptTemplate.js +0 -2
- package/model-provider/openai-compatible/OpenAICompatibleChatModel.d.ts +2 -2
- package/model-provider/stability/StabilityImageGenerationModel.d.ts +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
@@ -1,5 +1,12 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## v0.104.0 - 2023-12-24
|
4
|
+
|
5
|
+
### Changed
|
6
|
+
|
7
|
+
- **breaking change**: Unified text and multimodal prompt templates. `[Text/MultiModal]InstructionPrompt` is now `InstructionPrompt`, and `[Text/MultiModalChatPrompt]` is now `ChatPrompt`.
|
8
|
+
- More flexible chat prompts: The chat prompt validation is now chat template specific and validated at runtime. E.g. the Llama2 prompt template only supports turns of user and assistant messages, whereas other formats are more flexible.
|
9
|
+
|
3
10
|
## v0.103.0 - 2023-12-23
|
4
11
|
|
5
12
|
### Added
|
@@ -1,6 +1,7 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.instruction = exports.text = void 0;
|
4
|
+
const Content_js_1 = require("./Content.cjs");
|
4
5
|
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
6
|
const DEFAULT_SYSTEM_PROMPT_NO_INPUT = "Below is an instruction that describes a task. Write a response that appropriately completes the request.";
|
6
7
|
/**
|
@@ -67,7 +68,7 @@ function instruction() {
|
|
67
68
|
if (prompt.system != null) {
|
68
69
|
text += `${prompt.system}\n`;
|
69
70
|
}
|
70
|
-
text += prompt.instruction;
|
71
|
+
text += (0, Content_js_1.validateContentIsString)(prompt.instruction, prompt);
|
71
72
|
if (prompt.input != null) {
|
72
73
|
text += `\n\n### Input:\n${prompt.input}`;
|
73
74
|
}
|
@@ -1,5 +1,5 @@
|
|
1
1
|
import { TextGenerationPromptTemplate } from "../TextGenerationPromptTemplate.js";
|
2
|
-
import {
|
2
|
+
import { InstructionPrompt } from "./InstructionPrompt.js";
|
3
3
|
/**
|
4
4
|
* Formats a text prompt as an Alpaca prompt.
|
5
5
|
*/
|
@@ -40,7 +40,7 @@ export declare function text(): TextGenerationPromptTemplate<string, string>;
|
|
40
40
|
*
|
41
41
|
* @see https://github.com/tatsu-lab/stanford_alpaca#data-release
|
42
42
|
*/
|
43
|
-
export declare function instruction(): TextGenerationPromptTemplate<
|
43
|
+
export declare function instruction(): TextGenerationPromptTemplate<InstructionPrompt & {
|
44
44
|
input?: string;
|
45
45
|
}, // optional input supported by Alpaca
|
46
46
|
string>;
|
@@ -1,3 +1,4 @@
|
|
1
|
+
import { validateContentIsString } from "./Content.js";
|
1
2
|
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.";
|
2
3
|
const DEFAULT_SYSTEM_PROMPT_NO_INPUT = "Below is an instruction that describes a task. Write a response that appropriately completes the request.";
|
3
4
|
/**
|
@@ -63,7 +64,7 @@ export function instruction() {
|
|
63
64
|
if (prompt.system != null) {
|
64
65
|
text += `${prompt.system}\n`;
|
65
66
|
}
|
66
|
-
text += prompt.instruction;
|
67
|
+
text += validateContentIsString(prompt.instruction, prompt);
|
67
68
|
if (prompt.input != null) {
|
68
69
|
text += `\n\n### Input:\n${prompt.input}`;
|
69
70
|
}
|
@@ -1,7 +1,7 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.chat = exports.instruction = exports.text = void 0;
|
4
|
-
const
|
4
|
+
const Content_js_1 = require("./Content.cjs");
|
5
5
|
const START_SEGMENT = "<|im_start|>";
|
6
6
|
const END_SEGMENT = "<|im_end|>";
|
7
7
|
function segmentStart(role) {
|
@@ -40,8 +40,9 @@ function instruction() {
|
|
40
40
|
return {
|
41
41
|
stopSequences: [END_SEGMENT],
|
42
42
|
format(prompt) {
|
43
|
+
const instruction = (0, Content_js_1.validateContentIsString)(prompt.instruction, prompt);
|
43
44
|
return (segment("system", prompt.system) +
|
44
|
-
segment("user",
|
45
|
+
segment("user", instruction) +
|
45
46
|
segmentStart("assistant") +
|
46
47
|
(prompt.responsePrefix ?? ""));
|
47
48
|
},
|
@@ -64,12 +65,12 @@ exports.instruction = instruction;
|
|
64
65
|
function chat() {
|
65
66
|
return {
|
66
67
|
format(prompt) {
|
67
|
-
(0, ChatPrompt_js_1.validateChatPrompt)(prompt);
|
68
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
|
-
|
72
|
+
const textContent = (0, Content_js_1.validateContentIsString)(content, prompt);
|
73
|
+
text += segment("user", textContent);
|
73
74
|
break;
|
74
75
|
}
|
75
76
|
case "assistant": {
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import { TextGenerationPromptTemplate } from "../TextGenerationPromptTemplate.js";
|
2
|
-
import {
|
3
|
-
import {
|
2
|
+
import { ChatPrompt } from "./ChatPrompt.js";
|
3
|
+
import { InstructionPrompt } from "./InstructionPrompt.js";
|
4
4
|
/**
|
5
5
|
* Formats a text prompt using the ChatML format.
|
6
6
|
*/
|
@@ -18,7 +18,7 @@ export declare function text(): TextGenerationPromptTemplate<string, string>;
|
|
18
18
|
* ${response prefix}
|
19
19
|
* ```
|
20
20
|
*/
|
21
|
-
export declare function instruction(): TextGenerationPromptTemplate<
|
21
|
+
export declare function instruction(): TextGenerationPromptTemplate<InstructionPrompt, string>;
|
22
22
|
/**
|
23
23
|
* Formats a chat prompt using the ChatML format.
|
24
24
|
*
|
@@ -32,4 +32,4 @@ export declare function instruction(): TextGenerationPromptTemplate<TextInstruct
|
|
32
32
|
* Paris<|im_end|>
|
33
33
|
* ```
|
34
34
|
*/
|
35
|
-
export declare function chat(): TextGenerationPromptTemplate<
|
35
|
+
export declare function chat(): TextGenerationPromptTemplate<ChatPrompt, string>;
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import {
|
1
|
+
import { validateContentIsString } from "./Content.js";
|
2
2
|
const START_SEGMENT = "<|im_start|>";
|
3
3
|
const END_SEGMENT = "<|im_end|>";
|
4
4
|
function segmentStart(role) {
|
@@ -36,8 +36,9 @@ export function instruction() {
|
|
36
36
|
return {
|
37
37
|
stopSequences: [END_SEGMENT],
|
38
38
|
format(prompt) {
|
39
|
+
const instruction = validateContentIsString(prompt.instruction, prompt);
|
39
40
|
return (segment("system", prompt.system) +
|
40
|
-
segment("user",
|
41
|
+
segment("user", instruction) +
|
41
42
|
segmentStart("assistant") +
|
42
43
|
(prompt.responsePrefix ?? ""));
|
43
44
|
},
|
@@ -59,12 +60,12 @@ export function instruction() {
|
|
59
60
|
export function chat() {
|
60
61
|
return {
|
61
62
|
format(prompt) {
|
62
|
-
validateChatPrompt(prompt);
|
63
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
|
-
|
67
|
+
const textContent = validateContentIsString(content, prompt);
|
68
|
+
text += segment("user", textContent);
|
68
69
|
break;
|
69
70
|
}
|
70
71
|
case "assistant": {
|
@@ -1,26 +1,2 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.validateChatPrompt = void 0;
|
4
|
-
const InvalidPromptError_js_1 = require("./InvalidPromptError.cjs");
|
5
|
-
/**
|
6
|
-
* Checks if a chat prompt is valid. Throws a {@link ChatPromptValidationError} if it's not.
|
7
|
-
*
|
8
|
-
* @throws {@link ChatPromptValidationError}
|
9
|
-
*/
|
10
|
-
function validateChatPrompt(chatPrompt) {
|
11
|
-
const messages = chatPrompt.messages;
|
12
|
-
if (messages.length < 1) {
|
13
|
-
throw new InvalidPromptError_js_1.InvalidPromptError("ChatPrompt should have at least one message.", chatPrompt);
|
14
|
-
}
|
15
|
-
for (let i = 0; i < messages.length; i++) {
|
16
|
-
const expectedRole = i % 2 === 0 ? "user" : "assistant";
|
17
|
-
const role = messages[i].role;
|
18
|
-
if (role !== expectedRole) {
|
19
|
-
throw new InvalidPromptError_js_1.InvalidPromptError(`Message at index ${i} should have role '${expectedRole}', but has role '${role}'.`, chatPrompt);
|
20
|
-
}
|
21
|
-
}
|
22
|
-
if (messages.length % 2 === 0) {
|
23
|
-
throw new InvalidPromptError_js_1.InvalidPromptError("The last message must be a user message.", chatPrompt);
|
24
|
-
}
|
25
|
-
}
|
26
|
-
exports.validateChatPrompt = validateChatPrompt;
|
@@ -1,15 +1,10 @@
|
|
1
|
-
import {
|
1
|
+
import { Content } from "./Content.js";
|
2
2
|
/**
|
3
|
-
* A
|
3
|
+
* A chat prompt is a combination of a system message and a list of user and assistant messages.
|
4
4
|
*
|
5
|
-
*
|
6
|
-
* - The first message of the chat must be a user message.
|
7
|
-
* - Then it must be alternating between an assistant message and a user message.
|
8
|
-
* - The last message must always be a user message (when submitting to a model).
|
5
|
+
* The user messages can contain multi-modal content.
|
9
6
|
*
|
10
|
-
*
|
11
|
-
*
|
12
|
-
* The type checking is done at runtime when you submit a chat prompt to a model with a prompt template.
|
7
|
+
* Note: Not all models and prompt formats support multi-modal inputs.
|
13
8
|
*
|
14
9
|
* @example
|
15
10
|
* ```ts
|
@@ -22,38 +17,20 @@ import { MultiModalInput } from "./Content.js";
|
|
22
17
|
* ],
|
23
18
|
* };
|
24
19
|
* ```
|
25
|
-
*
|
26
|
-
* @see validateChatPrompt
|
27
20
|
*/
|
28
|
-
export interface
|
21
|
+
export interface ChatPrompt {
|
29
22
|
system?: string;
|
30
|
-
messages: Array<
|
23
|
+
messages: Array<ChatMessage>;
|
31
24
|
}
|
32
25
|
/**
|
33
|
-
* A
|
34
|
-
*
|
26
|
+
* A message in a chat prompt.
|
27
|
+
*
|
28
|
+
* @see ChatPrompt
|
35
29
|
*/
|
36
|
-
export type
|
37
|
-
role: "user";
|
38
|
-
content: string;
|
39
|
-
} | {
|
40
|
-
role: "assistant";
|
41
|
-
content: string;
|
42
|
-
};
|
43
|
-
export interface MultiModalChatPrompt {
|
44
|
-
system?: string;
|
45
|
-
messages: Array<MultiModalChatMessage>;
|
46
|
-
}
|
47
|
-
export type MultiModalChatMessage = {
|
30
|
+
export type ChatMessage = {
|
48
31
|
role: "user";
|
49
|
-
content:
|
32
|
+
content: Content;
|
50
33
|
} | {
|
51
34
|
role: "assistant";
|
52
35
|
content: string;
|
53
36
|
};
|
54
|
-
/**
|
55
|
-
* Checks if a chat prompt is valid. Throws a {@link ChatPromptValidationError} if it's not.
|
56
|
-
*
|
57
|
-
* @throws {@link ChatPromptValidationError}
|
58
|
-
*/
|
59
|
-
export declare function validateChatPrompt(chatPrompt: TextChatPrompt | MultiModalChatPrompt): void;
|
@@ -1,22 +1 @@
|
|
1
|
-
|
2
|
-
/**
|
3
|
-
* Checks if a chat prompt is valid. Throws a {@link ChatPromptValidationError} if it's not.
|
4
|
-
*
|
5
|
-
* @throws {@link ChatPromptValidationError}
|
6
|
-
*/
|
7
|
-
export function validateChatPrompt(chatPrompt) {
|
8
|
-
const messages = chatPrompt.messages;
|
9
|
-
if (messages.length < 1) {
|
10
|
-
throw new InvalidPromptError("ChatPrompt should have at least one message.", chatPrompt);
|
11
|
-
}
|
12
|
-
for (let i = 0; i < messages.length; i++) {
|
13
|
-
const expectedRole = i % 2 === 0 ? "user" : "assistant";
|
14
|
-
const role = messages[i].role;
|
15
|
-
if (role !== expectedRole) {
|
16
|
-
throw new InvalidPromptError(`Message at index ${i} should have role '${expectedRole}', but has role '${role}'.`, chatPrompt);
|
17
|
-
}
|
18
|
-
}
|
19
|
-
if (messages.length % 2 === 0) {
|
20
|
-
throw new InvalidPromptError("The last message must be a user message.", chatPrompt);
|
21
|
-
}
|
22
|
-
}
|
1
|
+
export {};
|
@@ -1,2 +1,11 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.validateContentIsString = void 0;
|
4
|
+
const InvalidPromptError_js_1 = require("./InvalidPromptError.cjs");
|
5
|
+
function validateContentIsString(content, prompt) {
|
6
|
+
if (typeof content !== "string") {
|
7
|
+
throw new InvalidPromptError_js_1.InvalidPromptError("only text prompts are are supported by this prompt template", prompt);
|
8
|
+
}
|
9
|
+
return content;
|
10
|
+
}
|
11
|
+
exports.validateContentIsString = validateContentIsString;
|
@@ -1,13 +1,17 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
/**
|
2
|
+
* Content can either be a simple text content (`string`) or a
|
3
|
+
* complex multi-modal content that is a mix of text parts and
|
4
|
+
* image parts.
|
5
|
+
*/
|
6
|
+
export type Content = string | Array<TextPart | ImagePart>;
|
7
|
+
export interface TextPart {
|
4
8
|
type: "text";
|
5
9
|
/**
|
6
10
|
* The text content.
|
7
11
|
*/
|
8
12
|
text: string;
|
9
13
|
}
|
10
|
-
export interface
|
14
|
+
export interface ImagePart {
|
11
15
|
type: "image";
|
12
16
|
/**
|
13
17
|
* Base-64 encoded image.
|
@@ -18,3 +22,4 @@ export interface ImageContent {
|
|
18
22
|
*/
|
19
23
|
mimeType?: string;
|
20
24
|
}
|
25
|
+
export declare function validateContentIsString(content: Content, prompt: unknown): string;
|
@@ -1 +1,7 @@
|
|
1
|
-
|
1
|
+
import { InvalidPromptError } from "./InvalidPromptError.js";
|
2
|
+
export function validateContentIsString(content, prompt) {
|
3
|
+
if (typeof content !== "string") {
|
4
|
+
throw new InvalidPromptError("only text prompts are are supported by this prompt template", prompt);
|
5
|
+
}
|
6
|
+
return content;
|
7
|
+
}
|
@@ -1,24 +1,10 @@
|
|
1
|
-
import {
|
2
|
-
/**
|
3
|
-
* A single multi-modal instruction prompt. It can contain an optional system message to define
|
4
|
-
* the role and behavior of the language model.
|
5
|
-
* The instruction is a multi-model input (`array` of content).
|
6
|
-
*/
|
7
|
-
export interface MultiModalInstructionPrompt {
|
8
|
-
/**
|
9
|
-
* Optional system message to provide context for the language model. Note that for some models,
|
10
|
-
* changing the system message can impact the results, because the model may be trained on the default system message.
|
11
|
-
*/
|
12
|
-
system?: string;
|
13
|
-
/**
|
14
|
-
* The multi-modal instruction for the model.
|
15
|
-
*/
|
16
|
-
instruction: MultiModalInput;
|
17
|
-
}
|
1
|
+
import { Content } from "./Content.js";
|
18
2
|
/**
|
19
3
|
* A single text instruction prompt. It can contain an optional system message to define
|
20
4
|
* the role and behavior of the language model.
|
21
5
|
*
|
6
|
+
* The instruction can be a text instruction or a multi-modal instruction.
|
7
|
+
*
|
22
8
|
* @example
|
23
9
|
* ```ts
|
24
10
|
* {
|
@@ -27,21 +13,19 @@ export interface MultiModalInstructionPrompt {
|
|
27
13
|
* }
|
28
14
|
* ```
|
29
15
|
*/
|
30
|
-
export interface
|
16
|
+
export interface InstructionPrompt {
|
31
17
|
/**
|
32
18
|
* Optional system message to provide context for the language model. Note that for some models,
|
33
19
|
* changing the system message can impact the results, because the model may be trained on the default system message.
|
34
20
|
*/
|
35
21
|
system?: string;
|
36
22
|
/**
|
37
|
-
* The
|
23
|
+
* The instruction for the model.
|
38
24
|
*/
|
39
|
-
instruction:
|
25
|
+
instruction: Content;
|
40
26
|
/**
|
41
27
|
* Response prefix that will be injected in the prompt at the beginning of the response.
|
42
28
|
* This is useful for guiding the model by starting its response with a specific text.
|
43
|
-
*
|
44
|
-
* Note: Not all models support this feature. E.g. it is not supported by OpenAI chat models.
|
45
29
|
*/
|
46
30
|
responsePrefix?: string;
|
47
31
|
}
|
@@ -1,7 +1,8 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.chat = exports.instruction = exports.text = void 0;
|
4
|
-
const
|
3
|
+
exports.validateLlama2Prompt = exports.chat = exports.instruction = exports.text = void 0;
|
4
|
+
const Content_js_1 = require("./Content.cjs");
|
5
|
+
const InvalidPromptError_js_1 = require("./InvalidPromptError.cjs");
|
5
6
|
// see https://github.com/facebookresearch/llama/blob/6c7fe276574e78057f917549435a2554000a876d/llama/generation.py#L44
|
6
7
|
const BEGIN_SEGMENT = "<s>";
|
7
8
|
const END_SEGMENT = " </s>";
|
@@ -47,9 +48,10 @@ function instruction() {
|
|
47
48
|
return {
|
48
49
|
stopSequences: [END_SEGMENT],
|
49
50
|
format(prompt) {
|
51
|
+
const instruction = (0, Content_js_1.validateContentIsString)(prompt.instruction, prompt);
|
50
52
|
return `${BEGIN_SEGMENT}${BEGIN_INSTRUCTION}${prompt.system != null
|
51
53
|
? `${BEGIN_SYSTEM}${prompt.system}${END_SYSTEM}`
|
52
|
-
: ""}${
|
54
|
+
: ""}${instruction}${END_INSTRUCTION}${prompt.responsePrefix ?? ""}`;
|
53
55
|
},
|
54
56
|
};
|
55
57
|
}
|
@@ -69,7 +71,7 @@ exports.instruction = instruction;
|
|
69
71
|
function chat() {
|
70
72
|
return {
|
71
73
|
format(prompt) {
|
72
|
-
(
|
74
|
+
validateLlama2Prompt(prompt);
|
73
75
|
let text = prompt.system != null
|
74
76
|
? // Separate section for system message to simplify implementation
|
75
77
|
// (this is slightly different from the original instructions):
|
@@ -78,7 +80,8 @@ function chat() {
|
|
78
80
|
for (const { role, content } of prompt.messages) {
|
79
81
|
switch (role) {
|
80
82
|
case "user": {
|
81
|
-
|
83
|
+
const textContent = (0, Content_js_1.validateContentIsString)(content, prompt);
|
84
|
+
text += `${BEGIN_SEGMENT}${BEGIN_INSTRUCTION}${textContent}${END_INSTRUCTION}`;
|
82
85
|
break;
|
83
86
|
}
|
84
87
|
case "assistant": {
|
@@ -97,3 +100,31 @@ function chat() {
|
|
97
100
|
};
|
98
101
|
}
|
99
102
|
exports.chat = chat;
|
103
|
+
/**
|
104
|
+
* Checks if a Llama2 chat prompt is valid. Throws a {@link ChatPromptValidationError} if it's not.
|
105
|
+
*
|
106
|
+
* - The first message of the chat must be a user message.
|
107
|
+
* - Then it must be alternating between an assistant message and a user message.
|
108
|
+
* - The last message must always be a user message (when submitting to a model).
|
109
|
+
*
|
110
|
+
* The type checking is done at runtime when you submit a chat prompt to a model with a prompt template.
|
111
|
+
*
|
112
|
+
* @throws {@link ChatPromptValidationError}
|
113
|
+
*/
|
114
|
+
function validateLlama2Prompt(chatPrompt) {
|
115
|
+
const messages = chatPrompt.messages;
|
116
|
+
if (messages.length < 1) {
|
117
|
+
throw new InvalidPromptError_js_1.InvalidPromptError("ChatPrompt should have at least one message.", chatPrompt);
|
118
|
+
}
|
119
|
+
for (let i = 0; i < messages.length; i++) {
|
120
|
+
const expectedRole = i % 2 === 0 ? "user" : "assistant";
|
121
|
+
const role = messages[i].role;
|
122
|
+
if (role !== expectedRole) {
|
123
|
+
throw new InvalidPromptError_js_1.InvalidPromptError(`Message at index ${i} should have role '${expectedRole}', but has role '${role}'.`, chatPrompt);
|
124
|
+
}
|
125
|
+
}
|
126
|
+
if (messages.length % 2 === 0) {
|
127
|
+
throw new InvalidPromptError_js_1.InvalidPromptError("The last message must be a user message.", chatPrompt);
|
128
|
+
}
|
129
|
+
}
|
130
|
+
exports.validateLlama2Prompt = validateLlama2Prompt;
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import { TextGenerationPromptTemplate } from "../TextGenerationPromptTemplate.js";
|
2
|
-
import {
|
3
|
-
import {
|
2
|
+
import { ChatPrompt } from "./ChatPrompt.js";
|
3
|
+
import { InstructionPrompt } from "./InstructionPrompt.js";
|
4
4
|
/**
|
5
5
|
* Formats a text prompt as a Llama 2 prompt.
|
6
6
|
*
|
@@ -27,7 +27,7 @@ export declare function text(): TextGenerationPromptTemplate<string, string>;
|
|
27
27
|
*
|
28
28
|
* @see https://www.philschmid.de/llama-2#how-to-prompt-llama-2-chat
|
29
29
|
*/
|
30
|
-
export declare function instruction(): TextGenerationPromptTemplate<
|
30
|
+
export declare function instruction(): TextGenerationPromptTemplate<InstructionPrompt, string>;
|
31
31
|
/**
|
32
32
|
* Formats a chat prompt as a Llama 2 prompt.
|
33
33
|
*
|
@@ -40,4 +40,16 @@ export declare function instruction(): TextGenerationPromptTemplate<TextInstruct
|
|
40
40
|
* ${ user msg 1 } [/INST] ${ model response 1 } </s><s>[INST] ${ user msg 2 } [/INST] ${ model response 2 } </s><s>[INST] ${ user msg 3 } [/INST]
|
41
41
|
* ```
|
42
42
|
*/
|
43
|
-
export declare function chat(): TextGenerationPromptTemplate<
|
43
|
+
export declare function chat(): TextGenerationPromptTemplate<ChatPrompt, string>;
|
44
|
+
/**
|
45
|
+
* Checks if a Llama2 chat prompt is valid. Throws a {@link ChatPromptValidationError} if it's not.
|
46
|
+
*
|
47
|
+
* - The first message of the chat must be a user message.
|
48
|
+
* - Then it must be alternating between an assistant message and a user message.
|
49
|
+
* - The last message must always be a user message (when submitting to a model).
|
50
|
+
*
|
51
|
+
* The type checking is done at runtime when you submit a chat prompt to a model with a prompt template.
|
52
|
+
*
|
53
|
+
* @throws {@link ChatPromptValidationError}
|
54
|
+
*/
|
55
|
+
export declare function validateLlama2Prompt(chatPrompt: ChatPrompt): void;
|
@@ -1,4 +1,5 @@
|
|
1
|
-
import {
|
1
|
+
import { validateContentIsString } from "./Content.js";
|
2
|
+
import { InvalidPromptError } from "./InvalidPromptError.js";
|
2
3
|
// see https://github.com/facebookresearch/llama/blob/6c7fe276574e78057f917549435a2554000a876d/llama/generation.py#L44
|
3
4
|
const BEGIN_SEGMENT = "<s>";
|
4
5
|
const END_SEGMENT = " </s>";
|
@@ -43,9 +44,10 @@ export function instruction() {
|
|
43
44
|
return {
|
44
45
|
stopSequences: [END_SEGMENT],
|
45
46
|
format(prompt) {
|
47
|
+
const instruction = validateContentIsString(prompt.instruction, prompt);
|
46
48
|
return `${BEGIN_SEGMENT}${BEGIN_INSTRUCTION}${prompt.system != null
|
47
49
|
? `${BEGIN_SYSTEM}${prompt.system}${END_SYSTEM}`
|
48
|
-
: ""}${
|
50
|
+
: ""}${instruction}${END_INSTRUCTION}${prompt.responsePrefix ?? ""}`;
|
49
51
|
},
|
50
52
|
};
|
51
53
|
}
|
@@ -64,7 +66,7 @@ export function instruction() {
|
|
64
66
|
export function chat() {
|
65
67
|
return {
|
66
68
|
format(prompt) {
|
67
|
-
|
69
|
+
validateLlama2Prompt(prompt);
|
68
70
|
let text = prompt.system != null
|
69
71
|
? // Separate section for system message to simplify implementation
|
70
72
|
// (this is slightly different from the original instructions):
|
@@ -73,7 +75,8 @@ export function chat() {
|
|
73
75
|
for (const { role, content } of prompt.messages) {
|
74
76
|
switch (role) {
|
75
77
|
case "user": {
|
76
|
-
|
78
|
+
const textContent = validateContentIsString(content, prompt);
|
79
|
+
text += `${BEGIN_SEGMENT}${BEGIN_INSTRUCTION}${textContent}${END_INSTRUCTION}`;
|
77
80
|
break;
|
78
81
|
}
|
79
82
|
case "assistant": {
|
@@ -91,3 +94,30 @@ export function chat() {
|
|
91
94
|
stopSequences: [END_SEGMENT],
|
92
95
|
};
|
93
96
|
}
|
97
|
+
/**
|
98
|
+
* Checks if a Llama2 chat prompt is valid. Throws a {@link ChatPromptValidationError} if it's not.
|
99
|
+
*
|
100
|
+
* - The first message of the chat must be a user message.
|
101
|
+
* - Then it must be alternating between an assistant message and a user message.
|
102
|
+
* - The last message must always be a user message (when submitting to a model).
|
103
|
+
*
|
104
|
+
* The type checking is done at runtime when you submit a chat prompt to a model with a prompt template.
|
105
|
+
*
|
106
|
+
* @throws {@link ChatPromptValidationError}
|
107
|
+
*/
|
108
|
+
export function validateLlama2Prompt(chatPrompt) {
|
109
|
+
const messages = chatPrompt.messages;
|
110
|
+
if (messages.length < 1) {
|
111
|
+
throw new InvalidPromptError("ChatPrompt should have at least one message.", chatPrompt);
|
112
|
+
}
|
113
|
+
for (let i = 0; i < messages.length; i++) {
|
114
|
+
const expectedRole = i % 2 === 0 ? "user" : "assistant";
|
115
|
+
const role = messages[i].role;
|
116
|
+
if (role !== expectedRole) {
|
117
|
+
throw new InvalidPromptError(`Message at index ${i} should have role '${expectedRole}', but has role '${role}'.`, chatPrompt);
|
118
|
+
}
|
119
|
+
}
|
120
|
+
if (messages.length % 2 === 0) {
|
121
|
+
throw new InvalidPromptError("The last message must be a user message.", chatPrompt);
|
122
|
+
}
|
123
|
+
}
|
@@ -1,7 +1,7 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.chat = exports.instruction = exports.text = void 0;
|
4
|
-
const
|
4
|
+
const Content_js_1 = require("./Content.cjs");
|
5
5
|
const roleNames = {
|
6
6
|
system: "System",
|
7
7
|
user: "User",
|
@@ -36,8 +36,9 @@ exports.text = text;
|
|
36
36
|
const instruction = () => ({
|
37
37
|
stopSequences: [],
|
38
38
|
format(prompt) {
|
39
|
+
const instruction = (0, Content_js_1.validateContentIsString)(prompt.instruction, prompt);
|
39
40
|
return (segment("system", prompt.system) +
|
40
|
-
segment("user",
|
41
|
+
segment("user", instruction) +
|
41
42
|
segmentStart("assistant") +
|
42
43
|
(prompt.responsePrefix ?? ""));
|
43
44
|
},
|
@@ -53,12 +54,12 @@ exports.instruction = instruction;
|
|
53
54
|
function chat() {
|
54
55
|
return {
|
55
56
|
format(prompt) {
|
56
|
-
(0, ChatPrompt_js_1.validateChatPrompt)(prompt);
|
57
57
|
let text = prompt.system != null ? segment("system", prompt.system) : "";
|
58
58
|
for (const { role, content } of prompt.messages) {
|
59
59
|
switch (role) {
|
60
60
|
case "user": {
|
61
|
-
|
61
|
+
const textContent = (0, Content_js_1.validateContentIsString)(content, prompt);
|
62
|
+
text += segment("user", textContent);
|
62
63
|
break;
|
63
64
|
}
|
64
65
|
case "assistant": {
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import { TextGenerationPromptTemplate } from "../TextGenerationPromptTemplate.js";
|
2
|
-
import {
|
3
|
-
import {
|
2
|
+
import { ChatPrompt } from "./ChatPrompt.js";
|
3
|
+
import { InstructionPrompt } from "./InstructionPrompt.js";
|
4
4
|
/**
|
5
5
|
* Formats a text prompt as a neural chat prompt.
|
6
6
|
*
|
@@ -12,7 +12,7 @@ export declare function text(): TextGenerationPromptTemplate<string, string>;
|
|
12
12
|
*
|
13
13
|
* @see https://huggingface.co/Intel/neural-chat-7b-v3-1#prompt-template
|
14
14
|
*/
|
15
|
-
export declare const instruction: () => TextGenerationPromptTemplate<
|
15
|
+
export declare const instruction: () => TextGenerationPromptTemplate<InstructionPrompt, string>;
|
16
16
|
/**
|
17
17
|
* Formats a chat prompt as a basic text prompt.
|
18
18
|
*
|
@@ -20,4 +20,4 @@ export declare const instruction: () => TextGenerationPromptTemplate<TextInstruc
|
|
20
20
|
* @param assistant The label of the assistant in the chat. Default to "assistant".
|
21
21
|
* @param system The label of the system in the chat. Optional, defaults to no prefix.
|
22
22
|
*/
|
23
|
-
export declare function chat(): TextGenerationPromptTemplate<
|
23
|
+
export declare function chat(): TextGenerationPromptTemplate<ChatPrompt, string>;
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import {
|
1
|
+
import { validateContentIsString } from "./Content.js";
|
2
2
|
const roleNames = {
|
3
3
|
system: "System",
|
4
4
|
user: "User",
|
@@ -32,8 +32,9 @@ export function text() {
|
|
32
32
|
export const instruction = () => ({
|
33
33
|
stopSequences: [],
|
34
34
|
format(prompt) {
|
35
|
+
const instruction = validateContentIsString(prompt.instruction, prompt);
|
35
36
|
return (segment("system", prompt.system) +
|
36
|
-
segment("user",
|
37
|
+
segment("user", instruction) +
|
37
38
|
segmentStart("assistant") +
|
38
39
|
(prompt.responsePrefix ?? ""));
|
39
40
|
},
|
@@ -48,12 +49,12 @@ export const instruction = () => ({
|
|
48
49
|
export function chat() {
|
49
50
|
return {
|
50
51
|
format(prompt) {
|
51
|
-
validateChatPrompt(prompt);
|
52
52
|
let text = prompt.system != null ? segment("system", prompt.system) : "";
|
53
53
|
for (const { role, content } of prompt.messages) {
|
54
54
|
switch (role) {
|
55
55
|
case "user": {
|
56
|
-
|
56
|
+
const textContent = validateContentIsString(content, prompt);
|
57
|
+
text += segment("user", textContent);
|
57
58
|
break;
|
58
59
|
}
|
59
60
|
case "assistant": {
|