ai 3.1.0-canary.2 → 3.1.0-canary.4
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 +1 -1
- package/ai-model-specification/dist/index.d.mts +152 -26
- package/ai-model-specification/dist/index.d.ts +152 -26
- package/ai-model-specification/dist/index.js +263 -128
- package/ai-model-specification/dist/index.js.map +1 -1
- package/ai-model-specification/dist/index.mjs +256 -126
- package/ai-model-specification/dist/index.mjs.map +1 -1
- package/core/dist/index.d.mts +118 -17
- package/core/dist/index.d.ts +118 -17
- package/core/dist/index.js +743 -445
- package/core/dist/index.js.map +1 -1
- package/core/dist/index.mjs +745 -444
- package/core/dist/index.mjs.map +1 -1
- package/dist/index.d.mts +3 -2
- package/dist/index.d.ts +3 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs.map +1 -1
- package/{provider → openai}/dist/index.d.mts +124 -45
- package/{provider → openai}/dist/index.d.ts +124 -45
- package/{provider → openai}/dist/index.js +267 -192
- package/openai/dist/index.js.map +1 -0
- package/{provider → openai}/dist/index.mjs +261 -187
- package/openai/dist/index.mjs.map +1 -0
- package/package.json +10 -10
- package/react/dist/index.d.mts +8 -4
- package/react/dist/index.d.ts +8 -4
- package/react/dist/index.js +2 -1
- package/react/dist/index.js.map +1 -1
- package/react/dist/index.mjs +2 -1
- package/react/dist/index.mjs.map +1 -1
- package/provider/dist/index.js.map +0 -1
- package/provider/dist/index.mjs.map +0 -1
@@ -1,5 +1,5 @@
|
|
1
1
|
// ai-model-specification/errors/api-call-error.ts
|
2
|
-
var
|
2
|
+
var APICallError = class extends Error {
|
3
3
|
constructor({
|
4
4
|
message,
|
5
5
|
url,
|
@@ -7,11 +7,15 @@ var ApiCallError = class extends Error {
|
|
7
7
|
statusCode,
|
8
8
|
responseBody,
|
9
9
|
cause,
|
10
|
-
isRetryable = statusCode != null && (statusCode ===
|
10
|
+
isRetryable = statusCode != null && (statusCode === 408 || // request timeout
|
11
|
+
statusCode === 409 || // conflict
|
12
|
+
statusCode === 429 || // too many requests
|
13
|
+
statusCode >= 500),
|
14
|
+
// server error
|
11
15
|
data
|
12
16
|
}) {
|
13
17
|
super(message);
|
14
|
-
this.name = "
|
18
|
+
this.name = "AI_APICallError";
|
15
19
|
this.url = url;
|
16
20
|
this.requestBodyValues = requestBodyValues;
|
17
21
|
this.statusCode = statusCode;
|
@@ -20,6 +24,9 @@ var ApiCallError = class extends Error {
|
|
20
24
|
this.isRetryable = isRetryable;
|
21
25
|
this.data = data;
|
22
26
|
}
|
27
|
+
static isAPICallError(error) {
|
28
|
+
return error instanceof Error && error.name === "AI_APICallError" && typeof error.url === "string" && typeof error.requestBodyValues === "object" && (error.statusCode == null || typeof error.statusCode === "number") && (error.responseBody == null || typeof error.responseBody === "string") && (error.cause == null || typeof error.cause === "object") && typeof error.isRetryable === "boolean" && (error.data == null || typeof error.data === "object");
|
29
|
+
}
|
23
30
|
toJSON() {
|
24
31
|
return {
|
25
32
|
name: this.name,
|
@@ -35,6 +42,26 @@ var ApiCallError = class extends Error {
|
|
35
42
|
}
|
36
43
|
};
|
37
44
|
|
45
|
+
// ai-model-specification/errors/invalid-prompt-error.ts
|
46
|
+
var InvalidPromptError = class extends Error {
|
47
|
+
constructor({ prompt: prompt2, message }) {
|
48
|
+
super(`Invalid prompt: ${message}`);
|
49
|
+
this.name = "AI_InvalidPromptError";
|
50
|
+
this.prompt = prompt2;
|
51
|
+
}
|
52
|
+
static isInvalidPromptError(error) {
|
53
|
+
return error instanceof Error && error.name === "AI_InvalidPromptError" && prompt != null;
|
54
|
+
}
|
55
|
+
toJSON() {
|
56
|
+
return {
|
57
|
+
name: this.name,
|
58
|
+
message: this.message,
|
59
|
+
stack: this.stack,
|
60
|
+
prompt: this.prompt
|
61
|
+
};
|
62
|
+
}
|
63
|
+
};
|
64
|
+
|
38
65
|
// ai-model-specification/util/get-error-message.ts
|
39
66
|
function getErrorMessage(error) {
|
40
67
|
if (error == null) {
|
@@ -49,6 +76,50 @@ function getErrorMessage(error) {
|
|
49
76
|
return JSON.stringify(error);
|
50
77
|
}
|
51
78
|
|
79
|
+
// ai-model-specification/errors/load-api-key-error.ts
|
80
|
+
var LoadAPIKeyError = class extends Error {
|
81
|
+
constructor({ message }) {
|
82
|
+
super(message);
|
83
|
+
this.name = "AI_LoadAPIKeyError";
|
84
|
+
}
|
85
|
+
static isLoadAPIKeyError(error) {
|
86
|
+
return error instanceof Error && error.name === "AI_LoadAPIKeyError";
|
87
|
+
}
|
88
|
+
toJSON() {
|
89
|
+
return {
|
90
|
+
name: this.name,
|
91
|
+
message: this.message
|
92
|
+
};
|
93
|
+
}
|
94
|
+
};
|
95
|
+
|
96
|
+
// ai-model-specification/util/load-api-key.ts
|
97
|
+
function loadApiKey({
|
98
|
+
apiKey,
|
99
|
+
environmentVariableName,
|
100
|
+
apiKeyParameterName = "apiKey",
|
101
|
+
description
|
102
|
+
}) {
|
103
|
+
if (apiKey != null) {
|
104
|
+
return apiKey;
|
105
|
+
}
|
106
|
+
if (typeof process === "undefined") {
|
107
|
+
throw new LoadAPIKeyError({
|
108
|
+
message: `${description} API key is missing. Pass it using the '${apiKeyParameterName}' parameter. Environment variables is not supported in this environment.`
|
109
|
+
});
|
110
|
+
}
|
111
|
+
apiKey = process.env[environmentVariableName];
|
112
|
+
if (apiKey == null) {
|
113
|
+
throw new LoadAPIKeyError({
|
114
|
+
message: `${description} API key is missing. Pass it using the '${apiKeyParameterName}' parameter or the ${environmentVariableName} environment variable.`
|
115
|
+
});
|
116
|
+
}
|
117
|
+
return apiKey;
|
118
|
+
}
|
119
|
+
|
120
|
+
// ai-model-specification/util/parse-json.ts
|
121
|
+
import SecureJSON from "secure-json-parse";
|
122
|
+
|
52
123
|
// ai-model-specification/errors/json-parse-error.ts
|
53
124
|
var JSONParseError = class extends Error {
|
54
125
|
constructor({ text, cause }) {
|
@@ -56,10 +127,13 @@ var JSONParseError = class extends Error {
|
|
56
127
|
`JSON parsing failed: Text: ${text}.
|
57
128
|
Error message: ${getErrorMessage(cause)}`
|
58
129
|
);
|
59
|
-
this.name = "
|
130
|
+
this.name = "AI_JSONParseError";
|
60
131
|
this.cause = cause;
|
61
132
|
this.text = text;
|
62
133
|
}
|
134
|
+
static isJSONParseError(error) {
|
135
|
+
return error instanceof Error && error.name === "AI_JSONParseError" && typeof error.text === "string" && typeof error.cause === "string";
|
136
|
+
}
|
63
137
|
toJSON() {
|
64
138
|
return {
|
65
139
|
name: this.name,
|
@@ -71,20 +145,6 @@ Error message: ${getErrorMessage(cause)}`
|
|
71
145
|
}
|
72
146
|
};
|
73
147
|
|
74
|
-
// ai-model-specification/errors/load-api-key-error.ts
|
75
|
-
var LoadAPIKeyError = class extends Error {
|
76
|
-
constructor({ message }) {
|
77
|
-
super(message);
|
78
|
-
this.name = "LoadAPIKeyError";
|
79
|
-
}
|
80
|
-
toJSON() {
|
81
|
-
return {
|
82
|
-
name: this.name,
|
83
|
-
message: this.message
|
84
|
-
};
|
85
|
-
}
|
86
|
-
};
|
87
|
-
|
88
148
|
// ai-model-specification/errors/type-validation-error.ts
|
89
149
|
var TypeValidationError = class extends Error {
|
90
150
|
constructor({ value, cause }) {
|
@@ -92,10 +152,13 @@ var TypeValidationError = class extends Error {
|
|
92
152
|
`Type validation failed: Value: ${JSON.stringify(value)}.
|
93
153
|
Error message: ${getErrorMessage(cause)}`
|
94
154
|
);
|
95
|
-
this.name = "
|
155
|
+
this.name = "AI_TypeValidationError";
|
96
156
|
this.cause = cause;
|
97
157
|
this.value = value;
|
98
158
|
}
|
159
|
+
static isTypeValidationError(error) {
|
160
|
+
return error instanceof Error && error.name === "AI_TypeValidationError" && typeof error.value === "string" && typeof error.cause === "string";
|
161
|
+
}
|
99
162
|
toJSON() {
|
100
163
|
return {
|
101
164
|
name: this.name,
|
@@ -107,58 +170,6 @@ Error message: ${getErrorMessage(cause)}`
|
|
107
170
|
}
|
108
171
|
};
|
109
172
|
|
110
|
-
// ai-model-specification/errors/unsupported-functionality-error.ts
|
111
|
-
var UnsupportedFunctionalityError = class extends Error {
|
112
|
-
constructor({
|
113
|
-
provider,
|
114
|
-
functionality
|
115
|
-
}) {
|
116
|
-
super(
|
117
|
-
`Functionality not supported by the provider. Provider: ${provider}.
|
118
|
-
Functionality: ${functionality}`
|
119
|
-
);
|
120
|
-
this.name = "UnsupportedFunctionalityError";
|
121
|
-
this.provider = provider;
|
122
|
-
this.functionality = functionality;
|
123
|
-
}
|
124
|
-
toJSON() {
|
125
|
-
return {
|
126
|
-
name: this.name,
|
127
|
-
message: this.message,
|
128
|
-
stack: this.stack,
|
129
|
-
provider: this.provider,
|
130
|
-
functionality: this.functionality
|
131
|
-
};
|
132
|
-
}
|
133
|
-
};
|
134
|
-
|
135
|
-
// ai-model-specification/util/load-api-key.ts
|
136
|
-
function loadApiKey({
|
137
|
-
apiKey,
|
138
|
-
environmentVariableName,
|
139
|
-
apiKeyParameterName = "apiKey",
|
140
|
-
description
|
141
|
-
}) {
|
142
|
-
if (apiKey != null) {
|
143
|
-
return apiKey;
|
144
|
-
}
|
145
|
-
if (typeof process === "undefined") {
|
146
|
-
throw new LoadAPIKeyError({
|
147
|
-
message: `${description} API key is missing. Pass it using the '${apiKeyParameterName}' parameter. Environment variables is not supported in this environment.`
|
148
|
-
});
|
149
|
-
}
|
150
|
-
apiKey = process.env[environmentVariableName];
|
151
|
-
if (apiKey == null) {
|
152
|
-
throw new LoadAPIKeyError({
|
153
|
-
message: `${description} API key is missing. Pass it using the '${apiKeyParameterName}' parameter or the ${environmentVariableName} environment variable.`
|
154
|
-
});
|
155
|
-
}
|
156
|
-
return apiKey;
|
157
|
-
}
|
158
|
-
|
159
|
-
// ai-model-specification/util/parse-json.ts
|
160
|
-
import SecureJSON from "secure-json-parse";
|
161
|
-
|
162
173
|
// ai-model-specification/util/validate-types.ts
|
163
174
|
function validateTypes({
|
164
175
|
value,
|
@@ -192,7 +203,7 @@ function safeValidateTypes({
|
|
192
203
|
} catch (error) {
|
193
204
|
return {
|
194
205
|
success: false,
|
195
|
-
error: error
|
206
|
+
error: TypeValidationError.isTypeValidationError(error) ? error : new TypeValidationError({ value, cause: error })
|
196
207
|
};
|
197
208
|
}
|
198
209
|
}
|
@@ -209,7 +220,7 @@ function parseJSON({
|
|
209
220
|
}
|
210
221
|
return validateTypes({ value, schema });
|
211
222
|
} catch (error) {
|
212
|
-
if (error
|
223
|
+
if (JSONParseError.isJSONParseError(error) || TypeValidationError.isTypeValidationError(error)) {
|
213
224
|
throw error;
|
214
225
|
}
|
215
226
|
throw new JSONParseError({ text, cause: error });
|
@@ -231,7 +242,7 @@ function safeParseJSON({
|
|
231
242
|
} catch (error) {
|
232
243
|
return {
|
233
244
|
success: false,
|
234
|
-
error: error
|
245
|
+
error: JSONParseError.isJSONParseError(error) ? error : new JSONParseError({ text, cause: error })
|
235
246
|
};
|
236
247
|
}
|
237
248
|
}
|
@@ -255,8 +266,8 @@ var postJsonToApi = async ({
|
|
255
266
|
}) => postToApi({
|
256
267
|
url,
|
257
268
|
headers: {
|
258
|
-
|
259
|
-
|
269
|
+
...headers,
|
270
|
+
"Content-Type": "application/json"
|
260
271
|
},
|
261
272
|
body: {
|
262
273
|
content: JSON.stringify(body),
|
@@ -275,9 +286,12 @@ var postToApi = async ({
|
|
275
286
|
abortSignal
|
276
287
|
}) => {
|
277
288
|
try {
|
289
|
+
const definedHeaders = Object.fromEntries(
|
290
|
+
Object.entries(headers).filter(([_key, value]) => value != null)
|
291
|
+
);
|
278
292
|
const response = await fetch(url, {
|
279
293
|
method: "POST",
|
280
|
-
headers,
|
294
|
+
headers: definedHeaders,
|
281
295
|
body: body.content,
|
282
296
|
signal: abortSignal
|
283
297
|
});
|
@@ -290,11 +304,11 @@ var postToApi = async ({
|
|
290
304
|
});
|
291
305
|
} catch (error) {
|
292
306
|
if (error instanceof Error) {
|
293
|
-
if (error.name === "AbortError" || error
|
307
|
+
if (error.name === "AbortError" || APICallError.isAPICallError(error)) {
|
294
308
|
throw error;
|
295
309
|
}
|
296
310
|
}
|
297
|
-
throw new
|
311
|
+
throw new APICallError({
|
298
312
|
message: "Failed to process error response",
|
299
313
|
cause: error,
|
300
314
|
statusCode: response.status,
|
@@ -311,11 +325,11 @@ var postToApi = async ({
|
|
311
325
|
});
|
312
326
|
} catch (error) {
|
313
327
|
if (error instanceof Error) {
|
314
|
-
if (error.name === "AbortError" || error
|
328
|
+
if (error.name === "AbortError" || APICallError.isAPICallError(error)) {
|
315
329
|
throw error;
|
316
330
|
}
|
317
331
|
}
|
318
|
-
throw new
|
332
|
+
throw new APICallError({
|
319
333
|
message: "Failed to process successful response",
|
320
334
|
cause: error,
|
321
335
|
statusCode: response.status,
|
@@ -332,12 +346,13 @@ var postToApi = async ({
|
|
332
346
|
if (error instanceof TypeError && error.message === "fetch failed") {
|
333
347
|
const cause = error.cause;
|
334
348
|
if (cause != null) {
|
335
|
-
throw new
|
349
|
+
throw new APICallError({
|
336
350
|
message: `Cannot connect to API: ${cause.message}`,
|
337
351
|
cause,
|
338
352
|
url,
|
339
353
|
requestBodyValues: body.values,
|
340
354
|
isRetryable: true
|
355
|
+
// retry when network error
|
341
356
|
});
|
342
357
|
}
|
343
358
|
}
|
@@ -356,7 +371,7 @@ var createJsonErrorResponseHandler = ({
|
|
356
371
|
}) => async ({ response, url, requestBodyValues }) => {
|
357
372
|
const responseBody = await response.text();
|
358
373
|
if (responseBody.trim() === "") {
|
359
|
-
return new
|
374
|
+
return new APICallError({
|
360
375
|
message: response.statusText,
|
361
376
|
url,
|
362
377
|
requestBodyValues,
|
@@ -370,7 +385,7 @@ var createJsonErrorResponseHandler = ({
|
|
370
385
|
text: responseBody,
|
371
386
|
schema: errorSchema
|
372
387
|
});
|
373
|
-
return new
|
388
|
+
return new APICallError({
|
374
389
|
message: errorToMessage(parsedError),
|
375
390
|
url,
|
376
391
|
requestBodyValues,
|
@@ -380,7 +395,7 @@ var createJsonErrorResponseHandler = ({
|
|
380
395
|
isRetryable: isRetryable == null ? void 0 : isRetryable(response, parsedError)
|
381
396
|
});
|
382
397
|
} catch (parseError) {
|
383
|
-
return new
|
398
|
+
return new APICallError({
|
384
399
|
message: response.statusText,
|
385
400
|
url,
|
386
401
|
requestBodyValues,
|
@@ -418,7 +433,7 @@ var createJsonResponseHandler = (responseSchema) => async ({ response, url, requ
|
|
418
433
|
schema: responseSchema
|
419
434
|
});
|
420
435
|
if (!parsedResult.success) {
|
421
|
-
throw new
|
436
|
+
throw new APICallError({
|
422
437
|
message: "Invalid JSON response",
|
423
438
|
cause: parsedResult.error,
|
424
439
|
statusCode: response.status,
|
@@ -455,14 +470,42 @@ function convertUint8ArrayToBase64(array) {
|
|
455
470
|
return globalThis.btoa(latin1string);
|
456
471
|
}
|
457
472
|
|
458
|
-
//
|
473
|
+
// ai-model-specification/errors/unsupported-functionality-error.ts
|
474
|
+
var UnsupportedFunctionalityError = class extends Error {
|
475
|
+
constructor({
|
476
|
+
provider,
|
477
|
+
functionality
|
478
|
+
}) {
|
479
|
+
super(
|
480
|
+
`Functionality not supported by the provider. Provider: ${provider}.
|
481
|
+
Functionality: ${functionality}`
|
482
|
+
);
|
483
|
+
this.name = "AI_UnsupportedFunctionalityError";
|
484
|
+
this.provider = provider;
|
485
|
+
this.functionality = functionality;
|
486
|
+
}
|
487
|
+
static isUnsupportedFunctionalityError(error) {
|
488
|
+
return error instanceof Error && error.name === "AI_UnsupportedFunctionalityError" && typeof error.provider === "string" && typeof error.functionality === "string";
|
489
|
+
}
|
490
|
+
toJSON() {
|
491
|
+
return {
|
492
|
+
name: this.name,
|
493
|
+
message: this.message,
|
494
|
+
stack: this.stack,
|
495
|
+
provider: this.provider,
|
496
|
+
functionality: this.functionality
|
497
|
+
};
|
498
|
+
}
|
499
|
+
};
|
500
|
+
|
501
|
+
// openai/openai-chat-language-model.ts
|
459
502
|
import { nanoid } from "nanoid";
|
460
503
|
import { z as z2 } from "zod";
|
461
504
|
|
462
|
-
//
|
463
|
-
function convertToOpenAIChatMessages(
|
505
|
+
// openai/convert-to-openai-chat-messages.ts
|
506
|
+
function convertToOpenAIChatMessages(prompt2) {
|
464
507
|
const messages = [];
|
465
|
-
for (const { role, content } of
|
508
|
+
for (const { role, content } of prompt2) {
|
466
509
|
switch (role) {
|
467
510
|
case "system": {
|
468
511
|
messages.push({ role: "system", content });
|
@@ -481,7 +524,7 @@ function convertToOpenAIChatMessages(prompt) {
|
|
481
524
|
return {
|
482
525
|
type: "image_url",
|
483
526
|
image_url: {
|
484
|
-
url: `data:${(_a = part.mimeType) != null ? _a : "image/jpeg"};base64,${convertUint8ArrayToBase64(part.image)}`
|
527
|
+
url: part.image instanceof URL ? part.image.toString() : `data:${(_a = part.mimeType) != null ? _a : "image/jpeg"};base64,${convertUint8ArrayToBase64(part.image)}`
|
485
528
|
}
|
486
529
|
};
|
487
530
|
}
|
@@ -542,7 +585,24 @@ function convertToOpenAIChatMessages(prompt) {
|
|
542
585
|
return messages;
|
543
586
|
}
|
544
587
|
|
545
|
-
//
|
588
|
+
// openai/map-openai-finish-reason.ts
|
589
|
+
function mapOpenAIFinishReason(finishReason) {
|
590
|
+
switch (finishReason) {
|
591
|
+
case "stop":
|
592
|
+
return "stop";
|
593
|
+
case "length":
|
594
|
+
return "length";
|
595
|
+
case "content-filter":
|
596
|
+
return "content-filter";
|
597
|
+
case "function_call":
|
598
|
+
case "tool-calls":
|
599
|
+
return "tool-calls";
|
600
|
+
default:
|
601
|
+
return "other";
|
602
|
+
}
|
603
|
+
}
|
604
|
+
|
605
|
+
// openai/openai-error.ts
|
546
606
|
import { z } from "zod";
|
547
607
|
var openAIErrorDataSchema = z.object({
|
548
608
|
error: z.object({
|
@@ -554,28 +614,24 @@ var openAIErrorDataSchema = z.object({
|
|
554
614
|
});
|
555
615
|
var openaiFailedResponseHandler = createJsonErrorResponseHandler({
|
556
616
|
errorSchema: openAIErrorDataSchema,
|
557
|
-
errorToMessage: (data) => data.error.message
|
558
|
-
isRetryable: (response, error) => response.status >= 500 || response.status === 429 && // insufficient_quota is also reported as a 429, but it's not retryable:
|
559
|
-
(error == null ? void 0 : error.error.type) !== "insufficient_quota"
|
617
|
+
errorToMessage: (data) => data.error.message
|
560
618
|
});
|
561
619
|
|
562
|
-
//
|
620
|
+
// openai/openai-chat-language-model.ts
|
563
621
|
var OpenAIChatLanguageModel = class {
|
564
|
-
constructor(settings, config) {
|
622
|
+
constructor(modelId, settings, config) {
|
565
623
|
this.specificationVersion = "v1";
|
566
624
|
this.defaultObjectGenerationMode = "tool";
|
625
|
+
this.modelId = modelId;
|
567
626
|
this.settings = settings;
|
568
627
|
this.config = config;
|
569
628
|
}
|
570
629
|
get provider() {
|
571
630
|
return this.config.provider;
|
572
631
|
}
|
573
|
-
get modelId() {
|
574
|
-
return this.settings.id;
|
575
|
-
}
|
576
632
|
getArgs({
|
577
633
|
mode,
|
578
|
-
prompt,
|
634
|
+
prompt: prompt2,
|
579
635
|
maxTokens,
|
580
636
|
temperature,
|
581
637
|
topP,
|
@@ -586,8 +642,11 @@ var OpenAIChatLanguageModel = class {
|
|
586
642
|
var _a;
|
587
643
|
const type = mode.type;
|
588
644
|
const baseArgs = {
|
645
|
+
// model id:
|
646
|
+
model: this.modelId,
|
589
647
|
// model specific settings:
|
590
|
-
|
648
|
+
logit_bias: this.settings.logitBias,
|
649
|
+
user: this.settings.user,
|
591
650
|
// standardized settings:
|
592
651
|
max_tokens: maxTokens,
|
593
652
|
temperature: scale({
|
@@ -612,7 +671,7 @@ var OpenAIChatLanguageModel = class {
|
|
612
671
|
}),
|
613
672
|
seed,
|
614
673
|
// messages:
|
615
|
-
messages: convertToOpenAIChatMessages(
|
674
|
+
messages: convertToOpenAIChatMessages(prompt2)
|
616
675
|
};
|
617
676
|
switch (type) {
|
618
677
|
case "regular": {
|
@@ -656,49 +715,54 @@ var OpenAIChatLanguageModel = class {
|
|
656
715
|
}
|
657
716
|
async doGenerate(options) {
|
658
717
|
var _a, _b;
|
718
|
+
const args = this.getArgs(options);
|
659
719
|
const response = await postJsonToApi({
|
660
720
|
url: `${this.config.baseUrl}/chat/completions`,
|
661
|
-
headers:
|
662
|
-
|
663
|
-
},
|
664
|
-
body: {
|
665
|
-
...this.getArgs(options)
|
666
|
-
},
|
721
|
+
headers: this.config.headers(),
|
722
|
+
body: args,
|
667
723
|
failedResponseHandler: openaiFailedResponseHandler,
|
668
724
|
successfulResponseHandler: createJsonResponseHandler(
|
669
725
|
openAIChatResponseSchema
|
670
|
-
)
|
726
|
+
),
|
727
|
+
abortSignal: options.abortSignal
|
671
728
|
});
|
672
|
-
const
|
729
|
+
const { messages: rawPrompt, ...rawSettings } = args;
|
730
|
+
const choice = response.choices[0];
|
673
731
|
return {
|
674
|
-
text: (_a = message.content) != null ? _a : void 0,
|
675
|
-
toolCalls: (_b = message.tool_calls) == null ? void 0 : _b.map((toolCall) => ({
|
732
|
+
text: (_a = choice.message.content) != null ? _a : void 0,
|
733
|
+
toolCalls: (_b = choice.message.tool_calls) == null ? void 0 : _b.map((toolCall) => ({
|
676
734
|
toolCallType: "function",
|
677
735
|
toolCallId: toolCall.id,
|
678
736
|
toolName: toolCall.function.name,
|
679
737
|
args: toolCall.function.arguments
|
680
738
|
})),
|
739
|
+
finishReason: mapOpenAIFinishReason(choice.finish_reason),
|
740
|
+
usage: {
|
741
|
+
promptTokens: response.usage.prompt_tokens,
|
742
|
+
completionTokens: response.usage.completion_tokens
|
743
|
+
},
|
744
|
+
rawCall: { rawPrompt, rawSettings },
|
681
745
|
warnings: []
|
682
746
|
};
|
683
747
|
}
|
684
748
|
async doStream(options) {
|
749
|
+
const args = this.getArgs(options);
|
685
750
|
const response = await postJsonToApi({
|
686
751
|
url: `${this.config.baseUrl}/chat/completions`,
|
687
|
-
headers:
|
688
|
-
Authorization: `Bearer ${this.config.apiKey()}`
|
689
|
-
},
|
752
|
+
headers: this.config.headers(),
|
690
753
|
body: {
|
691
|
-
...
|
754
|
+
...args,
|
692
755
|
stream: true
|
693
756
|
},
|
694
757
|
failedResponseHandler: openaiFailedResponseHandler,
|
695
758
|
successfulResponseHandler: createEventSourceResponseHandler(
|
696
759
|
openaiChatChunkSchema
|
697
|
-
)
|
760
|
+
),
|
761
|
+
abortSignal: options.abortSignal
|
698
762
|
});
|
763
|
+
const { messages: rawPrompt, ...rawSettings } = args;
|
699
764
|
const toolCalls = [];
|
700
765
|
return {
|
701
|
-
warnings: [],
|
702
766
|
stream: response.pipeThrough(
|
703
767
|
new TransformStream({
|
704
768
|
transform(chunk, controller) {
|
@@ -752,7 +816,9 @@ var OpenAIChatLanguageModel = class {
|
|
752
816
|
}
|
753
817
|
}
|
754
818
|
})
|
755
|
-
)
|
819
|
+
),
|
820
|
+
rawCall: { rawPrompt, rawSettings },
|
821
|
+
warnings: []
|
756
822
|
};
|
757
823
|
}
|
758
824
|
};
|
@@ -808,32 +874,34 @@ var openaiChatChunkSchema = z2.object({
|
|
808
874
|
)
|
809
875
|
});
|
810
876
|
|
811
|
-
//
|
877
|
+
// openai/openai-completion-language-model.ts
|
812
878
|
import { z as z3 } from "zod";
|
813
879
|
|
814
|
-
//
|
880
|
+
// openai/convert-to-openai-completion-prompt.ts
|
815
881
|
function convertToOpenAICompletionPrompt({
|
816
|
-
prompt,
|
882
|
+
prompt: prompt2,
|
817
883
|
inputFormat,
|
818
884
|
provider,
|
819
885
|
user = "user",
|
820
886
|
assistant = "assistant"
|
821
887
|
}) {
|
822
|
-
if (inputFormat === "prompt" &&
|
823
|
-
return { prompt:
|
888
|
+
if (inputFormat === "prompt" && prompt2.length === 1 && prompt2[0].role === "user" && prompt2[0].content.length === 1 && prompt2[0].content[0].type === "text") {
|
889
|
+
return { prompt: prompt2[0].content[0].text };
|
824
890
|
}
|
825
891
|
let text = "";
|
826
|
-
if (
|
827
|
-
text += `${
|
892
|
+
if (prompt2[0].role === "system") {
|
893
|
+
text += `${prompt2[0].content}
|
828
894
|
|
829
895
|
`;
|
830
|
-
|
896
|
+
prompt2 = prompt2.slice(1);
|
831
897
|
}
|
832
|
-
for (const { role, content } of
|
898
|
+
for (const { role, content } of prompt2) {
|
833
899
|
switch (role) {
|
834
900
|
case "system": {
|
835
|
-
throw new
|
836
|
-
|
901
|
+
throw new InvalidPromptError({
|
902
|
+
message: "Unexpected system message in prompt: ${content}",
|
903
|
+
prompt: prompt2
|
904
|
+
});
|
837
905
|
}
|
838
906
|
case "user": {
|
839
907
|
const userMessage = content.map((part) => {
|
@@ -896,24 +964,22 @@ ${user}:`]
|
|
896
964
|
};
|
897
965
|
}
|
898
966
|
|
899
|
-
//
|
967
|
+
// openai/openai-completion-language-model.ts
|
900
968
|
var OpenAICompletionLanguageModel = class {
|
901
|
-
constructor(settings, config) {
|
969
|
+
constructor(modelId, settings, config) {
|
902
970
|
this.specificationVersion = "v1";
|
903
971
|
this.defaultObjectGenerationMode = void 0;
|
972
|
+
this.modelId = modelId;
|
904
973
|
this.settings = settings;
|
905
974
|
this.config = config;
|
906
975
|
}
|
907
976
|
get provider() {
|
908
977
|
return this.config.provider;
|
909
978
|
}
|
910
|
-
get modelId() {
|
911
|
-
return this.settings.id;
|
912
|
-
}
|
913
979
|
getArgs({
|
914
980
|
mode,
|
915
981
|
inputFormat,
|
916
|
-
prompt,
|
982
|
+
prompt: prompt2,
|
917
983
|
maxTokens,
|
918
984
|
temperature,
|
919
985
|
topP,
|
@@ -924,13 +990,18 @@ var OpenAICompletionLanguageModel = class {
|
|
924
990
|
var _a;
|
925
991
|
const type = mode.type;
|
926
992
|
const { prompt: completionPrompt, stopSequences } = convertToOpenAICompletionPrompt({
|
927
|
-
prompt,
|
993
|
+
prompt: prompt2,
|
928
994
|
inputFormat,
|
929
995
|
provider: this.provider
|
930
996
|
});
|
931
997
|
const baseArgs = {
|
998
|
+
// model id:
|
999
|
+
model: this.modelId,
|
932
1000
|
// model specific settings:
|
933
|
-
|
1001
|
+
echo: this.settings.echo,
|
1002
|
+
logit_bias: this.settings.logitBias,
|
1003
|
+
suffix: this.settings.suffix,
|
1004
|
+
user: this.settings.user,
|
934
1005
|
// standardized settings:
|
935
1006
|
max_tokens: maxTokens,
|
936
1007
|
temperature: scale({
|
@@ -994,30 +1065,35 @@ var OpenAICompletionLanguageModel = class {
|
|
994
1065
|
}
|
995
1066
|
}
|
996
1067
|
async doGenerate(options) {
|
1068
|
+
const args = this.getArgs(options);
|
997
1069
|
const response = await postJsonToApi({
|
998
1070
|
url: `${this.config.baseUrl}/completions`,
|
999
|
-
headers:
|
1000
|
-
|
1001
|
-
},
|
1002
|
-
body: {
|
1003
|
-
...this.getArgs(options)
|
1004
|
-
},
|
1071
|
+
headers: this.config.headers(),
|
1072
|
+
body: args,
|
1005
1073
|
failedResponseHandler: openaiFailedResponseHandler,
|
1006
1074
|
successfulResponseHandler: createJsonResponseHandler(
|
1007
1075
|
openAICompletionResponseSchema
|
1008
|
-
)
|
1076
|
+
),
|
1077
|
+
abortSignal: options.abortSignal
|
1009
1078
|
});
|
1079
|
+
const { prompt: rawPrompt, ...rawSettings } = args;
|
1080
|
+
const choice = response.choices[0];
|
1010
1081
|
return {
|
1011
|
-
text:
|
1082
|
+
text: choice.text,
|
1083
|
+
usage: {
|
1084
|
+
promptTokens: response.usage.prompt_tokens,
|
1085
|
+
completionTokens: response.usage.completion_tokens
|
1086
|
+
},
|
1087
|
+
finishReason: mapOpenAIFinishReason(choice.finish_reason),
|
1088
|
+
rawCall: { rawPrompt, rawSettings },
|
1012
1089
|
warnings: []
|
1013
1090
|
};
|
1014
1091
|
}
|
1015
1092
|
async doStream(options) {
|
1093
|
+
const args = this.getArgs(options);
|
1016
1094
|
const response = await postJsonToApi({
|
1017
1095
|
url: `${this.config.baseUrl}/completions`,
|
1018
|
-
headers:
|
1019
|
-
Authorization: `Bearer ${this.config.apiKey()}`
|
1020
|
-
},
|
1096
|
+
headers: this.config.headers(),
|
1021
1097
|
body: {
|
1022
1098
|
...this.getArgs(options),
|
1023
1099
|
stream: true
|
@@ -1025,10 +1101,11 @@ var OpenAICompletionLanguageModel = class {
|
|
1025
1101
|
failedResponseHandler: openaiFailedResponseHandler,
|
1026
1102
|
successfulResponseHandler: createEventSourceResponseHandler(
|
1027
1103
|
openaiCompletionChunkSchema
|
1028
|
-
)
|
1104
|
+
),
|
1105
|
+
abortSignal: options.abortSignal
|
1029
1106
|
});
|
1107
|
+
const { prompt: rawPrompt, ...rawSettings } = args;
|
1030
1108
|
return {
|
1031
|
-
warnings: [],
|
1032
1109
|
stream: response.pipeThrough(
|
1033
1110
|
new TransformStream({
|
1034
1111
|
transform(chunk, controller) {
|
@@ -1046,7 +1123,9 @@ var OpenAICompletionLanguageModel = class {
|
|
1046
1123
|
}
|
1047
1124
|
}
|
1048
1125
|
})
|
1049
|
-
)
|
1126
|
+
),
|
1127
|
+
rawCall: { rawPrompt, rawSettings },
|
1128
|
+
warnings: []
|
1050
1129
|
};
|
1051
1130
|
}
|
1052
1131
|
};
|
@@ -1073,49 +1152,44 @@ var openaiCompletionChunkSchema = z3.object({
|
|
1073
1152
|
)
|
1074
1153
|
});
|
1075
1154
|
|
1076
|
-
//
|
1155
|
+
// openai/openai-facade.ts
|
1077
1156
|
var OpenAI = class {
|
1078
|
-
constructor(
|
1079
|
-
this.baseUrl = baseUrl;
|
1080
|
-
this.apiKey = apiKey;
|
1157
|
+
constructor(options = {}) {
|
1158
|
+
this.baseUrl = options.baseUrl;
|
1159
|
+
this.apiKey = options.apiKey;
|
1160
|
+
this.organization = options.organization;
|
1081
1161
|
}
|
1082
|
-
|
1162
|
+
get baseConfig() {
|
1083
1163
|
var _a;
|
1084
|
-
return
|
1085
|
-
|
1164
|
+
return {
|
1165
|
+
organization: this.organization,
|
1086
1166
|
baseUrl: (_a = this.baseUrl) != null ? _a : "https://api.openai.com/v1",
|
1087
|
-
|
1088
|
-
|
1089
|
-
environmentVariableName: "OPENAI_API_KEY",
|
1090
|
-
description: "OpenAI"
|
1091
|
-
}),
|
1092
|
-
mapSettings: (settings2) => ({
|
1093
|
-
model: settings2.id,
|
1094
|
-
logit_bias: settings2.logitBias
|
1095
|
-
})
|
1096
|
-
});
|
1097
|
-
}
|
1098
|
-
completion(settings) {
|
1099
|
-
var _a;
|
1100
|
-
return new OpenAICompletionLanguageModel(
|
1101
|
-
settings,
|
1102
|
-
{
|
1103
|
-
provider: "openai.completion",
|
1104
|
-
baseUrl: (_a = this.baseUrl) != null ? _a : "https://api.openai.com/v1",
|
1105
|
-
apiKey: () => loadApiKey({
|
1167
|
+
headers: () => ({
|
1168
|
+
Authorization: `Bearer ${loadApiKey({
|
1106
1169
|
apiKey: this.apiKey,
|
1107
1170
|
environmentVariableName: "OPENAI_API_KEY",
|
1108
1171
|
description: "OpenAI"
|
1109
|
-
})
|
1110
|
-
|
1111
|
-
|
1112
|
-
|
1113
|
-
|
1114
|
-
|
1115
|
-
|
1172
|
+
})}`,
|
1173
|
+
"OpenAI-Organization": this.organization
|
1174
|
+
})
|
1175
|
+
};
|
1176
|
+
}
|
1177
|
+
chat(modelId, settings = {}) {
|
1178
|
+
return new OpenAIChatLanguageModel(modelId, settings, {
|
1179
|
+
provider: "openai.chat",
|
1180
|
+
...this.baseConfig
|
1181
|
+
});
|
1182
|
+
}
|
1183
|
+
completion(modelId, settings = {}) {
|
1184
|
+
return new OpenAICompletionLanguageModel(modelId, settings, {
|
1185
|
+
provider: "openai.completion",
|
1186
|
+
...this.baseConfig
|
1187
|
+
});
|
1116
1188
|
}
|
1117
1189
|
};
|
1190
|
+
var openai = new OpenAI();
|
1118
1191
|
export {
|
1119
|
-
OpenAI
|
1192
|
+
OpenAI,
|
1193
|
+
openai
|
1120
1194
|
};
|
1121
1195
|
//# sourceMappingURL=index.mjs.map
|