@ai-sdk/openai-compatible 1.0.0-canary.6 → 1.0.0-canary.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +35 -0
- package/dist/index.d.mts +69 -56
- package/dist/index.d.ts +69 -56
- package/dist/index.js +252 -146
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +255 -146
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +3 -3
- package/dist/internal/index.d.ts +3 -3
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.mjs
CHANGED
@@ -9,9 +9,10 @@ import {
|
|
9
9
|
createJsonResponseHandler,
|
10
10
|
generateId,
|
11
11
|
isParsableJson,
|
12
|
+
parseProviderOptions,
|
12
13
|
postJsonToApi
|
13
14
|
} from "@ai-sdk/provider-utils";
|
14
|
-
import { z as
|
15
|
+
import { z as z3 } from "zod";
|
15
16
|
|
16
17
|
// src/convert-to-openai-compatible-chat-messages.ts
|
17
18
|
import {
|
@@ -152,17 +153,27 @@ function mapOpenAICompatibleFinishReason(finishReason) {
|
|
152
153
|
}
|
153
154
|
}
|
154
155
|
|
155
|
-
// src/openai-compatible-
|
156
|
+
// src/openai-compatible-chat-options.ts
|
156
157
|
import { z } from "zod";
|
157
|
-
var
|
158
|
-
|
159
|
-
|
158
|
+
var openaiCompatibleProviderOptions = z.object({
|
159
|
+
/**
|
160
|
+
* A unique identifier representing your end-user, which can help the provider to
|
161
|
+
* monitor and detect abuse.
|
162
|
+
*/
|
163
|
+
user: z.string().optional()
|
164
|
+
});
|
165
|
+
|
166
|
+
// src/openai-compatible-error.ts
|
167
|
+
import { z as z2 } from "zod";
|
168
|
+
var openaiCompatibleErrorDataSchema = z2.object({
|
169
|
+
error: z2.object({
|
170
|
+
message: z2.string(),
|
160
171
|
// The additional information below is handled loosely to support
|
161
172
|
// OpenAI-compatible providers that have slightly different error
|
162
173
|
// responses:
|
163
|
-
type:
|
164
|
-
param:
|
165
|
-
code:
|
174
|
+
type: z2.string().nullish(),
|
175
|
+
param: z2.any().nullish(),
|
176
|
+
code: z2.union([z2.string(), z2.number()]).nullish()
|
166
177
|
})
|
167
178
|
});
|
168
179
|
var defaultOpenAICompatibleErrorStructure = {
|
@@ -228,11 +239,10 @@ function prepareTools({
|
|
228
239
|
// src/openai-compatible-chat-language-model.ts
|
229
240
|
var OpenAICompatibleChatLanguageModel = class {
|
230
241
|
// type inferred via constructor
|
231
|
-
constructor(modelId,
|
242
|
+
constructor(modelId, config) {
|
232
243
|
this.specificationVersion = "v2";
|
233
244
|
var _a, _b;
|
234
245
|
this.modelId = modelId;
|
235
|
-
this.settings = settings;
|
236
246
|
this.config = config;
|
237
247
|
const errorStructure = (_a = config.errorStructure) != null ? _a : defaultOpenAICompatibleErrorStructure;
|
238
248
|
this.chunkSchema = createOpenAICompatibleChatChunkSchema(
|
@@ -265,8 +275,20 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
265
275
|
toolChoice,
|
266
276
|
tools
|
267
277
|
}) {
|
268
|
-
var _a;
|
278
|
+
var _a, _b, _c;
|
269
279
|
const warnings = [];
|
280
|
+
const compatibleOptions = Object.assign(
|
281
|
+
(_a = parseProviderOptions({
|
282
|
+
provider: "openai-compatible",
|
283
|
+
providerOptions,
|
284
|
+
schema: openaiCompatibleProviderOptions
|
285
|
+
})) != null ? _a : {},
|
286
|
+
(_b = parseProviderOptions({
|
287
|
+
provider: this.providerOptionsName,
|
288
|
+
providerOptions,
|
289
|
+
schema: openaiCompatibleProviderOptions
|
290
|
+
})) != null ? _b : {}
|
291
|
+
);
|
270
292
|
if (topK != null) {
|
271
293
|
warnings.push({ type: "unsupported-setting", setting: "topK" });
|
272
294
|
}
|
@@ -290,7 +312,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
290
312
|
// model id:
|
291
313
|
model: this.modelId,
|
292
314
|
// model specific settings:
|
293
|
-
user:
|
315
|
+
user: compatibleOptions.user,
|
294
316
|
// standardized settings:
|
295
317
|
max_tokens: maxOutputTokens,
|
296
318
|
temperature,
|
@@ -301,7 +323,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
301
323
|
type: "json_schema",
|
302
324
|
json_schema: {
|
303
325
|
schema: responseFormat.schema,
|
304
|
-
name: (
|
326
|
+
name: (_c = responseFormat.name) != null ? _c : "response",
|
305
327
|
description: responseFormat.description
|
306
328
|
}
|
307
329
|
} : { type: "json_object" } : void 0,
|
@@ -318,7 +340,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
318
340
|
};
|
319
341
|
}
|
320
342
|
async doGenerate(options) {
|
321
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i
|
343
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
322
344
|
const { args, warnings } = this.getArgs({ ...options });
|
323
345
|
const body = JSON.stringify(args);
|
324
346
|
const {
|
@@ -339,16 +361,39 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
339
361
|
abortSignal: options.abortSignal,
|
340
362
|
fetch: this.config.fetch
|
341
363
|
});
|
342
|
-
const { messages: rawPrompt, ...rawSettings } = args;
|
343
364
|
const choice = responseBody.choices[0];
|
365
|
+
const content = [];
|
366
|
+
const text = choice.message.content;
|
367
|
+
if (text != null && text.length > 0) {
|
368
|
+
content.push({ type: "text", text });
|
369
|
+
}
|
370
|
+
const reasoning = choice.message.reasoning_content;
|
371
|
+
if (reasoning != null && reasoning.length > 0) {
|
372
|
+
content.push({
|
373
|
+
type: "reasoning",
|
374
|
+
reasoningType: "text",
|
375
|
+
text: reasoning
|
376
|
+
});
|
377
|
+
}
|
378
|
+
if (choice.message.tool_calls != null) {
|
379
|
+
for (const toolCall of choice.message.tool_calls) {
|
380
|
+
content.push({
|
381
|
+
type: "tool-call",
|
382
|
+
toolCallType: "function",
|
383
|
+
toolCallId: (_a = toolCall.id) != null ? _a : generateId(),
|
384
|
+
toolName: toolCall.function.name,
|
385
|
+
args: toolCall.function.arguments
|
386
|
+
});
|
387
|
+
}
|
388
|
+
}
|
344
389
|
const providerMetadata = {
|
345
390
|
[this.providerOptionsName]: {},
|
346
|
-
...(
|
391
|
+
...(_c = (_b = this.config.metadataExtractor) == null ? void 0 : _b.extractMetadata) == null ? void 0 : _c.call(_b, {
|
347
392
|
parsedBody: rawResponse
|
348
393
|
})
|
349
394
|
};
|
350
|
-
const completionTokenDetails = (
|
351
|
-
const promptTokenDetails = (
|
395
|
+
const completionTokenDetails = (_d = responseBody.usage) == null ? void 0 : _d.completion_tokens_details;
|
396
|
+
const promptTokenDetails = (_e = responseBody.usage) == null ? void 0 : _e.prompt_tokens_details;
|
352
397
|
if ((completionTokenDetails == null ? void 0 : completionTokenDetails.reasoning_tokens) != null) {
|
353
398
|
providerMetadata[this.providerOptionsName].reasoningTokens = completionTokenDetails == null ? void 0 : completionTokenDetails.reasoning_tokens;
|
354
399
|
}
|
@@ -362,21 +407,11 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
362
407
|
providerMetadata[this.providerOptionsName].cachedPromptTokens = promptTokenDetails == null ? void 0 : promptTokenDetails.cached_tokens;
|
363
408
|
}
|
364
409
|
return {
|
365
|
-
|
366
|
-
reasoning: (_f = choice.message.reasoning_content) != null ? _f : void 0,
|
367
|
-
toolCalls: (_g = choice.message.tool_calls) == null ? void 0 : _g.map((toolCall) => {
|
368
|
-
var _a2;
|
369
|
-
return {
|
370
|
-
toolCallType: "function",
|
371
|
-
toolCallId: (_a2 = toolCall.id) != null ? _a2 : generateId(),
|
372
|
-
toolName: toolCall.function.name,
|
373
|
-
args: toolCall.function.arguments
|
374
|
-
};
|
375
|
-
}),
|
410
|
+
content,
|
376
411
|
finishReason: mapOpenAICompatibleFinishReason(choice.finish_reason),
|
377
412
|
usage: {
|
378
|
-
inputTokens: (
|
379
|
-
outputTokens: (
|
413
|
+
inputTokens: (_g = (_f = responseBody.usage) == null ? void 0 : _f.prompt_tokens) != null ? _g : void 0,
|
414
|
+
outputTokens: (_i = (_h = responseBody.usage) == null ? void 0 : _h.completion_tokens) != null ? _i : void 0
|
380
415
|
},
|
381
416
|
providerMetadata,
|
382
417
|
request: { body },
|
@@ -429,6 +464,9 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
429
464
|
return {
|
430
465
|
stream: response.pipeThrough(
|
431
466
|
new TransformStream({
|
467
|
+
start(controller) {
|
468
|
+
controller.enqueue({ type: "stream-start", warnings });
|
469
|
+
},
|
432
470
|
// TODO we lost type safety on Chunk, most likely due to the error schema. MUST FIX
|
433
471
|
transform(chunk, controller) {
|
434
472
|
var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
|
@@ -486,13 +524,14 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
486
524
|
if (delta.reasoning_content != null) {
|
487
525
|
controller.enqueue({
|
488
526
|
type: "reasoning",
|
489
|
-
|
527
|
+
reasoningType: "text",
|
528
|
+
text: delta.reasoning_content
|
490
529
|
});
|
491
530
|
}
|
492
531
|
if (delta.content != null) {
|
493
532
|
controller.enqueue({
|
494
|
-
type: "text
|
495
|
-
|
533
|
+
type: "text",
|
534
|
+
text: delta.content
|
496
535
|
});
|
497
536
|
}
|
498
537
|
if (delta.tool_calls != null) {
|
@@ -608,73 +647,72 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
608
647
|
})
|
609
648
|
),
|
610
649
|
request: { body },
|
611
|
-
response: { headers: responseHeaders }
|
612
|
-
warnings
|
650
|
+
response: { headers: responseHeaders }
|
613
651
|
};
|
614
652
|
}
|
615
653
|
};
|
616
|
-
var openaiCompatibleTokenUsageSchema =
|
617
|
-
prompt_tokens:
|
618
|
-
completion_tokens:
|
619
|
-
prompt_tokens_details:
|
620
|
-
cached_tokens:
|
654
|
+
var openaiCompatibleTokenUsageSchema = z3.object({
|
655
|
+
prompt_tokens: z3.number().nullish(),
|
656
|
+
completion_tokens: z3.number().nullish(),
|
657
|
+
prompt_tokens_details: z3.object({
|
658
|
+
cached_tokens: z3.number().nullish()
|
621
659
|
}).nullish(),
|
622
|
-
completion_tokens_details:
|
623
|
-
reasoning_tokens:
|
624
|
-
accepted_prediction_tokens:
|
625
|
-
rejected_prediction_tokens:
|
660
|
+
completion_tokens_details: z3.object({
|
661
|
+
reasoning_tokens: z3.number().nullish(),
|
662
|
+
accepted_prediction_tokens: z3.number().nullish(),
|
663
|
+
rejected_prediction_tokens: z3.number().nullish()
|
626
664
|
}).nullish()
|
627
665
|
}).nullish();
|
628
|
-
var OpenAICompatibleChatResponseSchema =
|
629
|
-
id:
|
630
|
-
created:
|
631
|
-
model:
|
632
|
-
choices:
|
633
|
-
|
634
|
-
message:
|
635
|
-
role:
|
636
|
-
content:
|
637
|
-
reasoning_content:
|
638
|
-
tool_calls:
|
639
|
-
|
640
|
-
id:
|
641
|
-
type:
|
642
|
-
function:
|
643
|
-
name:
|
644
|
-
arguments:
|
666
|
+
var OpenAICompatibleChatResponseSchema = z3.object({
|
667
|
+
id: z3.string().nullish(),
|
668
|
+
created: z3.number().nullish(),
|
669
|
+
model: z3.string().nullish(),
|
670
|
+
choices: z3.array(
|
671
|
+
z3.object({
|
672
|
+
message: z3.object({
|
673
|
+
role: z3.literal("assistant").nullish(),
|
674
|
+
content: z3.string().nullish(),
|
675
|
+
reasoning_content: z3.string().nullish(),
|
676
|
+
tool_calls: z3.array(
|
677
|
+
z3.object({
|
678
|
+
id: z3.string().nullish(),
|
679
|
+
type: z3.literal("function"),
|
680
|
+
function: z3.object({
|
681
|
+
name: z3.string(),
|
682
|
+
arguments: z3.string()
|
645
683
|
})
|
646
684
|
})
|
647
685
|
).nullish()
|
648
686
|
}),
|
649
|
-
finish_reason:
|
687
|
+
finish_reason: z3.string().nullish()
|
650
688
|
})
|
651
689
|
),
|
652
690
|
usage: openaiCompatibleTokenUsageSchema
|
653
691
|
});
|
654
|
-
var createOpenAICompatibleChatChunkSchema = (errorSchema) =>
|
655
|
-
|
656
|
-
id:
|
657
|
-
created:
|
658
|
-
model:
|
659
|
-
choices:
|
660
|
-
|
661
|
-
delta:
|
662
|
-
role:
|
663
|
-
content:
|
664
|
-
reasoning_content:
|
665
|
-
tool_calls:
|
666
|
-
|
667
|
-
index:
|
668
|
-
id:
|
669
|
-
type:
|
670
|
-
function:
|
671
|
-
name:
|
672
|
-
arguments:
|
692
|
+
var createOpenAICompatibleChatChunkSchema = (errorSchema) => z3.union([
|
693
|
+
z3.object({
|
694
|
+
id: z3.string().nullish(),
|
695
|
+
created: z3.number().nullish(),
|
696
|
+
model: z3.string().nullish(),
|
697
|
+
choices: z3.array(
|
698
|
+
z3.object({
|
699
|
+
delta: z3.object({
|
700
|
+
role: z3.enum(["assistant"]).nullish(),
|
701
|
+
content: z3.string().nullish(),
|
702
|
+
reasoning_content: z3.string().nullish(),
|
703
|
+
tool_calls: z3.array(
|
704
|
+
z3.object({
|
705
|
+
index: z3.number(),
|
706
|
+
id: z3.string().nullish(),
|
707
|
+
type: z3.literal("function").nullish(),
|
708
|
+
function: z3.object({
|
709
|
+
name: z3.string().nullish(),
|
710
|
+
arguments: z3.string().nullish()
|
673
711
|
})
|
674
712
|
})
|
675
713
|
).nullish()
|
676
714
|
}).nullish(),
|
677
|
-
finish_reason:
|
715
|
+
finish_reason: z3.string().nullish()
|
678
716
|
})
|
679
717
|
),
|
680
718
|
usage: openaiCompatibleTokenUsageSchema
|
@@ -688,9 +726,10 @@ import {
|
|
688
726
|
createEventSourceResponseHandler as createEventSourceResponseHandler2,
|
689
727
|
createJsonErrorResponseHandler as createJsonErrorResponseHandler2,
|
690
728
|
createJsonResponseHandler as createJsonResponseHandler2,
|
729
|
+
parseProviderOptions as parseProviderOptions2,
|
691
730
|
postJsonToApi as postJsonToApi2
|
692
731
|
} from "@ai-sdk/provider-utils";
|
693
|
-
import { z as
|
732
|
+
import { z as z5 } from "zod";
|
694
733
|
|
695
734
|
// src/convert-to-openai-compatible-completion-prompt.ts
|
696
735
|
import {
|
@@ -774,15 +813,39 @@ ${user}:`]
|
|
774
813
|
};
|
775
814
|
}
|
776
815
|
|
816
|
+
// src/openai-compatible-completion-options.ts
|
817
|
+
import { z as z4 } from "zod";
|
818
|
+
var openaiCompatibleCompletionProviderOptions = z4.object({
|
819
|
+
/**
|
820
|
+
* Echo back the prompt in addition to the completion.
|
821
|
+
*/
|
822
|
+
echo: z4.boolean().optional(),
|
823
|
+
/**
|
824
|
+
* Modify the likelihood of specified tokens appearing in the completion.
|
825
|
+
*
|
826
|
+
* Accepts a JSON object that maps tokens (specified by their token ID in
|
827
|
+
* the GPT tokenizer) to an associated bias value from -100 to 100.
|
828
|
+
*/
|
829
|
+
logitBias: z4.record(z4.number(), z4.number()).optional(),
|
830
|
+
/**
|
831
|
+
* The suffix that comes after a completion of inserted text.
|
832
|
+
*/
|
833
|
+
suffix: z4.string().optional(),
|
834
|
+
/**
|
835
|
+
* A unique identifier representing your end-user, which can help providers to
|
836
|
+
* monitor and detect abuse.
|
837
|
+
*/
|
838
|
+
user: z4.string().optional()
|
839
|
+
});
|
840
|
+
|
777
841
|
// src/openai-compatible-completion-language-model.ts
|
778
842
|
var OpenAICompatibleCompletionLanguageModel = class {
|
779
843
|
// type inferred via constructor
|
780
|
-
constructor(modelId,
|
844
|
+
constructor(modelId, config) {
|
781
845
|
this.specificationVersion = "v2";
|
782
846
|
this.defaultObjectGenerationMode = void 0;
|
783
847
|
var _a;
|
784
848
|
this.modelId = modelId;
|
785
|
-
this.settings = settings;
|
786
849
|
this.config = config;
|
787
850
|
const errorStructure = (_a = config.errorStructure) != null ? _a : defaultOpenAICompatibleErrorStructure;
|
788
851
|
this.chunkSchema = createOpenAICompatibleCompletionChunkSchema(
|
@@ -812,7 +875,13 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
812
875
|
tools,
|
813
876
|
toolChoice
|
814
877
|
}) {
|
878
|
+
var _a;
|
815
879
|
const warnings = [];
|
880
|
+
const completionOptions = (_a = parseProviderOptions2({
|
881
|
+
provider: this.providerOptionsName,
|
882
|
+
providerOptions,
|
883
|
+
schema: openaiCompatibleCompletionProviderOptions
|
884
|
+
})) != null ? _a : {};
|
816
885
|
if (topK != null) {
|
817
886
|
warnings.push({ type: "unsupported-setting", setting: "topK" });
|
818
887
|
}
|
@@ -836,10 +905,10 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
836
905
|
// model id:
|
837
906
|
model: this.modelId,
|
838
907
|
// model specific settings:
|
839
|
-
echo:
|
840
|
-
logit_bias:
|
841
|
-
suffix:
|
842
|
-
user:
|
908
|
+
echo: completionOptions.echo,
|
909
|
+
logit_bias: completionOptions.logitBias,
|
910
|
+
suffix: completionOptions.suffix,
|
911
|
+
user: completionOptions.user,
|
843
912
|
// standardized settings:
|
844
913
|
max_tokens: maxOutputTokens,
|
845
914
|
temperature,
|
@@ -878,8 +947,12 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
878
947
|
fetch: this.config.fetch
|
879
948
|
});
|
880
949
|
const choice = response.choices[0];
|
950
|
+
const content = [];
|
951
|
+
if (choice.text != null && choice.text.length > 0) {
|
952
|
+
content.push({ type: "text", text: choice.text });
|
953
|
+
}
|
881
954
|
return {
|
882
|
-
|
955
|
+
content,
|
883
956
|
usage: {
|
884
957
|
inputTokens: (_b = (_a = response.usage) == null ? void 0 : _a.prompt_tokens) != null ? _b : void 0,
|
885
958
|
outputTokens: (_d = (_c = response.usage) == null ? void 0 : _c.completion_tokens) != null ? _d : void 0
|
@@ -923,6 +996,9 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
923
996
|
return {
|
924
997
|
stream: response.pipeThrough(
|
925
998
|
new TransformStream({
|
999
|
+
start(controller) {
|
1000
|
+
controller.enqueue({ type: "stream-start", warnings });
|
1001
|
+
},
|
926
1002
|
transform(chunk, controller) {
|
927
1003
|
var _a, _b;
|
928
1004
|
if (!chunk.success) {
|
@@ -955,8 +1031,8 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
955
1031
|
}
|
956
1032
|
if ((choice == null ? void 0 : choice.text) != null) {
|
957
1033
|
controller.enqueue({
|
958
|
-
type: "text
|
959
|
-
|
1034
|
+
type: "text",
|
1035
|
+
text: choice.text
|
960
1036
|
});
|
961
1037
|
}
|
962
1038
|
},
|
@@ -970,41 +1046,40 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
970
1046
|
})
|
971
1047
|
),
|
972
1048
|
request: { body },
|
973
|
-
response: { headers: responseHeaders }
|
974
|
-
warnings
|
1049
|
+
response: { headers: responseHeaders }
|
975
1050
|
};
|
976
1051
|
}
|
977
1052
|
};
|
978
|
-
var openaiCompatibleCompletionResponseSchema =
|
979
|
-
id:
|
980
|
-
created:
|
981
|
-
model:
|
982
|
-
choices:
|
983
|
-
|
984
|
-
text:
|
985
|
-
finish_reason:
|
1053
|
+
var openaiCompatibleCompletionResponseSchema = z5.object({
|
1054
|
+
id: z5.string().nullish(),
|
1055
|
+
created: z5.number().nullish(),
|
1056
|
+
model: z5.string().nullish(),
|
1057
|
+
choices: z5.array(
|
1058
|
+
z5.object({
|
1059
|
+
text: z5.string(),
|
1060
|
+
finish_reason: z5.string()
|
986
1061
|
})
|
987
1062
|
),
|
988
|
-
usage:
|
989
|
-
prompt_tokens:
|
990
|
-
completion_tokens:
|
1063
|
+
usage: z5.object({
|
1064
|
+
prompt_tokens: z5.number(),
|
1065
|
+
completion_tokens: z5.number()
|
991
1066
|
}).nullish()
|
992
1067
|
});
|
993
|
-
var createOpenAICompatibleCompletionChunkSchema = (errorSchema) =>
|
994
|
-
|
995
|
-
id:
|
996
|
-
created:
|
997
|
-
model:
|
998
|
-
choices:
|
999
|
-
|
1000
|
-
text:
|
1001
|
-
finish_reason:
|
1002
|
-
index:
|
1068
|
+
var createOpenAICompatibleCompletionChunkSchema = (errorSchema) => z5.union([
|
1069
|
+
z5.object({
|
1070
|
+
id: z5.string().nullish(),
|
1071
|
+
created: z5.number().nullish(),
|
1072
|
+
model: z5.string().nullish(),
|
1073
|
+
choices: z5.array(
|
1074
|
+
z5.object({
|
1075
|
+
text: z5.string(),
|
1076
|
+
finish_reason: z5.string().nullish(),
|
1077
|
+
index: z5.number()
|
1003
1078
|
})
|
1004
1079
|
),
|
1005
|
-
usage:
|
1006
|
-
prompt_tokens:
|
1007
|
-
completion_tokens:
|
1080
|
+
usage: z5.object({
|
1081
|
+
prompt_tokens: z5.number(),
|
1082
|
+
completion_tokens: z5.number()
|
1008
1083
|
}).nullish()
|
1009
1084
|
}),
|
1010
1085
|
errorSchema
|
@@ -1018,14 +1093,31 @@ import {
|
|
1018
1093
|
combineHeaders as combineHeaders3,
|
1019
1094
|
createJsonErrorResponseHandler as createJsonErrorResponseHandler3,
|
1020
1095
|
createJsonResponseHandler as createJsonResponseHandler3,
|
1096
|
+
parseProviderOptions as parseProviderOptions3,
|
1021
1097
|
postJsonToApi as postJsonToApi3
|
1022
1098
|
} from "@ai-sdk/provider-utils";
|
1023
|
-
import { z as
|
1099
|
+
import { z as z7 } from "zod";
|
1100
|
+
|
1101
|
+
// src/openai-compatible-embedding-options.ts
|
1102
|
+
import { z as z6 } from "zod";
|
1103
|
+
var openaiCompatibleEmbeddingProviderOptions = z6.object({
|
1104
|
+
/**
|
1105
|
+
* The number of dimensions the resulting output embeddings should have.
|
1106
|
+
* Only supported in text-embedding-3 and later models.
|
1107
|
+
*/
|
1108
|
+
dimensions: z6.number().optional(),
|
1109
|
+
/**
|
1110
|
+
* A unique identifier representing your end-user, which can help providers to
|
1111
|
+
* monitor and detect abuse.
|
1112
|
+
*/
|
1113
|
+
user: z6.string().optional()
|
1114
|
+
});
|
1115
|
+
|
1116
|
+
// src/openai-compatible-embedding-model.ts
|
1024
1117
|
var OpenAICompatibleEmbeddingModel = class {
|
1025
|
-
constructor(modelId,
|
1026
|
-
this.specificationVersion = "
|
1118
|
+
constructor(modelId, config) {
|
1119
|
+
this.specificationVersion = "v2";
|
1027
1120
|
this.modelId = modelId;
|
1028
|
-
this.settings = settings;
|
1029
1121
|
this.config = config;
|
1030
1122
|
}
|
1031
1123
|
get provider() {
|
@@ -1039,12 +1131,28 @@ var OpenAICompatibleEmbeddingModel = class {
|
|
1039
1131
|
var _a;
|
1040
1132
|
return (_a = this.config.supportsParallelCalls) != null ? _a : true;
|
1041
1133
|
}
|
1134
|
+
get providerOptionsName() {
|
1135
|
+
return this.config.provider.split(".")[0].trim();
|
1136
|
+
}
|
1042
1137
|
async doEmbed({
|
1043
1138
|
values,
|
1044
1139
|
headers,
|
1045
|
-
abortSignal
|
1140
|
+
abortSignal,
|
1141
|
+
providerOptions
|
1046
1142
|
}) {
|
1047
|
-
var _a;
|
1143
|
+
var _a, _b, _c;
|
1144
|
+
const compatibleOptions = Object.assign(
|
1145
|
+
(_a = parseProviderOptions3({
|
1146
|
+
provider: "openai-compatible",
|
1147
|
+
providerOptions,
|
1148
|
+
schema: openaiCompatibleEmbeddingProviderOptions
|
1149
|
+
})) != null ? _a : {},
|
1150
|
+
(_b = parseProviderOptions3({
|
1151
|
+
provider: this.providerOptionsName,
|
1152
|
+
providerOptions,
|
1153
|
+
schema: openaiCompatibleEmbeddingProviderOptions
|
1154
|
+
})) != null ? _b : {}
|
1155
|
+
);
|
1048
1156
|
if (values.length > this.maxEmbeddingsPerCall) {
|
1049
1157
|
throw new TooManyEmbeddingValuesForCallError({
|
1050
1158
|
provider: this.provider,
|
@@ -1053,7 +1161,11 @@ var OpenAICompatibleEmbeddingModel = class {
|
|
1053
1161
|
values
|
1054
1162
|
});
|
1055
1163
|
}
|
1056
|
-
const {
|
1164
|
+
const {
|
1165
|
+
responseHeaders,
|
1166
|
+
value: response,
|
1167
|
+
rawValue
|
1168
|
+
} = await postJsonToApi3({
|
1057
1169
|
url: this.config.url({
|
1058
1170
|
path: "/embeddings",
|
1059
1171
|
modelId: this.modelId
|
@@ -1063,11 +1175,11 @@ var OpenAICompatibleEmbeddingModel = class {
|
|
1063
1175
|
model: this.modelId,
|
1064
1176
|
input: values,
|
1065
1177
|
encoding_format: "float",
|
1066
|
-
dimensions:
|
1067
|
-
user:
|
1178
|
+
dimensions: compatibleOptions.dimensions,
|
1179
|
+
user: compatibleOptions.user
|
1068
1180
|
},
|
1069
1181
|
failedResponseHandler: createJsonErrorResponseHandler3(
|
1070
|
-
(
|
1182
|
+
(_c = this.config.errorStructure) != null ? _c : defaultOpenAICompatibleErrorStructure
|
1071
1183
|
),
|
1072
1184
|
successfulResponseHandler: createJsonResponseHandler3(
|
1073
1185
|
openaiTextEmbeddingResponseSchema
|
@@ -1078,13 +1190,13 @@ var OpenAICompatibleEmbeddingModel = class {
|
|
1078
1190
|
return {
|
1079
1191
|
embeddings: response.data.map((item) => item.embedding),
|
1080
1192
|
usage: response.usage ? { tokens: response.usage.prompt_tokens } : void 0,
|
1081
|
-
|
1193
|
+
response: { headers: responseHeaders, body: rawValue }
|
1082
1194
|
};
|
1083
1195
|
}
|
1084
1196
|
};
|
1085
|
-
var openaiTextEmbeddingResponseSchema =
|
1086
|
-
data:
|
1087
|
-
usage:
|
1197
|
+
var openaiTextEmbeddingResponseSchema = z7.object({
|
1198
|
+
data: z7.array(z7.object({ embedding: z7.array(z7.number()) })),
|
1199
|
+
usage: z7.object({ prompt_tokens: z7.number() }).nullish()
|
1088
1200
|
});
|
1089
1201
|
|
1090
1202
|
// src/openai-compatible-image-model.ts
|
@@ -1094,7 +1206,7 @@ import {
|
|
1094
1206
|
createJsonResponseHandler as createJsonResponseHandler4,
|
1095
1207
|
postJsonToApi as postJsonToApi4
|
1096
1208
|
} from "@ai-sdk/provider-utils";
|
1097
|
-
import { z as
|
1209
|
+
import { z as z8 } from "zod";
|
1098
1210
|
var OpenAICompatibleImageModel = class {
|
1099
1211
|
constructor(modelId, settings, config) {
|
1100
1212
|
this.modelId = modelId;
|
@@ -1167,8 +1279,8 @@ var OpenAICompatibleImageModel = class {
|
|
1167
1279
|
};
|
1168
1280
|
}
|
1169
1281
|
};
|
1170
|
-
var openaiCompatibleImageResponseSchema =
|
1171
|
-
data:
|
1282
|
+
var openaiCompatibleImageResponseSchema = z8.object({
|
1283
|
+
data: z8.array(z8.object({ b64_json: z8.string() }))
|
1172
1284
|
});
|
1173
1285
|
|
1174
1286
|
// src/openai-compatible-provider.ts
|
@@ -1192,27 +1304,24 @@ function createOpenAICompatible(options) {
|
|
1192
1304
|
headers: getHeaders,
|
1193
1305
|
fetch: options.fetch
|
1194
1306
|
});
|
1195
|
-
const createLanguageModel = (modelId
|
1196
|
-
const createChatModel = (modelId
|
1307
|
+
const createLanguageModel = (modelId) => createChatModel(modelId);
|
1308
|
+
const createChatModel = (modelId) => new OpenAICompatibleChatLanguageModel(modelId, {
|
1197
1309
|
...getCommonModelConfig("chat"),
|
1198
1310
|
defaultObjectGenerationMode: "tool"
|
1199
1311
|
});
|
1200
|
-
const createCompletionModel = (modelId
|
1312
|
+
const createCompletionModel = (modelId) => new OpenAICompatibleCompletionLanguageModel(
|
1201
1313
|
modelId,
|
1202
|
-
settings,
|
1203
1314
|
getCommonModelConfig("completion")
|
1204
1315
|
);
|
1205
|
-
const createEmbeddingModel = (modelId
|
1206
|
-
|
1207
|
-
|
1208
|
-
getCommonModelConfig("embedding")
|
1209
|
-
);
|
1316
|
+
const createEmbeddingModel = (modelId) => new OpenAICompatibleEmbeddingModel(modelId, {
|
1317
|
+
...getCommonModelConfig("embedding")
|
1318
|
+
});
|
1210
1319
|
const createImageModel = (modelId, settings = {}) => new OpenAICompatibleImageModel(
|
1211
1320
|
modelId,
|
1212
1321
|
settings,
|
1213
1322
|
getCommonModelConfig("image")
|
1214
1323
|
);
|
1215
|
-
const provider = (modelId
|
1324
|
+
const provider = (modelId) => createLanguageModel(modelId);
|
1216
1325
|
provider.languageModel = createLanguageModel;
|
1217
1326
|
provider.chatModel = createChatModel;
|
1218
1327
|
provider.completionModel = createCompletionModel;
|