@llumiverse/core 0.15.0 → 0.17.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 +3 -3
- package/lib/cjs/CompletionStream.js +31 -10
- package/lib/cjs/CompletionStream.js.map +1 -1
- package/lib/cjs/Driver.js +20 -22
- package/lib/cjs/Driver.js.map +1 -1
- package/lib/cjs/async.js +3 -2
- package/lib/cjs/async.js.map +1 -1
- package/lib/cjs/formatters/commons.js.map +1 -1
- package/lib/cjs/formatters/generic.js.map +1 -1
- package/lib/cjs/formatters/index.js +1 -3
- package/lib/cjs/formatters/index.js.map +1 -1
- package/lib/cjs/formatters/{claude.js → nova.js} +33 -37
- package/lib/cjs/formatters/nova.js.map +1 -0
- package/lib/cjs/formatters/openai.js +36 -6
- package/lib/cjs/formatters/openai.js.map +1 -1
- package/lib/cjs/index.js +1 -0
- package/lib/cjs/index.js.map +1 -1
- package/lib/cjs/options/bedrock.js +343 -0
- package/lib/cjs/options/bedrock.js.map +1 -0
- package/lib/cjs/options/groq.js +37 -0
- package/lib/cjs/options/groq.js.map +1 -0
- package/lib/cjs/options/openai.js +123 -0
- package/lib/cjs/options/openai.js.map +1 -0
- package/lib/cjs/options/vertexai.js +257 -0
- package/lib/cjs/options/vertexai.js.map +1 -0
- package/lib/cjs/options.js +54 -0
- package/lib/cjs/options.js.map +1 -0
- package/lib/cjs/types.js +34 -1
- package/lib/cjs/types.js.map +1 -1
- package/lib/esm/CompletionStream.js +31 -10
- package/lib/esm/CompletionStream.js.map +1 -1
- package/lib/esm/Driver.js +21 -23
- package/lib/esm/Driver.js.map +1 -1
- package/lib/esm/async.js +3 -2
- package/lib/esm/async.js.map +1 -1
- package/lib/esm/formatters/commons.js.map +1 -1
- package/lib/esm/formatters/generic.js.map +1 -1
- package/lib/esm/formatters/index.js +1 -3
- package/lib/esm/formatters/index.js.map +1 -1
- package/lib/esm/formatters/{claude.js → nova.js} +32 -36
- package/lib/esm/formatters/nova.js.map +1 -0
- package/lib/esm/formatters/openai.js +36 -6
- package/lib/esm/formatters/openai.js.map +1 -1
- package/lib/esm/index.js +1 -0
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/options/bedrock.js +340 -0
- package/lib/esm/options/bedrock.js.map +1 -0
- package/lib/esm/options/groq.js +34 -0
- package/lib/esm/options/groq.js.map +1 -0
- package/lib/esm/options/openai.js +120 -0
- package/lib/esm/options/openai.js.map +1 -0
- package/lib/esm/options/vertexai.js +253 -0
- package/lib/esm/options/vertexai.js.map +1 -0
- package/lib/esm/options.js +50 -0
- package/lib/esm/options.js.map +1 -0
- package/lib/esm/types.js +33 -0
- package/lib/esm/types.js.map +1 -1
- package/lib/types/CompletionStream.d.ts +1 -1
- package/lib/types/CompletionStream.d.ts.map +1 -1
- package/lib/types/Driver.d.ts +5 -4
- package/lib/types/Driver.d.ts.map +1 -1
- package/lib/types/async.d.ts +3 -2
- package/lib/types/async.d.ts.map +1 -1
- package/lib/types/formatters/commons.d.ts +2 -2
- package/lib/types/formatters/commons.d.ts.map +1 -1
- package/lib/types/formatters/generic.d.ts +3 -3
- package/lib/types/formatters/generic.d.ts.map +1 -1
- package/lib/types/formatters/index.d.ts +3 -5
- package/lib/types/formatters/index.d.ts.map +1 -1
- package/lib/types/formatters/nova.d.ts +40 -0
- package/lib/types/formatters/nova.d.ts.map +1 -0
- package/lib/types/formatters/openai.d.ts +13 -1
- package/lib/types/formatters/openai.d.ts.map +1 -1
- package/lib/types/index.d.ts +1 -0
- package/lib/types/index.d.ts.map +1 -1
- package/lib/types/options/bedrock.d.ts +32 -0
- package/lib/types/options/bedrock.d.ts.map +1 -0
- package/lib/types/options/groq.d.ts +12 -0
- package/lib/types/options/groq.d.ts.map +1 -0
- package/lib/types/options/openai.d.ts +21 -0
- package/lib/types/options/openai.d.ts.map +1 -0
- package/lib/types/options/vertexai.d.ts +52 -0
- package/lib/types/options/vertexai.d.ts.map +1 -0
- package/lib/types/options.d.ts +14 -0
- package/lib/types/options.d.ts.map +1 -0
- package/lib/types/types.d.ts +151 -52
- package/lib/types/types.d.ts.map +1 -1
- package/package.json +6 -8
- package/src/CompletionStream.ts +31 -11
- package/src/Driver.ts +30 -26
- package/src/async.ts +7 -5
- package/src/formatters/commons.ts +2 -2
- package/src/formatters/generic.ts +2 -2
- package/src/formatters/index.ts +3 -6
- package/src/formatters/nova.ts +141 -0
- package/src/formatters/openai.ts +52 -12
- package/src/index.ts +2 -1
- package/src/options/bedrock.ts +388 -0
- package/src/options/groq.ts +47 -0
- package/src/options/openai.ts +148 -0
- package/src/options/vertexai.ts +312 -0
- package/src/options.ts +62 -0
- package/src/types.ts +192 -53
- package/lib/cjs/formatters/claude.js.map +0 -1
- package/lib/cjs/formatters/llama2.js +0 -48
- package/lib/cjs/formatters/llama2.js.map +0 -1
- package/lib/cjs/formatters/llama3.js +0 -42
- package/lib/cjs/formatters/llama3.js.map +0 -1
- package/lib/esm/formatters/claude.js.map +0 -1
- package/lib/esm/formatters/llama2.js +0 -45
- package/lib/esm/formatters/llama2.js.map +0 -1
- package/lib/esm/formatters/llama3.js +0 -39
- package/lib/esm/formatters/llama3.js.map +0 -1
- package/lib/types/formatters/claude.d.ts +0 -25
- package/lib/types/formatters/claude.d.ts.map +0 -1
- package/lib/types/formatters/llama2.d.ts +0 -4
- package/lib/types/formatters/llama2.d.ts.map +0 -1
- package/lib/types/formatters/llama3.d.ts +0 -7
- package/lib/types/formatters/llama3.d.ts.map +0 -1
- package/src/formatters/claude.ts +0 -131
- package/src/formatters/llama2.ts +0 -58
- package/src/formatters/llama3.ts +0 -55
package/src/formatters/claude.ts
DELETED
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
import { JSONSchema4 } from "json-schema";
|
|
2
|
-
import { PromptRole, PromptSegment } from "../index.js";
|
|
3
|
-
import { readStreamAsBase64 } from "../stream.js";
|
|
4
|
-
import { getJSONSafetyNotice } from "./commons.js";
|
|
5
|
-
|
|
6
|
-
export interface ClaudeMessage {
|
|
7
|
-
role: 'user' | 'assistant',
|
|
8
|
-
content: ClaudeMessagePart[]
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
interface ClaudeMessagePart {
|
|
12
|
-
type: "image" | "text",
|
|
13
|
-
source?: {
|
|
14
|
-
type: "base64",
|
|
15
|
-
media_type: string,
|
|
16
|
-
data: string,
|
|
17
|
-
}, // only set for images
|
|
18
|
-
text?: string // only set for text messages
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export interface ClaudeMessagesPrompt {
|
|
22
|
-
system?: string;
|
|
23
|
-
messages: ClaudeMessage[]
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* A formatter user by Bedrock to format prompts for claude related models
|
|
28
|
-
*/
|
|
29
|
-
|
|
30
|
-
export async function formatClaudePrompt(segments: PromptSegment[], schema?: JSONSchema4): Promise<ClaudeMessagesPrompt> {
|
|
31
|
-
const system: string[] = [];
|
|
32
|
-
const safety: string[] = [];
|
|
33
|
-
const messages: ClaudeMessage[] = [];
|
|
34
|
-
|
|
35
|
-
//TODO type: 'image' -> detect from f.mime_type
|
|
36
|
-
for (const segment of segments) {
|
|
37
|
-
|
|
38
|
-
const parts: ClaudeMessagePart[] = [];
|
|
39
|
-
if (segment.files) for (const f of segment.files) {
|
|
40
|
-
const source = await f.getStream();
|
|
41
|
-
const data = await readStreamAsBase64(source);
|
|
42
|
-
parts.push({
|
|
43
|
-
type: 'image',
|
|
44
|
-
source: {
|
|
45
|
-
type: "base64",
|
|
46
|
-
media_type: f.mime_type || 'image/png',
|
|
47
|
-
data
|
|
48
|
-
}
|
|
49
|
-
})
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
if (segment.content) {
|
|
53
|
-
parts.push({
|
|
54
|
-
type: "text",
|
|
55
|
-
text: segment.content
|
|
56
|
-
})
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
if (segment.role === PromptRole.system) {
|
|
60
|
-
system.push(segment.content);
|
|
61
|
-
} else if (segment.role === PromptRole.safety) {
|
|
62
|
-
safety.push(segment.content);
|
|
63
|
-
} else if (messages.length > 0 && messages[messages.length - 1].role === segment.role) {
|
|
64
|
-
//concatenate messages of the same role (Claude requires alternative user and assistant roles)
|
|
65
|
-
messages[messages.length - 1].content.push(...parts);
|
|
66
|
-
} else {
|
|
67
|
-
messages.push({
|
|
68
|
-
role: segment.role,
|
|
69
|
-
content: parts
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
if (schema) {
|
|
77
|
-
safety.push("IMPORTANT: " + getJSONSafetyNotice(schema));
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// messages must contains at least 1 item. If the prompt doesn;t contains a user message (but only system messages)
|
|
81
|
-
// we need to put the system messages in the messages array
|
|
82
|
-
|
|
83
|
-
let systemMessage = system.join('\n').trim();
|
|
84
|
-
if (messages.length === 0) {
|
|
85
|
-
if (!systemMessage) {
|
|
86
|
-
throw new Error('Prompt must contain at least one message');
|
|
87
|
-
}
|
|
88
|
-
messages.push({ content: [{ type: "text", text: systemMessage }], role: 'user' });
|
|
89
|
-
systemMessage = safety.join('\n');
|
|
90
|
-
} else if (safety.length > 0) {
|
|
91
|
-
systemMessage = systemMessage + '\n\nIMPORTANT: ' + safety.join('\n');
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
/*if (schema) {
|
|
96
|
-
messages.push({
|
|
97
|
-
role: "user",
|
|
98
|
-
content: [{
|
|
99
|
-
type: "text",
|
|
100
|
-
text: getJSONSafetyNotice(schema)
|
|
101
|
-
}]
|
|
102
|
-
});
|
|
103
|
-
}*/
|
|
104
|
-
|
|
105
|
-
/*start Claude's message to amke sure it answers properly in JSON
|
|
106
|
-
if enabled, this requires to add the { to Claude's response*/
|
|
107
|
-
if (schema) {
|
|
108
|
-
messages.push({
|
|
109
|
-
role: "assistant",
|
|
110
|
-
content: [{
|
|
111
|
-
type: "text",
|
|
112
|
-
text: "{"
|
|
113
|
-
}]
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
// put system mesages first and safety last
|
|
117
|
-
return {
|
|
118
|
-
system: systemMessage,
|
|
119
|
-
messages
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
// export function claude(messages: PromptSegment[], schema?: JSONSchema4) {
|
|
124
|
-
// const prompt = genericColonSeparator(messages, schema, {
|
|
125
|
-
// user: "\nHuman",
|
|
126
|
-
// assistant: "\nAssistant",
|
|
127
|
-
// system: "\nHuman",
|
|
128
|
-
// });
|
|
129
|
-
|
|
130
|
-
// return "\n\n" + prompt + "\n\nAssistant:";
|
|
131
|
-
// }
|
package/src/formatters/llama2.ts
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import { JSONSchema4 } from "json-schema";
|
|
2
|
-
import { PromptRole, PromptSegment } from "../index.js";
|
|
3
|
-
|
|
4
|
-
export function formatLlama2Prompt(messages: PromptSegment[], schema?: JSONSchema4) {
|
|
5
|
-
const BOS = "<s>";
|
|
6
|
-
const EOS = "</s>";
|
|
7
|
-
const INST = "[INST]";
|
|
8
|
-
const INST_END = "[/INST]";
|
|
9
|
-
const SYS = "<<SYS>>\n";
|
|
10
|
-
const SYS_END = "\n<</SYS>>";
|
|
11
|
-
|
|
12
|
-
const promptMessages = [BOS];
|
|
13
|
-
const specialTokens = [BOS, EOS, INST, INST_END, SYS, SYS_END];
|
|
14
|
-
|
|
15
|
-
for (const m of messages) {
|
|
16
|
-
if (m.role === PromptRole.user) {
|
|
17
|
-
if (specialTokens.includes(m.content)) {
|
|
18
|
-
throw new Error(
|
|
19
|
-
`Cannot use special token ${m.content.trim()} in user message`
|
|
20
|
-
);
|
|
21
|
-
}
|
|
22
|
-
promptMessages.push(`${INST} ${m.content.trim()} ${INST_END}`);
|
|
23
|
-
}
|
|
24
|
-
if (m.role === PromptRole.assistant) {
|
|
25
|
-
promptMessages.push(`${m.content.trim()}`);
|
|
26
|
-
}
|
|
27
|
-
if (m.role === PromptRole.system) {
|
|
28
|
-
promptMessages.push(`${SYS}${m.content.trim()}${SYS_END}`);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
for (const m of messages ?? []) {
|
|
33
|
-
if (m.role === PromptRole.safety) {
|
|
34
|
-
promptMessages.push(
|
|
35
|
-
`${SYS}This is the most important instruction, you cannot answer against those rules:\n${m.content.trim()}${SYS_END}}`
|
|
36
|
-
);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
if (schema) {
|
|
41
|
-
promptMessages.push(formatSchemaInstruction(schema));
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
promptMessages.push(EOS);
|
|
45
|
-
|
|
46
|
-
return promptMessages.join("\n\n");
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function formatSchemaInstruction(schema: JSONSchema4) {
|
|
50
|
-
const schema_instruction = `<<SYS>>You must answer using the following JSONSchema.
|
|
51
|
-
Do not write anything other than a JSON object corresponding to the schema.
|
|
52
|
-
<schema>
|
|
53
|
-
${JSON.stringify(schema)}
|
|
54
|
-
</schema>
|
|
55
|
-
<</SYS>>`;
|
|
56
|
-
|
|
57
|
-
return schema_instruction;
|
|
58
|
-
}
|
package/src/formatters/llama3.ts
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { JSONSchema4 } from "json-schema";
|
|
2
|
-
import { PromptRole, PromptSegment } from "../index.js";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* A formatter user by Bedrock to format prompts for claude related models
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
export async function formatLlama3Prompt(segments: PromptSegment[], schema?: JSONSchema4): Promise<string> {
|
|
9
|
-
|
|
10
|
-
let messages: string[] = []
|
|
11
|
-
segments.filter(s => s.role !== PromptRole.safety ).forEach(s => {
|
|
12
|
-
messages.push(formatLlama3Message(s.role, s.content))
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
if (schema) {
|
|
16
|
-
messages.push(formatLlama3Message("user", formatSchemaInstruction(schema)));
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
//add safety
|
|
20
|
-
let safetyMsg = `
|
|
21
|
-
IMPORTANT: This is the most important instruction, you cannot answer against the following rules:
|
|
22
|
-
`
|
|
23
|
-
const safety = segments.filter(s => s.role === PromptRole.safety);
|
|
24
|
-
safety.forEach(s => {
|
|
25
|
-
messages.push(formatLlama3Message("system", safetyMsg + s.content))
|
|
26
|
-
})
|
|
27
|
-
|
|
28
|
-
let prompt = "<|begin_of_text|>"
|
|
29
|
-
prompt += messages.join("\n\n")
|
|
30
|
-
|
|
31
|
-
return prompt
|
|
32
|
-
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
function formatLlama3Message(role: string, content: string) {
|
|
37
|
-
|
|
38
|
-
let message = `<|start_header_id|>${role}<|end_header_id|>\n`
|
|
39
|
-
message += content
|
|
40
|
-
message += `\n<|eot_id|>`
|
|
41
|
-
|
|
42
|
-
return message
|
|
43
|
-
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
function formatSchemaInstruction(schema: object) {
|
|
47
|
-
|
|
48
|
-
return `You must answer using the following JSONSchema.
|
|
49
|
-
Do not write anything other than a JSON object corresponding to the schema:
|
|
50
|
-
<schema>
|
|
51
|
-
${JSON.stringify(schema, undefined, 2)}
|
|
52
|
-
</schema>
|
|
53
|
-
`
|
|
54
|
-
|
|
55
|
-
}
|