@llumiverse/core 0.8.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/lib/cjs/CompletionStream.js +78 -0
- package/lib/cjs/CompletionStream.js.map +1 -0
- package/lib/cjs/Driver.js +115 -0
- package/lib/cjs/Driver.js.map +1 -0
- package/lib/cjs/async.js +107 -0
- package/lib/cjs/async.js.map +1 -0
- package/lib/cjs/formatters.js +117 -0
- package/lib/cjs/formatters.js.map +1 -0
- package/lib/cjs/index.js +21 -0
- package/lib/cjs/index.js.map +1 -0
- package/lib/cjs/json.js +14 -0
- package/lib/cjs/json.js.map +1 -0
- package/lib/cjs/package.json +3 -0
- package/lib/cjs/types.js +67 -0
- package/lib/cjs/types.js.map +1 -0
- package/lib/cjs/validation.js +35 -0
- package/lib/cjs/validation.js.map +1 -0
- package/lib/esm/CompletionStream.js +73 -0
- package/lib/esm/CompletionStream.js.map +1 -0
- package/lib/esm/Driver.js +110 -0
- package/lib/esm/Driver.js.map +1 -0
- package/lib/esm/async.js +100 -0
- package/lib/esm/async.js.map +1 -0
- package/lib/esm/formatters.js +113 -0
- package/lib/esm/formatters.js.map +1 -0
- package/lib/esm/index.js +5 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/esm/json.js +10 -0
- package/lib/esm/json.js.map +1 -0
- package/lib/esm/types.js +64 -0
- package/lib/esm/types.js.map +1 -0
- package/lib/esm/validation.js +30 -0
- package/lib/esm/validation.js.map +1 -0
- package/lib/types/CompletionStream.d.ts +21 -0
- package/lib/types/CompletionStream.d.ts.map +1 -0
- package/lib/types/Driver.d.ts +68 -0
- package/lib/types/Driver.d.ts.map +1 -0
- package/lib/types/async.d.ts +21 -0
- package/lib/types/async.d.ts.map +1 -0
- package/lib/types/formatters.d.ts +5 -0
- package/lib/types/formatters.d.ts.map +1 -0
- package/lib/types/index.d.ts +5 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/json.d.ts +9 -0
- package/lib/types/json.d.ts.map +1 -0
- package/lib/types/types.d.ts +153 -0
- package/lib/types/types.d.ts.map +1 -0
- package/lib/types/validation.d.ts +8 -0
- package/lib/types/validation.d.ts.map +1 -0
- package/package.json +74 -0
- package/src/CompletionStream.ts +92 -0
- package/src/Driver.ts +204 -0
- package/src/async.ts +120 -0
- package/src/formatters.ts +147 -0
- package/src/index.ts +4 -0
- package/src/json.ts +17 -0
- package/src/types.ts +187 -0
- package/src/validation.ts +34 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Driver.d.ts","sourceRoot":"","sources":["../../src/Driver.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,OAAO,EACH,OAAO,EACP,UAAU,EACV,gBAAgB,EAChB,UAAU,EACV,aAAa,EACb,gBAAgB,EAChB,iBAAiB,EACjB,MAAM,EACN,kBAAkB,EAClB,aAAa,EACb,aAAa,EACb,aAAa,EACb,WAAW,EACX,eAAe,EACf,qBAAqB,EACxB,MAAM,YAAY,CAAC;AAmBpB,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,KAAK,GAAG,SAAS,UAE9D;AAGD,MAAM,WAAW,MAAM,CAAC,OAAO,GAAG,OAAO;IAErC;;;;;OAKG;IACH,oBAAoB,CAAC,OAAO,EAAE,qBAAqB,GAAG,MAAM,CAAC;IAE7D,YAAY,CAAC,QAAQ,EAAE,aAAa,EAAE,EAAE,IAAI,EAAE,aAAa,GAAG,OAAO,CAAC;IAEtE,OAAO,CAAC,QAAQ,EAAE,aAAa,EAAE,EAAE,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,iBAAiB,CAAC,OAAO,CAAC,CAAC,CAAC;IAInG,MAAM,CAAC,QAAQ,EAAE,aAAa,EAAE,EAAE,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,gBAAgB,CAAC,OAAO,CAAC,CAAC,CAAC;IAEjG,aAAa,CAAC,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,eAAe,GAAG,OA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awaited<R>, void, unknown>;
|
|
2
|
+
export declare function oneAsyncIterator<T>(value: T): AsyncIterable<T>;
|
|
3
|
+
export declare class EventStream<T, ReturnT = any> implements AsyncIterable<T> {
|
|
4
|
+
private queue;
|
|
5
|
+
private pending?;
|
|
6
|
+
private done;
|
|
7
|
+
push(event: T): void;
|
|
8
|
+
/**
|
|
9
|
+
* Close the stream. This means the stream cannot be feeded anymore.
|
|
10
|
+
* But the consumer can still consume the remaining events.
|
|
11
|
+
*/
|
|
12
|
+
close(value?: ReturnT): void;
|
|
13
|
+
[Symbol.asyncIterator](): AsyncIterator<T, ReturnT | undefined>;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Transform an async iterator by applying a function to each value.
|
|
17
|
+
* @param originalGenerator
|
|
18
|
+
* @param transform
|
|
19
|
+
**/
|
|
20
|
+
export declare function transformAsyncIterator<T, V>(originalGenerator: AsyncIterable<T>, transform: (value: T) => V | Promise<V>): AsyncIterable<V>;
|
|
21
|
+
//# sourceMappingURL=async.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"async.d.ts","sourceRoot":"","sources":["../../src/async.ts"],"names":[],"mappings":"AACA,wBAAuB,QAAQ,CAAC,CAAC,EAAE,CAAC,EAAE,aAAa,EAAE,aAAa,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,KAAK,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,KAAK,CAAC,6CAI9G;AAED,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,aAAa,CAAC,CAAC,CAAC,CAM9D;AAGD,qBAAa,WAAW,CAAC,CAAC,EAAE,OAAO,GAAG,GAAG,CAAE,YAAW,aAAa,CAAC,CAAC,CAAC;IAElE,OAAO,CAAC,KAAK,CAAW;IACxB,OAAO,CAAC,OAAO,CAAC,CAGd;IACF,OAAO,CAAC,IAAI,CAAS;IAGrB,IAAI,CAAC,KAAK,EAAE,CAAC;IAYb;;;OAGG;IACH,KAAK,CAAC,KAAK,CAAC,EAAE,OAAO;IAQrB,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,aAAa,CAAC,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;CA0BlE;AAID;;;;IAII;AACJ,wBAAuB,sBAAsB,CAAC,CAAC,EAAE,CAAC,EAC9C,iBAAiB,EAAE,aAAa,CAAC,CAAC,CAAC,EACnC,SAAS,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,GACxC,aAAa,CAAC,CAAC,CAAC,CAIlB"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { JSONSchema4 } from "json-schema";
|
|
2
|
+
import { PromptFormats, PromptSegment } from "./types.js";
|
|
3
|
+
export declare function inferFormatterFromModelName(modelName: string): PromptFormats;
|
|
4
|
+
export declare const PromptFormatters: Record<PromptFormats, (messages: PromptSegment[], schema?: JSONSchema4) => any>;
|
|
5
|
+
//# sourceMappingURL=formatters.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formatters.d.ts","sourceRoot":"","sources":["../../src/formatters.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE1C,OAAO,EACH,aAAa,EAEb,aAAa,EAChB,MAAM,YAAY,CAAC;AAEpB,wBAAgB,2BAA2B,CAAC,SAAS,EAAE,MAAM,GAAG,aAAa,CAW5E;AAED,eAAO,MAAM,gBAAgB,EAAE,MAAM,CACjC,aAAa,EACb,CAAC,QAAQ,EAAE,aAAa,EAAE,EAAE,MAAM,CAAC,EAAE,WAAW,KAAK,GAAG,CAM3D,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,iBAAiB,CAAC;AAChC,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare function parseJSON(text: string): Json;
|
|
2
|
+
export type JsonPrimative = string | number | boolean | null;
|
|
3
|
+
export type JsonArray = Json[];
|
|
4
|
+
export type JsonObject = {
|
|
5
|
+
[key: string]: Json;
|
|
6
|
+
};
|
|
7
|
+
export type JsonComposite = JsonArray | JsonObject;
|
|
8
|
+
export type Json = JsonPrimative | JsonComposite;
|
|
9
|
+
//# sourceMappingURL=json.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../../src/json.ts"],"names":[],"mappings":"AAQA,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAE5C;AAED,MAAM,MAAM,aAAa,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,CAAC;AAC7D,MAAM,MAAM,SAAS,GAAG,IAAI,EAAE,CAAC;AAC/B,MAAM,MAAM,UAAU,GAAG;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAAA;CAAE,CAAC;AACjD,MAAM,MAAM,aAAa,GAAG,SAAS,GAAG,UAAU,CAAC;AACnD,MAAM,MAAM,IAAI,GAAG,aAAa,GAAG,aAAa,CAAC"}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
import { JSONSchema4 } from "json-schema";
|
|
3
|
+
import { Readable } from "stream";
|
|
4
|
+
import { JsonObject } from "./json.js";
|
|
5
|
+
export interface ResultValidationError {
|
|
6
|
+
code: 'validation_error' | 'json_error';
|
|
7
|
+
message: string;
|
|
8
|
+
data?: string;
|
|
9
|
+
}
|
|
10
|
+
export interface Completion<ResultT = any> {
|
|
11
|
+
result: ResultT;
|
|
12
|
+
token_usage?: ExecutionTokenUsage;
|
|
13
|
+
execution_time?: number;
|
|
14
|
+
/**
|
|
15
|
+
* Set only if a result validation error occured, otherwise if the result is valid the error field is undefined
|
|
16
|
+
* This can only be set if the resultSchema is set and the reuslt could not be parsed as a json or if the result does not match the schema
|
|
17
|
+
*/
|
|
18
|
+
error?: ResultValidationError;
|
|
19
|
+
}
|
|
20
|
+
export interface ExecutionResponse<PromptT = any> extends Completion {
|
|
21
|
+
prompt: PromptT;
|
|
22
|
+
}
|
|
23
|
+
export interface CompletionStream<PromptT = any> extends AsyncIterable<string> {
|
|
24
|
+
completion: ExecutionResponse<PromptT> | undefined;
|
|
25
|
+
}
|
|
26
|
+
export interface Logger {
|
|
27
|
+
debug: (...obj: any[]) => void;
|
|
28
|
+
info: (...obj: any[]) => void;
|
|
29
|
+
warn: (...obj: any[]) => void;
|
|
30
|
+
error: (...obj: any[]) => void;
|
|
31
|
+
}
|
|
32
|
+
export interface DriverOptions {
|
|
33
|
+
logger?: Logger | false;
|
|
34
|
+
}
|
|
35
|
+
export interface PromptOptions {
|
|
36
|
+
model: string;
|
|
37
|
+
format?: PromptFormats;
|
|
38
|
+
resultSchema?: JSONSchema4;
|
|
39
|
+
}
|
|
40
|
+
export interface ExecutionOptions extends PromptOptions {
|
|
41
|
+
temperature?: number;
|
|
42
|
+
max_tokens?: number;
|
|
43
|
+
}
|
|
44
|
+
export declare enum PromptRole {
|
|
45
|
+
safety = "safety",
|
|
46
|
+
system = "system",
|
|
47
|
+
user = "user",
|
|
48
|
+
assistant = "assistant"
|
|
49
|
+
}
|
|
50
|
+
export interface PromptSegment {
|
|
51
|
+
role: PromptRole;
|
|
52
|
+
content: string;
|
|
53
|
+
}
|
|
54
|
+
export interface ExecutionTokenUsage {
|
|
55
|
+
prompt?: number;
|
|
56
|
+
result?: number;
|
|
57
|
+
total?: number;
|
|
58
|
+
}
|
|
59
|
+
export interface AIModel<ProviderKeys = string> {
|
|
60
|
+
id: string;
|
|
61
|
+
name: string;
|
|
62
|
+
provider: ProviderKeys;
|
|
63
|
+
description?: string;
|
|
64
|
+
version?: string;
|
|
65
|
+
type?: ModelType;
|
|
66
|
+
tags?: string[];
|
|
67
|
+
owner?: string;
|
|
68
|
+
status?: AIModelStatus;
|
|
69
|
+
canStream?: boolean;
|
|
70
|
+
isCustom?: boolean;
|
|
71
|
+
}
|
|
72
|
+
export declare enum AIModelStatus {
|
|
73
|
+
Available = "available",
|
|
74
|
+
Pending = "pending",
|
|
75
|
+
Stopped = "stopped",
|
|
76
|
+
Unavailable = "unavailable",
|
|
77
|
+
Unknown = "unknown"
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* payload to list available models for an enviroment
|
|
81
|
+
* @param environmentId id of the environment
|
|
82
|
+
* @param query text to search for in model name/description
|
|
83
|
+
* @param type type of the model
|
|
84
|
+
* @param tags tags for searching
|
|
85
|
+
*/
|
|
86
|
+
export interface ModelSearchPayload {
|
|
87
|
+
text: string;
|
|
88
|
+
type?: ModelType;
|
|
89
|
+
tags?: string[];
|
|
90
|
+
owner?: string;
|
|
91
|
+
}
|
|
92
|
+
export declare enum ModelType {
|
|
93
|
+
Classifier = "classifier",
|
|
94
|
+
Regressor = "regressor",
|
|
95
|
+
Clustering = "clustering",
|
|
96
|
+
AnomalyDetection = "anomaly-detection",
|
|
97
|
+
TimeSeries = "time-series",
|
|
98
|
+
Text = "text",
|
|
99
|
+
Image = "image",
|
|
100
|
+
Audio = "audio",
|
|
101
|
+
Video = "video",
|
|
102
|
+
Embedding = "embedding",
|
|
103
|
+
Chat = "chat",
|
|
104
|
+
Code = "code",
|
|
105
|
+
NLP = "nlp",
|
|
106
|
+
MultiModal = "multi-modal",
|
|
107
|
+
Test = "test",
|
|
108
|
+
Other = "other",
|
|
109
|
+
Unknown = "unknown"
|
|
110
|
+
}
|
|
111
|
+
export declare enum PromptFormats {
|
|
112
|
+
openai = "openai",
|
|
113
|
+
llama2 = "llama2",
|
|
114
|
+
claude = "claude",
|
|
115
|
+
genericTextLLM = "genericTextLLM"
|
|
116
|
+
}
|
|
117
|
+
export declare enum BuiltinProviders {
|
|
118
|
+
openai = "openai",
|
|
119
|
+
huggingface_ie = "huggingface_ie",
|
|
120
|
+
replicate = "replicate",
|
|
121
|
+
bedrock = "bedrock",
|
|
122
|
+
vertexai = "vertexai",
|
|
123
|
+
togetherai = "togetherai"
|
|
124
|
+
}
|
|
125
|
+
export interface DataSource {
|
|
126
|
+
name: string;
|
|
127
|
+
getStream(): Readable;
|
|
128
|
+
getURL(): Promise<string>;
|
|
129
|
+
}
|
|
130
|
+
export interface TrainingOptions {
|
|
131
|
+
name: string;
|
|
132
|
+
model: string;
|
|
133
|
+
params?: JsonObject;
|
|
134
|
+
}
|
|
135
|
+
export interface TrainingPromptOptions {
|
|
136
|
+
segments: PromptSegment[];
|
|
137
|
+
completion: string | JsonObject;
|
|
138
|
+
model: string;
|
|
139
|
+
schema?: JSONSchema4;
|
|
140
|
+
}
|
|
141
|
+
export declare enum TrainingJobStatus {
|
|
142
|
+
running = "running",
|
|
143
|
+
succeeded = "succeeded",
|
|
144
|
+
failed = "failed",
|
|
145
|
+
cancelled = "cancelled"
|
|
146
|
+
}
|
|
147
|
+
export interface TrainingJob {
|
|
148
|
+
id: string;
|
|
149
|
+
status: TrainingJobStatus;
|
|
150
|
+
details?: string;
|
|
151
|
+
model?: string;
|
|
152
|
+
}
|
|
153
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
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,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAClC,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAEvC,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,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SAAS;IACb,KAAK,UAAU;IACf,OAAO,YAAY;CACtB;AAKD,oBAAY,aAAa;IACrB,MAAM,WAAW;IACjB,MAAM,WAAW;IACjB,MAAM,WAAW;IACjB,cAAc,mBAAmB;CACpC;AAED,oBAAY,gBAAgB;IACxB,MAAM,WAAW;IACjB,cAAc,mBAAmB;IACjC,SAAS,cAAc;IACvB,OAAO,YAAY;IACnB,QAAQ,aAAa;IACrB,UAAU,eAAe;CAG5B;AAKD,MAAM,WAAW,UAAU;IACvB,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,IAAI,QAAQ,CAAC;IACtB,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"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { JSONSchema4 } from "json-schema";
|
|
2
|
+
import { ResultValidationError } from "./types.js";
|
|
3
|
+
export declare class ValidationError extends Error implements ResultValidationError {
|
|
4
|
+
code: 'validation_error' | 'json_error';
|
|
5
|
+
constructor(code: 'validation_error' | 'json_error', message: string);
|
|
6
|
+
}
|
|
7
|
+
export declare function validateResult(data: any, schema: JSONSchema4): any;
|
|
8
|
+
//# sourceMappingURL=validation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../../src/validation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAY,MAAM,aAAa,CAAC;AAEpD,OAAO,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AAEnD,qBAAa,eAAgB,SAAQ,KAAM,YAAW,qBAAqB;IAE5D,IAAI,EAAE,kBAAkB,GAAG,YAAY;gBAAvC,IAAI,EAAE,kBAAkB,GAAG,YAAY,EAC9C,OAAO,EAAE,MAAM;CAKtB;AAED,wBAAgB,cAAc,CAAC,IAAI,EAAE,GAAG,EAAE,MAAM,EAAE,WAAW,OAmB5D"}
|
package/package.json
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@llumiverse/core",
|
|
3
|
+
"version": "0.8.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Provide an universal API to LLMs. Support for existing LLMs can be added by writing a driver.",
|
|
6
|
+
"files": [
|
|
7
|
+
"lib",
|
|
8
|
+
"src"
|
|
9
|
+
],
|
|
10
|
+
"keywords": [
|
|
11
|
+
"llm",
|
|
12
|
+
"ai",
|
|
13
|
+
"prompt",
|
|
14
|
+
"prompt engineering",
|
|
15
|
+
"ml",
|
|
16
|
+
"machine learning",
|
|
17
|
+
"embeddings",
|
|
18
|
+
"training",
|
|
19
|
+
"model",
|
|
20
|
+
"universal",
|
|
21
|
+
"api",
|
|
22
|
+
"chatgpt"
|
|
23
|
+
],
|
|
24
|
+
"types": "./lib/types/index.d.ts",
|
|
25
|
+
"typesVersions": {
|
|
26
|
+
"*": {
|
|
27
|
+
"async": [
|
|
28
|
+
"./lib/types/async.d.ts"
|
|
29
|
+
]
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"exports": {
|
|
33
|
+
".": {
|
|
34
|
+
"types": "./lib/types/index.d.ts",
|
|
35
|
+
"default": "./lib/esm/index.js",
|
|
36
|
+
"import": "./lib/esm/index.js",
|
|
37
|
+
"require": "./lib/cjs/index.js"
|
|
38
|
+
},
|
|
39
|
+
"./async": {
|
|
40
|
+
"types": "./lib/types/async.d.ts",
|
|
41
|
+
"default": "./lib/esm/async.js",
|
|
42
|
+
"import": "./lib/esm/async.js",
|
|
43
|
+
"require": "./lib/cjs/async.js"
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
"scripts": {
|
|
47
|
+
"test": "vitest run",
|
|
48
|
+
"build": "tsmod build",
|
|
49
|
+
"clean": "rm -rf ./lib tsconfig.tsbuildinfo"
|
|
50
|
+
},
|
|
51
|
+
"author": "Llumiverse",
|
|
52
|
+
"license": "Apache-2.0",
|
|
53
|
+
"homepage": "https://github.com/llumiverse/llumiverse",
|
|
54
|
+
"repository": {
|
|
55
|
+
"type": "git",
|
|
56
|
+
"url": "git+ssh://git@github.com/llumiverse/llumiverse.git"
|
|
57
|
+
},
|
|
58
|
+
"devDependencies": {
|
|
59
|
+
"@types/eventsource": "^1.1.15",
|
|
60
|
+
"@types/json-schema": "^7.0.15",
|
|
61
|
+
"ts-dual-module": "^0.5.0",
|
|
62
|
+
"typescript": "^5.3.3",
|
|
63
|
+
"vitest": "^1.2.2"
|
|
64
|
+
},
|
|
65
|
+
"dependencies": {
|
|
66
|
+
"json-schema": "^0.4.0"
|
|
67
|
+
},
|
|
68
|
+
"ts_dual_module": {
|
|
69
|
+
"outDir": "lib",
|
|
70
|
+
"exports": {
|
|
71
|
+
"async": "async.js"
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { AbstractDriver } from "./Driver.js";
|
|
2
|
+
import { ExecutionResponse, CompletionStream, DriverOptions, ExecutionOptions, PromptSegment } from "./types.js";
|
|
3
|
+
|
|
4
|
+
export class DefaultCompletionStream<PromptT = any> implements CompletionStream<PromptT> {
|
|
5
|
+
|
|
6
|
+
chunks: string[];
|
|
7
|
+
prompt: PromptT;
|
|
8
|
+
completion: ExecutionResponse<PromptT> | undefined;
|
|
9
|
+
|
|
10
|
+
constructor(public driver: AbstractDriver<DriverOptions, PromptT>,
|
|
11
|
+
segments: PromptSegment[],
|
|
12
|
+
public options: ExecutionOptions) {
|
|
13
|
+
this.driver = driver;
|
|
14
|
+
this.prompt = this.driver.createPrompt(segments, options);
|
|
15
|
+
this.chunks = [];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async *[Symbol.asyncIterator]() {
|
|
19
|
+
// reset state
|
|
20
|
+
this.completion = undefined;
|
|
21
|
+
if (this.chunks.length > 0) {
|
|
22
|
+
this.chunks = [];
|
|
23
|
+
}
|
|
24
|
+
const chunks = this.chunks;
|
|
25
|
+
|
|
26
|
+
this.driver.logger.debug(
|
|
27
|
+
`[${this.driver.provider}] Streaming Execution of ${this.options.model} with prompt`, this.prompt,
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
const start = Date.now();
|
|
31
|
+
const stream = await this.driver.requestCompletionStream(this.prompt, this.options);
|
|
32
|
+
|
|
33
|
+
for await (const chunk of stream) {
|
|
34
|
+
if (chunk) {
|
|
35
|
+
chunks.push(chunk);
|
|
36
|
+
yield chunk;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const content = chunks.join('');
|
|
41
|
+
const promptTokens = typeof this.prompt === 'string' ? this.prompt.length : JSON.stringify(this.prompt).length;
|
|
42
|
+
const resultTokens = content.length; //TODO use chunks.length ?
|
|
43
|
+
|
|
44
|
+
this.completion = {
|
|
45
|
+
result: content,
|
|
46
|
+
prompt: this.prompt,
|
|
47
|
+
execution_time: Date.now() - start,
|
|
48
|
+
token_usage: {
|
|
49
|
+
prompt: promptTokens,
|
|
50
|
+
result: resultTokens,
|
|
51
|
+
total: resultTokens + promptTokens,
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
this.driver.validateResult(this.completion, this.options);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export class FallbackCompletionStream<PromptT = any> implements CompletionStream<PromptT> {
|
|
61
|
+
|
|
62
|
+
prompt: PromptT;
|
|
63
|
+
chunks: string[];
|
|
64
|
+
completion: ExecutionResponse<PromptT> | undefined;
|
|
65
|
+
|
|
66
|
+
constructor(public driver: AbstractDriver<DriverOptions, PromptT>,
|
|
67
|
+
segments: PromptSegment[],
|
|
68
|
+
public options: ExecutionOptions) {
|
|
69
|
+
this.driver = driver;
|
|
70
|
+
this.prompt = this.driver.createPrompt(segments, options);
|
|
71
|
+
this.chunks = [];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async *[Symbol.asyncIterator]() {
|
|
75
|
+
// reset state
|
|
76
|
+
this.completion = undefined;
|
|
77
|
+
if (this.chunks.length > 0) {
|
|
78
|
+
this.chunks = [];
|
|
79
|
+
}
|
|
80
|
+
this.driver.logger.debug(
|
|
81
|
+
`[${this.driver.provider}] Streaming is not supported, falling back to blocking execution`
|
|
82
|
+
);
|
|
83
|
+
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);
|
|
87
|
+
yield content;
|
|
88
|
+
|
|
89
|
+
this.completion = completion;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
package/src/Driver.ts
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Classes to handle the execution of an interaction in an execution environment.
|
|
3
|
+
* Base abstract class is then implemented by each environment
|
|
4
|
+
* (eg: OpenAI, HuggingFace, etc.)
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { DefaultCompletionStream, FallbackCompletionStream } from "./CompletionStream.js";
|
|
8
|
+
import { PromptFormatters } from "./formatters.js";
|
|
9
|
+
import {
|
|
10
|
+
AIModel,
|
|
11
|
+
Completion,
|
|
12
|
+
CompletionStream,
|
|
13
|
+
DataSource,
|
|
14
|
+
DriverOptions,
|
|
15
|
+
ExecutionOptions,
|
|
16
|
+
ExecutionResponse,
|
|
17
|
+
Logger,
|
|
18
|
+
ModelSearchPayload,
|
|
19
|
+
PromptFormats,
|
|
20
|
+
PromptOptions,
|
|
21
|
+
PromptSegment,
|
|
22
|
+
TrainingJob,
|
|
23
|
+
TrainingOptions,
|
|
24
|
+
TrainingPromptOptions
|
|
25
|
+
} from "./types.js";
|
|
26
|
+
import { validateResult } from "./validation.js";
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
const ConsoleLogger: Logger = {
|
|
30
|
+
debug: console.debug,
|
|
31
|
+
info: console.info,
|
|
32
|
+
warn: console.warn,
|
|
33
|
+
error: console.error,
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const noop = () => void 0;
|
|
37
|
+
const NoopLogger: Logger = {
|
|
38
|
+
debug: noop,
|
|
39
|
+
info: noop,
|
|
40
|
+
warn: noop,
|
|
41
|
+
error: noop,
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function createLogger(logger: Logger | false | undefined) {
|
|
45
|
+
return logger ? logger : logger === false ? NoopLogger : ConsoleLogger;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
export interface Driver<PromptT = unknown> {
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
*
|
|
53
|
+
* @param segments
|
|
54
|
+
* @param completion
|
|
55
|
+
* @param model the model to train
|
|
56
|
+
*/
|
|
57
|
+
createTrainingPrompt(options: TrainingPromptOptions): string;
|
|
58
|
+
|
|
59
|
+
createPrompt(segments: PromptSegment[], opts: PromptOptions): PromptT;
|
|
60
|
+
|
|
61
|
+
execute(segments: PromptSegment[], options: ExecutionOptions): Promise<ExecutionResponse<PromptT>>;
|
|
62
|
+
|
|
63
|
+
// by default no stream is supported. we block and we return all at once
|
|
64
|
+
//stream(segments: PromptSegment[], options: ExecutionOptions): Promise<StreamingExecutionResponse<PromptT>>;
|
|
65
|
+
stream(segments: PromptSegment[], options: ExecutionOptions): Promise<CompletionStream<PromptT>>;
|
|
66
|
+
|
|
67
|
+
startTraining(dataset: DataSource, options: TrainingOptions): Promise<TrainingJob>;
|
|
68
|
+
|
|
69
|
+
cancelTraining(jobId: string): Promise<TrainingJob>;
|
|
70
|
+
|
|
71
|
+
getTrainingJob(jobId: string): Promise<TrainingJob>;
|
|
72
|
+
|
|
73
|
+
//list models available for this environement
|
|
74
|
+
listModels(params?: ModelSearchPayload): Promise<AIModel[]>;
|
|
75
|
+
|
|
76
|
+
//list models that can be trained
|
|
77
|
+
listTrainableModels(): Promise<AIModel[]>;
|
|
78
|
+
|
|
79
|
+
//check that it is possible to connect to the environment
|
|
80
|
+
validateConnection(): Promise<boolean>;
|
|
81
|
+
|
|
82
|
+
//generate embeddings for a given text
|
|
83
|
+
generateEmbeddings(content: string, model?: string): Promise<{ embeddings: number[], model: string; }>;
|
|
84
|
+
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* To be implemented by each driver
|
|
89
|
+
*/
|
|
90
|
+
export abstract class AbstractDriver<OptionsT extends DriverOptions = DriverOptions, PromptT = unknown> implements Driver<PromptT> {
|
|
91
|
+
options: OptionsT;
|
|
92
|
+
logger: Logger;
|
|
93
|
+
|
|
94
|
+
abstract provider: string; // the provider name
|
|
95
|
+
abstract defaultFormat: PromptFormats;
|
|
96
|
+
|
|
97
|
+
constructor(opts: OptionsT) {
|
|
98
|
+
this.options = opts;
|
|
99
|
+
this.logger = createLogger(opts.logger);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
createTrainingPrompt(options: TrainingPromptOptions): string {
|
|
103
|
+
const prompt = this.createPrompt(options.segments, { resultSchema: options.schema, model: options.model })
|
|
104
|
+
return JSON.stringify({
|
|
105
|
+
prompt,
|
|
106
|
+
completion: typeof options.completion === 'string' ? options.completion : JSON.stringify(options.completion)
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
startTraining(_dataset: DataSource, _options: TrainingOptions): Promise<TrainingJob> {
|
|
111
|
+
throw new Error("Method not implemented.");
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
cancelTraining(_jobId: string): Promise<TrainingJob> {
|
|
115
|
+
throw new Error("Method not implemented.");
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
getTrainingJob(_jobId: string): Promise<TrainingJob> {
|
|
119
|
+
throw new Error("Method not implemented.");
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
validateResult(result: Completion, options: ExecutionOptions) {
|
|
123
|
+
if (!result.error && options.resultSchema) {
|
|
124
|
+
try {
|
|
125
|
+
result.result = validateResult(result.result, options.resultSchema);
|
|
126
|
+
} catch (error: any) {
|
|
127
|
+
this.logger?.error({ err: error, data: result.result }, `[${this.provider}] [${options.model}] ${error.code ? '[' + error.code + '] ' : ''}Result validation error: ${error.message}`);
|
|
128
|
+
result.error = {
|
|
129
|
+
code: error.code || error.name,
|
|
130
|
+
message: error.message,
|
|
131
|
+
data: result.result,
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
async execute(segments: PromptSegment[], options: ExecutionOptions): Promise<ExecutionResponse<PromptT>> {
|
|
138
|
+
const prompt = this.createPrompt(segments, options);
|
|
139
|
+
return this._execute(prompt, options);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
async _execute(prompt: PromptT, options: ExecutionOptions): Promise<ExecutionResponse<PromptT>> {
|
|
143
|
+
this.logger.debug(
|
|
144
|
+
`[${this.provider}] Executing ${options.model} with prompt`, prompt,
|
|
145
|
+
);
|
|
146
|
+
try {
|
|
147
|
+
const start = Date.now();
|
|
148
|
+
const result = await this.requestCompletion(prompt, options);
|
|
149
|
+
this.validateResult(result, options);
|
|
150
|
+
const execution_time = Date.now() - start;
|
|
151
|
+
return { ...result, prompt, execution_time };
|
|
152
|
+
} catch (error) {
|
|
153
|
+
(error as any).prompt = prompt;
|
|
154
|
+
throw error;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// by default no stream is supported. we block and we return all at once
|
|
159
|
+
async stream(segments: PromptSegment[], options: ExecutionOptions): Promise<CompletionStream<PromptT>> {
|
|
160
|
+
const canStream = await this.canStream(options);
|
|
161
|
+
if (canStream) {
|
|
162
|
+
return new DefaultCompletionStream(this, segments, options);
|
|
163
|
+
} else {
|
|
164
|
+
return new FallbackCompletionStream(this, segments, options);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
public createPrompt(segments: PromptSegment[], opts: PromptOptions): PromptT {
|
|
169
|
+
return PromptFormatters[opts.format || this.defaultFormat](
|
|
170
|
+
segments,
|
|
171
|
+
opts.resultSchema
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Must be overrided if the implementation cannot stream.
|
|
177
|
+
* Some implementation may be able to stream for certain models but not for others.
|
|
178
|
+
* You must overwrite and return false if the current model doesn't support streaming.
|
|
179
|
+
* The default implementation returns true, so it is assumed that the streaming can be done.
|
|
180
|
+
* If this method returns false then the streaming execution will fallback on a blocking execution streaming the entire response as a single event.
|
|
181
|
+
* @param options the execution options containing the target model name.
|
|
182
|
+
* @returns true if the exeuction can be streamed false otherwise.
|
|
183
|
+
*/
|
|
184
|
+
protected canStream(_options: ExecutionOptions) {
|
|
185
|
+
return Promise.resolve(true);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
abstract requestCompletion(prompt: PromptT, options: ExecutionOptions): Promise<Completion>;
|
|
189
|
+
|
|
190
|
+
abstract requestCompletionStream(prompt: PromptT, options: ExecutionOptions): Promise<AsyncIterable<string>>;
|
|
191
|
+
|
|
192
|
+
//list models available for this environement
|
|
193
|
+
abstract listModels(params: ModelSearchPayload): Promise<AIModel[]>;
|
|
194
|
+
|
|
195
|
+
abstract listTrainableModels(): Promise<AIModel[]>;
|
|
196
|
+
|
|
197
|
+
//check that it is possible to connect to the environment
|
|
198
|
+
abstract validateConnection(): Promise<boolean>;
|
|
199
|
+
|
|
200
|
+
//generate embeddings for a given text
|
|
201
|
+
abstract generateEmbeddings(content: string, model?: string): Promise<{ embeddings: number[], model: string; }>;
|
|
202
|
+
|
|
203
|
+
}
|
|
204
|
+
|