@llumiverse/core 0.9.2 → 0.11.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 +51 -7
- package/lib/cjs/CompletionStream.js +1 -7
- package/lib/cjs/CompletionStream.js.map +1 -1
- package/lib/cjs/Driver.js +32 -1
- package/lib/cjs/Driver.js.map +1 -1
- package/lib/cjs/formatters/claude.js +5 -7
- package/lib/cjs/formatters/claude.js.map +1 -1
- package/lib/cjs/formatters/commons.js +8 -0
- package/lib/cjs/formatters/commons.js.map +1 -0
- package/lib/cjs/formatters/generic.js +52 -20
- package/lib/cjs/formatters/generic.js.map +1 -1
- package/lib/cjs/formatters/index.js +18 -27
- package/lib/cjs/formatters/index.js.map +1 -1
- package/lib/cjs/formatters/llama2.js +3 -3
- package/lib/cjs/formatters/llama2.js.map +1 -1
- package/lib/cjs/formatters/openai.js +9 -4
- package/lib/cjs/formatters/openai.js.map +1 -1
- package/lib/cjs/formatters.js +117 -0
- package/lib/cjs/formatters.js.map +1 -0
- package/lib/cjs/index.js +0 -1
- package/lib/cjs/index.js.map +1 -1
- package/lib/cjs/test.js +55 -0
- package/lib/cjs/test.js.map +1 -0
- package/lib/cjs/types.js +1 -8
- package/lib/cjs/types.js.map +1 -1
- package/lib/esm/CompletionStream.js +1 -7
- package/lib/esm/CompletionStream.js.map +1 -1
- package/lib/esm/Driver.js +33 -2
- package/lib/esm/Driver.js.map +1 -1
- package/lib/esm/formatters/claude.js +3 -5
- package/lib/esm/formatters/claude.js.map +1 -1
- package/lib/esm/formatters/commons.js +4 -0
- package/lib/esm/formatters/commons.js.map +1 -0
- package/lib/esm/formatters/generic.js +50 -18
- package/lib/esm/formatters/generic.js.map +1 -1
- package/lib/esm/formatters/index.js +5 -26
- package/lib/esm/formatters/index.js.map +1 -1
- package/lib/esm/formatters/llama2.js +1 -1
- package/lib/esm/formatters/llama2.js.map +1 -1
- package/lib/esm/formatters/openai.js +7 -2
- package/lib/esm/formatters/openai.js.map +1 -1
- package/lib/esm/formatters.js +113 -0
- package/lib/esm/formatters.js.map +1 -0
- package/lib/esm/index.js +0 -1
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/test.js +50 -0
- package/lib/esm/test.js.map +1 -0
- package/lib/esm/types.js +0 -7
- package/lib/esm/types.js.map +1 -1
- package/lib/tsconfig.tsbuildinfo +1 -0
- package/lib/types/CompletionStream.d.ts +1 -2
- package/lib/types/CompletionStream.d.ts.map +1 -1
- package/lib/types/Driver.d.ts +16 -11
- package/lib/types/Driver.d.ts.map +1 -1
- package/lib/types/formatters/claude.d.ts +1 -1
- package/lib/types/formatters/claude.d.ts.map +1 -1
- package/lib/types/formatters/commons.d.ts +3 -0
- package/lib/types/formatters/commons.d.ts.map +1 -0
- package/lib/types/formatters/generic.d.ts +9 -4
- package/lib/types/formatters/generic.d.ts.map +1 -1
- package/lib/types/formatters/index.d.ts +7 -3
- package/lib/types/formatters/index.d.ts.map +1 -1
- package/lib/types/formatters/llama2.d.ts +1 -1
- package/lib/types/formatters/llama2.d.ts.map +1 -1
- package/lib/types/formatters/openai.d.ts +10 -2
- package/lib/types/formatters/openai.d.ts.map +1 -1
- package/lib/types/formatters.d.ts +5 -0
- package/lib/types/formatters.d.ts.map +1 -0
- package/lib/types/index.d.ts +0 -1
- package/lib/types/index.d.ts.map +1 -1
- package/lib/types/json.d.ts +8 -8
- package/lib/types/json.d.ts.map +1 -1
- package/lib/types/test.d.ts +2 -0
- package/lib/types/test.d.ts.map +1 -0
- package/lib/types/types.d.ts +43 -13
- package/lib/types/types.d.ts.map +1 -1
- package/package.json +15 -5
- package/src/CompletionStream.ts +1 -8
- package/src/Driver.ts +38 -12
- package/src/formatters/claude.ts +3 -6
- package/src/formatters/commons.ts +5 -0
- package/src/formatters/generic.ts +59 -27
- package/src/formatters/index.ts +7 -30
- package/src/formatters/llama2.ts +1 -1
- package/src/formatters/openai.ts +14 -6
- package/src/index.ts +0 -1
- package/src/json.ts +7 -7
- package/src/types.ts +46 -13
package/lib/types/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACvC,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAExD,MAAM,WAAW,iBAAiB;IAC9B;;OAEG;IACH,OAAO,EAAE,MAAM,CAAC;IAChB;;OAEG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;IACf;;;OAGG;IACH,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;CACtB;AAED,MAAM,WAAW,gBAAgB;IAC7B;;OAEG;IACH,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB;;OAEG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;IACf;;OAEG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;OAEG;IACH,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;CACtB;AAED,MAAM,WAAW,qBAAqB;IAClC,IAAI,EAAE,kBAAkB,GAAG,YAAY,CAAC;IACxC,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,UAAU,CAAC,OAAO,GAAG,GAAG;IAErC,MAAM,EAAE,OAAO,CAAC;IAChB,WAAW,CAAC,EAAE,mBAAmB,CAAC;IAElC;;;OAGG;IACH,KAAK,CAAC,EAAE,qBAAqB,CAAC;CACjC;AAED,MAAM,WAAW,iBAAiB,CAAC,OAAO,GAAG,GAAG,CAAE,SAAQ,UAAU;IAChE,MAAM,EAAE,OAAO,CAAC;IAChB;;OAEG;IACH,cAAc,CAAC,EAAE,MAAM,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AAS,IAAI,cAAc,CAAC,UAAU,GAAG,MAAM,CAAC,CAAC;IACjD,MAAM,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;CAC7B;AAED,MAAM,WAAW,eAAe;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,UAAU,CAAC;CACvB;AAED,MAAM,WAAW,qBAAqB;IAClC,QAAQ,EAAE,aAAa,EAAE,CAAC;IAC1B,UAAU,EAAE,MAAM,GAAG,UAAU,CAAC;IAChC,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,WAAW,CAAC;CACxB;AAED,oBAAY,iBAAiB;IACzB,OAAO,YAAY;IACnB,SAAS,cAAc;IACvB,MAAM,WAAW;IACjB,SAAS,cAAc;CAC1B;AAED,MAAM,WAAW,WAAW;IACxB,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,iBAAiB,CAAC;IAC1B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,KAAK,CAAC,EAAE,MAAM,CAAC;CAClB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@llumiverse/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Provide an universal API to LLMs. Support for existing LLMs can be added by writing a driver.",
|
|
6
6
|
"files": [
|
|
@@ -32,6 +32,9 @@
|
|
|
32
32
|
"*": {
|
|
33
33
|
"async": [
|
|
34
34
|
"./lib/types/async.d.ts"
|
|
35
|
+
],
|
|
36
|
+
"formatters": [
|
|
37
|
+
"./lib/types/formatters/index.d.ts"
|
|
35
38
|
]
|
|
36
39
|
}
|
|
37
40
|
},
|
|
@@ -47,6 +50,12 @@
|
|
|
47
50
|
"default": "./lib/esm/async.js",
|
|
48
51
|
"import": "./lib/esm/async.js",
|
|
49
52
|
"require": "./lib/cjs/async.js"
|
|
53
|
+
},
|
|
54
|
+
"./formatters": {
|
|
55
|
+
"types": "./lib/types/formatters/index.d.ts",
|
|
56
|
+
"default": "./lib/esm/formatters/index.js",
|
|
57
|
+
"import": "./lib/esm/formatters/index.js",
|
|
58
|
+
"require": "./lib/cjs/formatters/index.js"
|
|
50
59
|
}
|
|
51
60
|
},
|
|
52
61
|
"scripts": {
|
|
@@ -66,8 +75,8 @@
|
|
|
66
75
|
"@types/json-schema": "^7.0.15",
|
|
67
76
|
"api-fetch-client": "^0.8.6",
|
|
68
77
|
"ts-dual-module": "^0.6.2",
|
|
69
|
-
"typescript": "^5.
|
|
70
|
-
"vitest": "^1.
|
|
78
|
+
"typescript": "^5.4.2",
|
|
79
|
+
"vitest": "^1.4.0"
|
|
71
80
|
},
|
|
72
81
|
"dependencies": {
|
|
73
82
|
"json-schema": "^0.4.0"
|
|
@@ -75,7 +84,8 @@
|
|
|
75
84
|
"ts_dual_module": {
|
|
76
85
|
"outDir": "lib",
|
|
77
86
|
"exports": {
|
|
78
|
-
"async": "async.js"
|
|
87
|
+
"async": "async.js",
|
|
88
|
+
"formatters": "formatters/index.js"
|
|
79
89
|
}
|
|
80
90
|
}
|
|
81
|
-
}
|
|
91
|
+
}
|
package/src/CompletionStream.ts
CHANGED
|
@@ -60,7 +60,6 @@ export class DefaultCompletionStream<PromptT = any> implements CompletionStream<
|
|
|
60
60
|
export class FallbackCompletionStream<PromptT = any> implements CompletionStream<PromptT> {
|
|
61
61
|
|
|
62
62
|
prompt: PromptT;
|
|
63
|
-
chunks: string[];
|
|
64
63
|
completion: ExecutionResponse<PromptT> | undefined;
|
|
65
64
|
|
|
66
65
|
constructor(public driver: AbstractDriver<DriverOptions, PromptT>,
|
|
@@ -68,22 +67,16 @@ export class FallbackCompletionStream<PromptT = any> implements CompletionStream
|
|
|
68
67
|
public options: ExecutionOptions) {
|
|
69
68
|
this.driver = driver;
|
|
70
69
|
this.prompt = this.driver.createPrompt(segments, options);
|
|
71
|
-
this.chunks = [];
|
|
72
70
|
}
|
|
73
71
|
|
|
74
72
|
async *[Symbol.asyncIterator]() {
|
|
75
73
|
// reset state
|
|
76
74
|
this.completion = undefined;
|
|
77
|
-
if (this.chunks.length > 0) {
|
|
78
|
-
this.chunks = [];
|
|
79
|
-
}
|
|
80
75
|
this.driver.logger.debug(
|
|
81
76
|
`[${this.driver.provider}] Streaming is not supported, falling back to blocking execution`
|
|
82
77
|
);
|
|
83
78
|
const completion = await this.driver._execute(this.prompt, this.options);
|
|
84
|
-
|
|
85
|
-
const content = completion.result === 'string' ? completion.result : JSON.stringify(completion.result);
|
|
86
|
-
this.chunks.push(content);
|
|
79
|
+
const content = typeof completion.result === 'string' ? completion.result : JSON.stringify(completion.result);
|
|
87
80
|
yield content;
|
|
88
81
|
|
|
89
82
|
this.completion = completion;
|
package/src/Driver.ts
CHANGED
|
@@ -5,18 +5,19 @@
|
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
import { DefaultCompletionStream, FallbackCompletionStream } from "./CompletionStream.js";
|
|
8
|
-
import {
|
|
8
|
+
import { formatLlama2Prompt, formatTextPrompt } from "./formatters/index.js";
|
|
9
9
|
import {
|
|
10
10
|
AIModel,
|
|
11
11
|
Completion,
|
|
12
12
|
CompletionStream,
|
|
13
13
|
DataSource,
|
|
14
14
|
DriverOptions,
|
|
15
|
+
EmbeddingsOptions,
|
|
16
|
+
EmbeddingsResult,
|
|
15
17
|
ExecutionOptions,
|
|
16
18
|
ExecutionResponse,
|
|
17
19
|
Logger,
|
|
18
20
|
ModelSearchPayload,
|
|
19
|
-
PromptFormats,
|
|
20
21
|
PromptOptions,
|
|
21
22
|
PromptSegment,
|
|
22
23
|
TrainingJob,
|
|
@@ -25,7 +26,6 @@ import {
|
|
|
25
26
|
} from "./types.js";
|
|
26
27
|
import { validateResult } from "./validation.js";
|
|
27
28
|
|
|
28
|
-
|
|
29
29
|
const ConsoleLogger: Logger = {
|
|
30
30
|
debug: console.debug,
|
|
31
31
|
info: console.info,
|
|
@@ -51,6 +51,13 @@ export function createLogger(logger: Logger | "console" | undefined) {
|
|
|
51
51
|
}
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
+
function applyExecutionDefaults(options: ExecutionOptions): ExecutionOptions {
|
|
55
|
+
return {
|
|
56
|
+
max_tokens: 4096,
|
|
57
|
+
temperature: 0.7,
|
|
58
|
+
...options
|
|
59
|
+
}
|
|
60
|
+
}
|
|
54
61
|
|
|
55
62
|
export interface Driver<PromptT = unknown> {
|
|
56
63
|
|
|
@@ -86,7 +93,7 @@ export interface Driver<PromptT = unknown> {
|
|
|
86
93
|
validateConnection(): Promise<boolean>;
|
|
87
94
|
|
|
88
95
|
//generate embeddings for a given text
|
|
89
|
-
generateEmbeddings(
|
|
96
|
+
generateEmbeddings(options: EmbeddingsOptions): Promise<EmbeddingsResult>;
|
|
90
97
|
|
|
91
98
|
}
|
|
92
99
|
|
|
@@ -98,7 +105,6 @@ export abstract class AbstractDriver<OptionsT extends DriverOptions = DriverOpti
|
|
|
98
105
|
logger: Logger;
|
|
99
106
|
|
|
100
107
|
abstract provider: string; // the provider name
|
|
101
|
-
abstract defaultFormat: PromptFormats;
|
|
102
108
|
|
|
103
109
|
constructor(opts: OptionsT) {
|
|
104
110
|
this.options = opts;
|
|
@@ -141,6 +147,7 @@ export abstract class AbstractDriver<OptionsT extends DriverOptions = DriverOpti
|
|
|
141
147
|
}
|
|
142
148
|
|
|
143
149
|
async execute(segments: PromptSegment[], options: ExecutionOptions): Promise<ExecutionResponse<PromptT>> {
|
|
150
|
+
options = applyExecutionDefaults(options);
|
|
144
151
|
const prompt = this.createPrompt(segments, options);
|
|
145
152
|
return this._execute(prompt, options);
|
|
146
153
|
}
|
|
@@ -163,6 +170,7 @@ export abstract class AbstractDriver<OptionsT extends DriverOptions = DriverOpti
|
|
|
163
170
|
|
|
164
171
|
// by default no stream is supported. we block and we return all at once
|
|
165
172
|
async stream(segments: PromptSegment[], options: ExecutionOptions): Promise<CompletionStream<PromptT>> {
|
|
173
|
+
options = applyExecutionDefaults(options);
|
|
166
174
|
const canStream = await this.canStream(options);
|
|
167
175
|
if (canStream) {
|
|
168
176
|
return new DefaultCompletionStream(this, segments, options);
|
|
@@ -171,11 +179,22 @@ export abstract class AbstractDriver<OptionsT extends DriverOptions = DriverOpti
|
|
|
171
179
|
}
|
|
172
180
|
}
|
|
173
181
|
|
|
182
|
+
/**
|
|
183
|
+
* Override this method to provide a custom prompt formatter
|
|
184
|
+
* @param segments
|
|
185
|
+
* @param options
|
|
186
|
+
* @returns
|
|
187
|
+
*/
|
|
188
|
+
protected formatPrompt(segments: PromptSegment[], opts: PromptOptions): PromptT {
|
|
189
|
+
if (/\bllama2?\b/i.test(opts.model)) {
|
|
190
|
+
return formatLlama2Prompt(segments, opts.resultSchema) as PromptT;
|
|
191
|
+
} else {
|
|
192
|
+
return formatTextPrompt(segments, opts.resultSchema) as PromptT;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
174
196
|
public createPrompt(segments: PromptSegment[], opts: PromptOptions): PromptT {
|
|
175
|
-
return
|
|
176
|
-
segments,
|
|
177
|
-
opts.resultSchema
|
|
178
|
-
);
|
|
197
|
+
return opts.format ? opts.format(segments, opts.resultSchema) : this.formatPrompt(segments, opts);
|
|
179
198
|
}
|
|
180
199
|
|
|
181
200
|
/**
|
|
@@ -191,6 +210,15 @@ export abstract class AbstractDriver<OptionsT extends DriverOptions = DriverOpti
|
|
|
191
210
|
return Promise.resolve(true);
|
|
192
211
|
}
|
|
193
212
|
|
|
213
|
+
/**
|
|
214
|
+
* Get a list of models that can be trained.
|
|
215
|
+
* The default is to return an empty array
|
|
216
|
+
* @returns
|
|
217
|
+
*/
|
|
218
|
+
async listTrainableModels(): Promise<AIModel[]> {
|
|
219
|
+
return [];
|
|
220
|
+
}
|
|
221
|
+
|
|
194
222
|
abstract requestCompletion(prompt: PromptT, options: ExecutionOptions): Promise<Completion>;
|
|
195
223
|
|
|
196
224
|
abstract requestCompletionStream(prompt: PromptT, options: ExecutionOptions): Promise<AsyncIterable<string>>;
|
|
@@ -198,13 +226,11 @@ export abstract class AbstractDriver<OptionsT extends DriverOptions = DriverOpti
|
|
|
198
226
|
//list models available for this environement
|
|
199
227
|
abstract listModels(params?: ModelSearchPayload): Promise<AIModel[]>;
|
|
200
228
|
|
|
201
|
-
abstract listTrainableModels(): Promise<AIModel[]>;
|
|
202
|
-
|
|
203
229
|
//check that it is possible to connect to the environment
|
|
204
230
|
abstract validateConnection(): Promise<boolean>;
|
|
205
231
|
|
|
206
232
|
//generate embeddings for a given text
|
|
207
|
-
abstract generateEmbeddings(
|
|
233
|
+
abstract generateEmbeddings(options: EmbeddingsOptions): Promise<EmbeddingsResult>;
|
|
208
234
|
|
|
209
235
|
}
|
|
210
236
|
|
package/src/formatters/claude.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { JSONSchema4 } from "json-schema";
|
|
2
2
|
import { PromptRole, PromptSegment } from "../index.js";
|
|
3
|
+
import { getJSONSafetyNotice } from "./commons.js";
|
|
3
4
|
|
|
4
5
|
export interface ClaudeMessage {
|
|
5
6
|
role: 'user' | 'assistant',
|
|
@@ -19,7 +20,7 @@ export interface ClaudeMessagesPrompt {
|
|
|
19
20
|
* A formatter user by Bedrock to format prompts for claude related models
|
|
20
21
|
*/
|
|
21
22
|
|
|
22
|
-
export function
|
|
23
|
+
export function formatClaudePrompt(segments: PromptSegment[], schema?: JSONSchema4): ClaudeMessagesPrompt {
|
|
23
24
|
const system: string[] = [];
|
|
24
25
|
const safety: string[] = [];
|
|
25
26
|
const messages: ClaudeMessage[] = [];
|
|
@@ -35,13 +36,9 @@ export function claudeMessages(segments: PromptSegment[], schema?: JSONSchema4):
|
|
|
35
36
|
}
|
|
36
37
|
|
|
37
38
|
if (schema) {
|
|
38
|
-
safety.push(
|
|
39
|
-
---
|
|
40
|
-
${JSON.stringify(schema)}
|
|
41
|
-
---`);
|
|
39
|
+
safety.push(getJSONSafetyNotice(schema));
|
|
42
40
|
}
|
|
43
41
|
|
|
44
|
-
|
|
45
42
|
// messages must contains at least 1 item. If the prompt doesn;t contains a user message (but only system messages)
|
|
46
43
|
// we need to put the system messages in the messages array
|
|
47
44
|
|
|
@@ -1,34 +1,66 @@
|
|
|
1
1
|
import { JSONSchema4 } from "json-schema";
|
|
2
|
-
import { PromptRole, PromptSegment } from "../
|
|
2
|
+
import { PromptRole, PromptSegment } from "../types.js";
|
|
3
|
+
import { getJSONSafetyNotice } from "./commons.js";
|
|
3
4
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
5
|
+
interface Labels {
|
|
6
|
+
user: string,
|
|
7
|
+
system: string,
|
|
8
|
+
assistant: string,
|
|
9
|
+
safety: string,
|
|
10
|
+
instruction: string
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function createTextPromptFormatter(labels: Labels = {
|
|
14
|
+
user: "USER",
|
|
15
|
+
system: "CONTEXT",
|
|
16
|
+
assistant: "ASSISTANT",
|
|
17
|
+
safety: "IMPORTANT",
|
|
18
|
+
instruction: "INSTRUCTION"
|
|
19
|
+
}) {
|
|
20
|
+
return function genericTextPrompt(segments: PromptSegment[], schema?: JSONSchema4): string {
|
|
21
|
+
const isChat = segments.find(m => m.role === PromptRole.assistant);
|
|
22
|
+
const context: string[] = [];
|
|
23
|
+
const content: string[] = [];
|
|
24
|
+
const safety: string[] = [];
|
|
25
|
+
for (const segment of segments) {
|
|
26
|
+
switch (segment.role) {
|
|
27
|
+
case PromptRole.user:
|
|
28
|
+
if (isChat) {
|
|
29
|
+
content.push(`${labels.user}: ${segment.content}`);
|
|
30
|
+
} else {
|
|
31
|
+
content.push(segment.content);
|
|
32
|
+
}
|
|
33
|
+
break;
|
|
34
|
+
case PromptRole.assistant:
|
|
35
|
+
content.push(`${labels.assistant}: ${segment.content}`);
|
|
36
|
+
break;
|
|
37
|
+
case PromptRole.system:
|
|
38
|
+
context.push(segment.content);
|
|
39
|
+
break;
|
|
40
|
+
case PromptRole.safety:
|
|
41
|
+
safety.push(segment.content);
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
20
44
|
}
|
|
21
|
-
|
|
22
|
-
|
|
45
|
+
|
|
46
|
+
if (schema) {
|
|
47
|
+
safety.push(getJSONSafetyNotice(schema));
|
|
23
48
|
}
|
|
24
|
-
}
|
|
25
49
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
50
|
+
const out = [];
|
|
51
|
+
if (context.length > 0) {
|
|
52
|
+
out.push(`${labels.system}: ${context.join('\n')}`);
|
|
53
|
+
}
|
|
54
|
+
if (content.length > 0) {
|
|
55
|
+
const prefix = context.length > 0 && !isChat ? `${labels.instruction}: ` : '';
|
|
56
|
+
out.push(prefix + content.join('\n'));
|
|
57
|
+
}
|
|
58
|
+
if (safety.length > 0) {
|
|
59
|
+
out.push(`${labels.safety}: ${safety.join('\n')}`);
|
|
60
|
+
}
|
|
61
|
+
return out.join('\n');
|
|
31
62
|
}
|
|
32
|
-
|
|
33
|
-
return promptMessages.join("\n\n");
|
|
34
63
|
}
|
|
64
|
+
|
|
65
|
+
const formatTextPrompt = createTextPromptFormatter();
|
|
66
|
+
export { formatTextPrompt };
|
package/src/formatters/index.ts
CHANGED
|
@@ -1,33 +1,10 @@
|
|
|
1
1
|
import { JSONSchema4 } from "json-schema";
|
|
2
|
-
import {
|
|
3
|
-
import { llama2 } from "./llama2.js";
|
|
4
|
-
import { openAI } from "./openai.js";
|
|
5
|
-
import {
|
|
6
|
-
PromptFormats,
|
|
7
|
-
PromptSegment
|
|
8
|
-
} from "../types.js";
|
|
9
|
-
import { claudeMessages } from "./claude.js";
|
|
2
|
+
import { PromptSegment } from "../types.js";
|
|
10
3
|
|
|
11
|
-
export
|
|
12
|
-
const name = modelName.toLowerCase();
|
|
13
|
-
if (name.includes("llama")) {
|
|
14
|
-
return PromptFormats.llama2;
|
|
15
|
-
} else if (name.includes("gpt")) {
|
|
16
|
-
return PromptFormats.openai;
|
|
17
|
-
} else if (name.includes("claude")) {
|
|
18
|
-
return PromptFormats.claude;
|
|
19
|
-
} else {
|
|
20
|
-
return PromptFormats.genericTextLLM;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export const PromptFormatters: Record<
|
|
25
|
-
PromptFormats,
|
|
26
|
-
(messages: PromptSegment[], schema?: JSONSchema4) => any
|
|
27
|
-
> = {
|
|
28
|
-
openai: openAI,
|
|
29
|
-
llama2: llama2,
|
|
30
|
-
claude: claudeMessages,
|
|
31
|
-
genericTextLLM: genericColonSeparator,
|
|
32
|
-
};
|
|
4
|
+
export type PromptFormatter<T = any> = (messages: PromptSegment[], schema?: JSONSchema4) => T;
|
|
33
5
|
|
|
6
|
+
export * from "./commons.js"
|
|
7
|
+
export * from "./generic.js";
|
|
8
|
+
export * from "./llama2.js";
|
|
9
|
+
export * from "./claude.js";
|
|
10
|
+
export * from "./openai.js";
|
package/src/formatters/llama2.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { JSONSchema4 } from "json-schema";
|
|
2
2
|
import { PromptRole, PromptSegment } from "../index.js";
|
|
3
3
|
|
|
4
|
-
export function
|
|
4
|
+
export function formatLlama2Prompt(messages: PromptSegment[], schema?: JSONSchema4) {
|
|
5
5
|
const BOS = "<s>";
|
|
6
6
|
const EOS = "</s>";
|
|
7
7
|
const INST = "[INST]";
|
package/src/formatters/openai.ts
CHANGED
|
@@ -1,17 +1,25 @@
|
|
|
1
1
|
import { PromptRole } from "../index.js";
|
|
2
2
|
import { PromptSegment } from "../types.js";
|
|
3
|
-
import OpenAI from "openai";
|
|
4
3
|
|
|
5
|
-
export
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
export interface OpenAITextMessage {
|
|
5
|
+
content: string;
|
|
6
|
+
role: "system" | "user" | "assistant";
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* OpenAI text only prompts
|
|
10
|
+
* @param segments
|
|
11
|
+
* @returns
|
|
12
|
+
*/
|
|
13
|
+
export function formatOpenAILikePrompt(segments: PromptSegment[]) {
|
|
14
|
+
const system: OpenAITextMessage[] = [];
|
|
15
|
+
const others: OpenAITextMessage[] = [];
|
|
16
|
+
const safety: OpenAITextMessage[] = [];
|
|
9
17
|
|
|
10
18
|
for (const msg of segments) {
|
|
11
19
|
if (msg.role === PromptRole.system) {
|
|
12
20
|
system.push({ content: msg.content, role: "system" });
|
|
13
21
|
} else if (msg.role === PromptRole.safety) {
|
|
14
|
-
safety.push({ content: msg.content, role: "system" });
|
|
22
|
+
safety.push({ content: "IMPORTANT: " + msg.content, role: "system" });
|
|
15
23
|
} else {
|
|
16
24
|
others.push({ content: msg.content, role: "user" });
|
|
17
25
|
}
|
package/src/index.ts
CHANGED
package/src/json.ts
CHANGED
|
@@ -6,15 +6,15 @@ function extractJsonFromText(text: string): string {
|
|
|
6
6
|
return text.replace(/\\n/g, "");
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
-
export function extractAndParseJSON(text: string):
|
|
9
|
+
export function extractAndParseJSON(text: string): JSONValue {
|
|
10
10
|
return parseJSON(extractJsonFromText(text));
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
export type
|
|
14
|
-
export type
|
|
15
|
-
export type
|
|
16
|
-
export type
|
|
17
|
-
export type
|
|
13
|
+
export type JSONPrimitive = string | number | boolean | null;
|
|
14
|
+
export type JSONArray = JSONValue[];
|
|
15
|
+
export type JSONObject = { [key: string]: JSONValue };
|
|
16
|
+
export type JSONComposite = JSONArray | JSONObject;
|
|
17
|
+
export type JSONValue = JSONPrimitive | JSONComposite;
|
|
18
18
|
|
|
19
19
|
|
|
20
20
|
|
|
@@ -184,7 +184,7 @@ export class JsonParser {
|
|
|
184
184
|
}
|
|
185
185
|
|
|
186
186
|
|
|
187
|
-
export function parseJSON(text: string):
|
|
187
|
+
export function parseJSON(text: string): JSONValue {
|
|
188
188
|
text = text.trim();
|
|
189
189
|
try {
|
|
190
190
|
return JSON.parse(text);
|
package/src/types.ts
CHANGED
|
@@ -1,6 +1,41 @@
|
|
|
1
1
|
import { JSONSchema4 } from "json-schema";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { JSONObject } from "./json.js";
|
|
3
|
+
import { PromptFormatter } from "./formatters/index.js";
|
|
4
|
+
|
|
5
|
+
export interface EmbeddingsOptions {
|
|
6
|
+
/**
|
|
7
|
+
* The content to generate the embeddings for. Required.
|
|
8
|
+
*/
|
|
9
|
+
content: string;
|
|
10
|
+
/**
|
|
11
|
+
* The model to use to generate the embeddings. Optional.
|
|
12
|
+
*/
|
|
13
|
+
model?: string;
|
|
14
|
+
/**
|
|
15
|
+
* Additional options for the embeddings generation. Optional.
|
|
16
|
+
* The supported properties depends on the target implementation.
|
|
17
|
+
*/
|
|
18
|
+
[key: string]: any;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface EmbeddingsResult {
|
|
22
|
+
/**
|
|
23
|
+
* The embedding vectors corresponding to the words in the input text.
|
|
24
|
+
*/
|
|
25
|
+
values: number[];
|
|
26
|
+
/**
|
|
27
|
+
* The model used to hgenerate the embeddings.
|
|
28
|
+
*/
|
|
29
|
+
model?: string;
|
|
30
|
+
/**
|
|
31
|
+
* Number of tokens of the input text.
|
|
32
|
+
*/
|
|
33
|
+
token_count?: number;
|
|
34
|
+
/**
|
|
35
|
+
* Additional properties. Depends on the target implementation.
|
|
36
|
+
*/
|
|
37
|
+
[key: string]: any;
|
|
38
|
+
}
|
|
4
39
|
|
|
5
40
|
export interface ResultValidationError {
|
|
6
41
|
code: 'validation_error' | 'json_error';
|
|
@@ -46,7 +81,11 @@ export interface DriverOptions {
|
|
|
46
81
|
|
|
47
82
|
export interface PromptOptions {
|
|
48
83
|
model: string;
|
|
49
|
-
|
|
84
|
+
/**
|
|
85
|
+
* A custom formatter to use for format the final model prompt from the input prompt segments.
|
|
86
|
+
* If no one is specified the driver will choose a formatter compatible with the target model
|
|
87
|
+
*/
|
|
88
|
+
format?: PromptFormatter;
|
|
50
89
|
resultSchema?: JSONSchema4;
|
|
51
90
|
}
|
|
52
91
|
export interface ExecutionOptions extends PromptOptions {
|
|
@@ -136,13 +175,6 @@ export enum ModelType {
|
|
|
136
175
|
// ============== Built-in formats and drivers =====================
|
|
137
176
|
//TODO
|
|
138
177
|
|
|
139
|
-
export enum PromptFormats {
|
|
140
|
-
openai = "openai",
|
|
141
|
-
llama2 = "llama2",
|
|
142
|
-
claude = "claude",
|
|
143
|
-
genericTextLLM = "genericTextLLM",
|
|
144
|
-
}
|
|
145
|
-
|
|
146
178
|
export enum BuiltinProviders {
|
|
147
179
|
openai = 'openai',
|
|
148
180
|
huggingface_ie = 'huggingface_ie',
|
|
@@ -158,21 +190,22 @@ export enum BuiltinProviders {
|
|
|
158
190
|
// ============== training =====================
|
|
159
191
|
|
|
160
192
|
|
|
193
|
+
|
|
161
194
|
export interface DataSource {
|
|
162
195
|
name: string;
|
|
163
|
-
getStream():
|
|
196
|
+
getStream(): ReadableStream<Uint8Array | string>;
|
|
164
197
|
getURL(): Promise<string>;
|
|
165
198
|
}
|
|
166
199
|
|
|
167
200
|
export interface TrainingOptions {
|
|
168
201
|
name: string; // the new model name
|
|
169
202
|
model: string; // the model to train
|
|
170
|
-
params?:
|
|
203
|
+
params?: JSONObject; // the training parameters
|
|
171
204
|
}
|
|
172
205
|
|
|
173
206
|
export interface TrainingPromptOptions {
|
|
174
207
|
segments: PromptSegment[];
|
|
175
|
-
completion: string |
|
|
208
|
+
completion: string | JSONObject;
|
|
176
209
|
model: string; // the model to train
|
|
177
210
|
schema?: JSONSchema4; // the resuilt schema f any
|
|
178
211
|
}
|