@ai-sdk/openai-compatible 0.0.6 → 0.0.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 +12 -0
- package/dist/index.d.mts +22 -1
- package/dist/index.d.ts +22 -1
- package/dist/index.js +109 -58
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +87 -33
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
@@ -1,5 +1,17 @@
|
|
1
1
|
# @ai-sdk/openai-compatible
|
2
2
|
|
3
|
+
## 0.0.8
|
4
|
+
|
5
|
+
### Patch Changes
|
6
|
+
|
7
|
+
- 6faab13: feat (provider/openai-compatible): simulated streaming setting
|
8
|
+
|
9
|
+
## 0.0.7
|
10
|
+
|
11
|
+
### Patch Changes
|
12
|
+
|
13
|
+
- ad2bf11: feat (provider/fireworks): Add Fireworks provider.
|
14
|
+
|
3
15
|
## 0.0.6
|
4
16
|
|
5
17
|
### Patch Changes
|
package/dist/index.d.mts
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
import { ProviderV1, LanguageModelV1, EmbeddingModelV1, LanguageModelV1ObjectGenerationMode } from '@ai-sdk/provider';
|
2
2
|
import { FetchFunction } from '@ai-sdk/provider-utils';
|
3
|
+
import { ZodSchema } from 'zod';
|
3
4
|
|
4
5
|
type OpenAICompatibleChatModelId = string;
|
5
6
|
interface OpenAICompatibleChatSettings {
|
@@ -8,6 +9,13 @@ interface OpenAICompatibleChatSettings {
|
|
8
9
|
monitor and detect abuse.
|
9
10
|
*/
|
10
11
|
user?: string;
|
12
|
+
/**
|
13
|
+
Simulates streaming by using a normal generate call and returning it as a stream.
|
14
|
+
Enable this if the model that you are using does not support streaming.
|
15
|
+
|
16
|
+
Defaults to `false`.
|
17
|
+
*/
|
18
|
+
simulateStreaming?: boolean;
|
11
19
|
}
|
12
20
|
|
13
21
|
type OpenAICompatibleCompletionModelId = string;
|
@@ -87,6 +95,12 @@ Create an OpenAICompatible provider instance.
|
|
87
95
|
*/
|
88
96
|
declare function createOpenAICompatible<CHAT_MODEL_IDS extends string, COMPLETION_MODEL_IDS extends string, EMBEDDING_MODEL_IDS extends string>(options: OpenAICompatibleProviderSettings): OpenAICompatibleProvider<CHAT_MODEL_IDS, COMPLETION_MODEL_IDS, EMBEDDING_MODEL_IDS>;
|
89
97
|
|
98
|
+
type ProviderErrorStructure<T> = {
|
99
|
+
errorSchema: ZodSchema<T>;
|
100
|
+
errorToMessage: (error: T) => string;
|
101
|
+
isRetryable?: (response: Response, error?: T) => boolean;
|
102
|
+
};
|
103
|
+
|
90
104
|
type OpenAICompatibleChatConfig = {
|
91
105
|
provider: string;
|
92
106
|
headers: () => Record<string, string | undefined>;
|
@@ -95,6 +109,7 @@ type OpenAICompatibleChatConfig = {
|
|
95
109
|
path: string;
|
96
110
|
}) => string;
|
97
111
|
fetch?: FetchFunction;
|
112
|
+
errorStructure?: ProviderErrorStructure<any>;
|
98
113
|
/**
|
99
114
|
Default object generation mode that should be used with this model when
|
100
115
|
no mode is specified. Should be the mode with the best results for this
|
@@ -112,6 +127,8 @@ declare class OpenAICompatibleChatLanguageModel implements LanguageModelV1 {
|
|
112
127
|
readonly modelId: OpenAICompatibleChatModelId;
|
113
128
|
readonly settings: OpenAICompatibleChatSettings;
|
114
129
|
private readonly config;
|
130
|
+
private readonly failedResponseHandler;
|
131
|
+
private readonly chunkSchema;
|
115
132
|
constructor(modelId: OpenAICompatibleChatModelId, settings: OpenAICompatibleChatSettings, config: OpenAICompatibleChatConfig);
|
116
133
|
get defaultObjectGenerationMode(): 'json' | 'tool' | undefined;
|
117
134
|
get provider(): string;
|
@@ -128,6 +145,7 @@ type OpenAICompatibleCompletionConfig = {
|
|
128
145
|
path: string;
|
129
146
|
}) => string;
|
130
147
|
fetch?: FetchFunction;
|
148
|
+
errorStructure?: ProviderErrorStructure<any>;
|
131
149
|
};
|
132
150
|
declare class OpenAICompatibleCompletionLanguageModel implements LanguageModelV1 {
|
133
151
|
readonly specificationVersion = "v1";
|
@@ -135,6 +153,8 @@ declare class OpenAICompatibleCompletionLanguageModel implements LanguageModelV1
|
|
135
153
|
readonly modelId: OpenAICompatibleCompletionModelId;
|
136
154
|
readonly settings: OpenAICompatibleCompletionSettings;
|
137
155
|
private readonly config;
|
156
|
+
private readonly failedResponseHandler;
|
157
|
+
private readonly chunkSchema;
|
138
158
|
constructor(modelId: OpenAICompatibleCompletionModelId, settings: OpenAICompatibleCompletionSettings, config: OpenAICompatibleCompletionConfig);
|
139
159
|
get provider(): string;
|
140
160
|
private getArgs;
|
@@ -158,6 +178,7 @@ type OpenAICompatibleEmbeddingConfig = {
|
|
158
178
|
}) => string;
|
159
179
|
headers: () => Record<string, string | undefined>;
|
160
180
|
fetch?: FetchFunction;
|
181
|
+
errorStructure?: ProviderErrorStructure<any>;
|
161
182
|
};
|
162
183
|
declare class OpenAICompatibleEmbeddingModel implements EmbeddingModelV1<string> {
|
163
184
|
readonly specificationVersion = "v1";
|
@@ -171,4 +192,4 @@ declare class OpenAICompatibleEmbeddingModel implements EmbeddingModelV1<string>
|
|
171
192
|
doEmbed({ values, headers, abortSignal, }: Parameters<EmbeddingModelV1<string>['doEmbed']>[0]): Promise<Awaited<ReturnType<EmbeddingModelV1<string>['doEmbed']>>>;
|
172
193
|
}
|
173
194
|
|
174
|
-
export { OpenAICompatibleChatLanguageModel, type OpenAICompatibleChatSettings, OpenAICompatibleCompletionLanguageModel, type OpenAICompatibleCompletionSettings, OpenAICompatibleEmbeddingModel, type OpenAICompatibleEmbeddingSettings, type OpenAICompatibleProvider, type OpenAICompatibleProviderSettings, createOpenAICompatible };
|
195
|
+
export { OpenAICompatibleChatLanguageModel, type OpenAICompatibleChatSettings, OpenAICompatibleCompletionLanguageModel, type OpenAICompatibleCompletionSettings, OpenAICompatibleEmbeddingModel, type OpenAICompatibleEmbeddingSettings, type OpenAICompatibleProvider, type OpenAICompatibleProviderSettings, type ProviderErrorStructure, createOpenAICompatible };
|
package/dist/index.d.ts
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
import { ProviderV1, LanguageModelV1, EmbeddingModelV1, LanguageModelV1ObjectGenerationMode } from '@ai-sdk/provider';
|
2
2
|
import { FetchFunction } from '@ai-sdk/provider-utils';
|
3
|
+
import { ZodSchema } from 'zod';
|
3
4
|
|
4
5
|
type OpenAICompatibleChatModelId = string;
|
5
6
|
interface OpenAICompatibleChatSettings {
|
@@ -8,6 +9,13 @@ interface OpenAICompatibleChatSettings {
|
|
8
9
|
monitor and detect abuse.
|
9
10
|
*/
|
10
11
|
user?: string;
|
12
|
+
/**
|
13
|
+
Simulates streaming by using a normal generate call and returning it as a stream.
|
14
|
+
Enable this if the model that you are using does not support streaming.
|
15
|
+
|
16
|
+
Defaults to `false`.
|
17
|
+
*/
|
18
|
+
simulateStreaming?: boolean;
|
11
19
|
}
|
12
20
|
|
13
21
|
type OpenAICompatibleCompletionModelId = string;
|
@@ -87,6 +95,12 @@ Create an OpenAICompatible provider instance.
|
|
87
95
|
*/
|
88
96
|
declare function createOpenAICompatible<CHAT_MODEL_IDS extends string, COMPLETION_MODEL_IDS extends string, EMBEDDING_MODEL_IDS extends string>(options: OpenAICompatibleProviderSettings): OpenAICompatibleProvider<CHAT_MODEL_IDS, COMPLETION_MODEL_IDS, EMBEDDING_MODEL_IDS>;
|
89
97
|
|
98
|
+
type ProviderErrorStructure<T> = {
|
99
|
+
errorSchema: ZodSchema<T>;
|
100
|
+
errorToMessage: (error: T) => string;
|
101
|
+
isRetryable?: (response: Response, error?: T) => boolean;
|
102
|
+
};
|
103
|
+
|
90
104
|
type OpenAICompatibleChatConfig = {
|
91
105
|
provider: string;
|
92
106
|
headers: () => Record<string, string | undefined>;
|
@@ -95,6 +109,7 @@ type OpenAICompatibleChatConfig = {
|
|
95
109
|
path: string;
|
96
110
|
}) => string;
|
97
111
|
fetch?: FetchFunction;
|
112
|
+
errorStructure?: ProviderErrorStructure<any>;
|
98
113
|
/**
|
99
114
|
Default object generation mode that should be used with this model when
|
100
115
|
no mode is specified. Should be the mode with the best results for this
|
@@ -112,6 +127,8 @@ declare class OpenAICompatibleChatLanguageModel implements LanguageModelV1 {
|
|
112
127
|
readonly modelId: OpenAICompatibleChatModelId;
|
113
128
|
readonly settings: OpenAICompatibleChatSettings;
|
114
129
|
private readonly config;
|
130
|
+
private readonly failedResponseHandler;
|
131
|
+
private readonly chunkSchema;
|
115
132
|
constructor(modelId: OpenAICompatibleChatModelId, settings: OpenAICompatibleChatSettings, config: OpenAICompatibleChatConfig);
|
116
133
|
get defaultObjectGenerationMode(): 'json' | 'tool' | undefined;
|
117
134
|
get provider(): string;
|
@@ -128,6 +145,7 @@ type OpenAICompatibleCompletionConfig = {
|
|
128
145
|
path: string;
|
129
146
|
}) => string;
|
130
147
|
fetch?: FetchFunction;
|
148
|
+
errorStructure?: ProviderErrorStructure<any>;
|
131
149
|
};
|
132
150
|
declare class OpenAICompatibleCompletionLanguageModel implements LanguageModelV1 {
|
133
151
|
readonly specificationVersion = "v1";
|
@@ -135,6 +153,8 @@ declare class OpenAICompatibleCompletionLanguageModel implements LanguageModelV1
|
|
135
153
|
readonly modelId: OpenAICompatibleCompletionModelId;
|
136
154
|
readonly settings: OpenAICompatibleCompletionSettings;
|
137
155
|
private readonly config;
|
156
|
+
private readonly failedResponseHandler;
|
157
|
+
private readonly chunkSchema;
|
138
158
|
constructor(modelId: OpenAICompatibleCompletionModelId, settings: OpenAICompatibleCompletionSettings, config: OpenAICompatibleCompletionConfig);
|
139
159
|
get provider(): string;
|
140
160
|
private getArgs;
|
@@ -158,6 +178,7 @@ type OpenAICompatibleEmbeddingConfig = {
|
|
158
178
|
}) => string;
|
159
179
|
headers: () => Record<string, string | undefined>;
|
160
180
|
fetch?: FetchFunction;
|
181
|
+
errorStructure?: ProviderErrorStructure<any>;
|
161
182
|
};
|
162
183
|
declare class OpenAICompatibleEmbeddingModel implements EmbeddingModelV1<string> {
|
163
184
|
readonly specificationVersion = "v1";
|
@@ -171,4 +192,4 @@ declare class OpenAICompatibleEmbeddingModel implements EmbeddingModelV1<string>
|
|
171
192
|
doEmbed({ values, headers, abortSignal, }: Parameters<EmbeddingModelV1<string>['doEmbed']>[0]): Promise<Awaited<ReturnType<EmbeddingModelV1<string>['doEmbed']>>>;
|
172
193
|
}
|
173
194
|
|
174
|
-
export { OpenAICompatibleChatLanguageModel, type OpenAICompatibleChatSettings, OpenAICompatibleCompletionLanguageModel, type OpenAICompatibleCompletionSettings, OpenAICompatibleEmbeddingModel, type OpenAICompatibleEmbeddingSettings, type OpenAICompatibleProvider, type OpenAICompatibleProviderSettings, createOpenAICompatible };
|
195
|
+
export { OpenAICompatibleChatLanguageModel, type OpenAICompatibleChatSettings, OpenAICompatibleCompletionLanguageModel, type OpenAICompatibleCompletionSettings, OpenAICompatibleEmbeddingModel, type OpenAICompatibleEmbeddingSettings, type OpenAICompatibleProvider, type OpenAICompatibleProviderSettings, type ProviderErrorStructure, createOpenAICompatible };
|
package/dist/index.js
CHANGED
@@ -28,11 +28,11 @@ __export(src_exports, {
|
|
28
28
|
module.exports = __toCommonJS(src_exports);
|
29
29
|
|
30
30
|
// src/openai-compatible-provider.ts
|
31
|
-
var
|
31
|
+
var import_provider_utils5 = require("@ai-sdk/provider-utils");
|
32
32
|
|
33
33
|
// src/openai-compatible-chat-language-model.ts
|
34
34
|
var import_provider3 = require("@ai-sdk/provider");
|
35
|
-
var
|
35
|
+
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
36
36
|
var import_zod2 = require("zod");
|
37
37
|
|
38
38
|
// src/convert-to-openai-compatible-chat-messages.ts
|
@@ -142,9 +142,25 @@ function getResponseMetadata({
|
|
142
142
|
};
|
143
143
|
}
|
144
144
|
|
145
|
+
// src/map-openai-compatible-finish-reason.ts
|
146
|
+
function mapOpenAICompatibleFinishReason(finishReason) {
|
147
|
+
switch (finishReason) {
|
148
|
+
case "stop":
|
149
|
+
return "stop";
|
150
|
+
case "length":
|
151
|
+
return "length";
|
152
|
+
case "content_filter":
|
153
|
+
return "content-filter";
|
154
|
+
case "function_call":
|
155
|
+
case "tool_calls":
|
156
|
+
return "tool-calls";
|
157
|
+
default:
|
158
|
+
return "unknown";
|
159
|
+
}
|
160
|
+
}
|
161
|
+
|
145
162
|
// src/openai-compatible-error.ts
|
146
163
|
var import_zod = require("zod");
|
147
|
-
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
148
164
|
var openaiCompatibleErrorDataSchema = import_zod.z.object({
|
149
165
|
error: import_zod.z.object({
|
150
166
|
message: import_zod.z.string(),
|
@@ -156,10 +172,10 @@ var openaiCompatibleErrorDataSchema = import_zod.z.object({
|
|
156
172
|
code: import_zod.z.union([import_zod.z.string(), import_zod.z.number()]).nullish()
|
157
173
|
})
|
158
174
|
});
|
159
|
-
var
|
175
|
+
var defaultOpenAICompatibleErrorStructure = {
|
160
176
|
errorSchema: openaiCompatibleErrorDataSchema,
|
161
177
|
errorToMessage: (data) => data.error.message
|
162
|
-
}
|
178
|
+
};
|
163
179
|
|
164
180
|
// src/openai-compatible-prepare-tools.ts
|
165
181
|
var import_provider2 = require("@ai-sdk/provider");
|
@@ -219,32 +235,21 @@ function prepareTools({
|
|
219
235
|
}
|
220
236
|
}
|
221
237
|
|
222
|
-
// src/map-openai-compatible-finish-reason.ts
|
223
|
-
function mapOpenAICompatibleFinishReason(finishReason) {
|
224
|
-
switch (finishReason) {
|
225
|
-
case "stop":
|
226
|
-
return "stop";
|
227
|
-
case "length":
|
228
|
-
return "length";
|
229
|
-
case "content_filter":
|
230
|
-
return "content-filter";
|
231
|
-
case "function_call":
|
232
|
-
case "tool_calls":
|
233
|
-
return "tool-calls";
|
234
|
-
default:
|
235
|
-
return "unknown";
|
236
|
-
}
|
237
|
-
}
|
238
|
-
|
239
238
|
// src/openai-compatible-chat-language-model.ts
|
240
239
|
var OpenAICompatibleChatLanguageModel = class {
|
240
|
+
// type inferred via constructor
|
241
241
|
constructor(modelId, settings, config) {
|
242
242
|
this.specificationVersion = "v1";
|
243
|
-
var _a;
|
243
|
+
var _a, _b;
|
244
244
|
this.modelId = modelId;
|
245
245
|
this.settings = settings;
|
246
246
|
this.config = config;
|
247
|
-
|
247
|
+
const errorStructure = (_a = config.errorStructure) != null ? _a : defaultOpenAICompatibleErrorStructure;
|
248
|
+
this.chunkSchema = createOpenAICompatibleChatChunkSchema(
|
249
|
+
errorStructure.errorSchema
|
250
|
+
);
|
251
|
+
this.failedResponseHandler = (0, import_provider_utils2.createJsonErrorResponseHandler)(errorStructure);
|
252
|
+
this.supportsStructuredOutputs = (_b = config.supportsStructuredOutputs) != null ? _b : false;
|
248
253
|
}
|
249
254
|
get defaultObjectGenerationMode() {
|
250
255
|
return this.config.defaultObjectGenerationMode;
|
@@ -367,15 +372,15 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
367
372
|
var _a, _b, _c, _d, _e, _f;
|
368
373
|
const { args, warnings } = this.getArgs({ ...options });
|
369
374
|
const body = JSON.stringify(args);
|
370
|
-
const { responseHeaders, value: response } = await (0,
|
375
|
+
const { responseHeaders, value: response } = await (0, import_provider_utils2.postJsonToApi)({
|
371
376
|
url: this.config.url({
|
372
377
|
path: "/chat/completions",
|
373
378
|
modelId: this.modelId
|
374
379
|
}),
|
375
|
-
headers: (0,
|
380
|
+
headers: (0, import_provider_utils2.combineHeaders)(this.config.headers(), options.headers),
|
376
381
|
body: args,
|
377
|
-
failedResponseHandler:
|
378
|
-
successfulResponseHandler: (0,
|
382
|
+
failedResponseHandler: this.failedResponseHandler,
|
383
|
+
successfulResponseHandler: (0, import_provider_utils2.createJsonResponseHandler)(
|
379
384
|
OpenAICompatibleChatResponseSchema
|
380
385
|
),
|
381
386
|
abortSignal: options.abortSignal,
|
@@ -389,7 +394,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
389
394
|
var _a2;
|
390
395
|
return {
|
391
396
|
toolCallType: "function",
|
392
|
-
toolCallId: (_a2 = toolCall.id) != null ? _a2 : (0,
|
397
|
+
toolCallId: (_a2 = toolCall.id) != null ? _a2 : (0, import_provider_utils2.generateId)(),
|
393
398
|
toolName: toolCall.function.name,
|
394
399
|
args: toolCall.function.arguments
|
395
400
|
};
|
@@ -407,21 +412,57 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
407
412
|
};
|
408
413
|
}
|
409
414
|
async doStream(options) {
|
415
|
+
if (this.settings.simulateStreaming) {
|
416
|
+
const result = await this.doGenerate(options);
|
417
|
+
const simulatedStream = new ReadableStream({
|
418
|
+
start(controller) {
|
419
|
+
controller.enqueue({ type: "response-metadata", ...result.response });
|
420
|
+
if (result.text) {
|
421
|
+
controller.enqueue({
|
422
|
+
type: "text-delta",
|
423
|
+
textDelta: result.text
|
424
|
+
});
|
425
|
+
}
|
426
|
+
if (result.toolCalls) {
|
427
|
+
for (const toolCall of result.toolCalls) {
|
428
|
+
controller.enqueue({
|
429
|
+
type: "tool-call",
|
430
|
+
...toolCall
|
431
|
+
});
|
432
|
+
}
|
433
|
+
}
|
434
|
+
controller.enqueue({
|
435
|
+
type: "finish",
|
436
|
+
finishReason: result.finishReason,
|
437
|
+
usage: result.usage,
|
438
|
+
logprobs: result.logprobs,
|
439
|
+
providerMetadata: result.providerMetadata
|
440
|
+
});
|
441
|
+
controller.close();
|
442
|
+
}
|
443
|
+
});
|
444
|
+
return {
|
445
|
+
stream: simulatedStream,
|
446
|
+
rawCall: result.rawCall,
|
447
|
+
rawResponse: result.rawResponse,
|
448
|
+
warnings: result.warnings
|
449
|
+
};
|
450
|
+
}
|
410
451
|
const { args, warnings } = this.getArgs({ ...options });
|
411
452
|
const body = JSON.stringify({ ...args, stream: true });
|
412
|
-
const { responseHeaders, value: response } = await (0,
|
453
|
+
const { responseHeaders, value: response } = await (0, import_provider_utils2.postJsonToApi)({
|
413
454
|
url: this.config.url({
|
414
455
|
path: "/chat/completions",
|
415
456
|
modelId: this.modelId
|
416
457
|
}),
|
417
|
-
headers: (0,
|
458
|
+
headers: (0, import_provider_utils2.combineHeaders)(this.config.headers(), options.headers),
|
418
459
|
body: {
|
419
460
|
...args,
|
420
461
|
stream: true
|
421
462
|
},
|
422
|
-
failedResponseHandler:
|
423
|
-
successfulResponseHandler: (0,
|
424
|
-
|
463
|
+
failedResponseHandler: this.failedResponseHandler,
|
464
|
+
successfulResponseHandler: (0, import_provider_utils2.createEventSourceResponseHandler)(
|
465
|
+
this.chunkSchema
|
425
466
|
),
|
426
467
|
abortSignal: options.abortSignal,
|
427
468
|
fetch: this.config.fetch
|
@@ -522,11 +563,11 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
522
563
|
argsTextDelta: toolCall2.function.arguments
|
523
564
|
});
|
524
565
|
}
|
525
|
-
if ((0,
|
566
|
+
if ((0, import_provider_utils2.isParsableJson)(toolCall2.function.arguments)) {
|
526
567
|
controller.enqueue({
|
527
568
|
type: "tool-call",
|
528
569
|
toolCallType: "function",
|
529
|
-
toolCallId: (_g = toolCall2.id) != null ? _g : (0,
|
570
|
+
toolCallId: (_g = toolCall2.id) != null ? _g : (0, import_provider_utils2.generateId)(),
|
530
571
|
toolName: toolCall2.function.name,
|
531
572
|
args: toolCall2.function.arguments
|
532
573
|
});
|
@@ -549,11 +590,11 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
549
590
|
toolName: toolCall.function.name,
|
550
591
|
argsTextDelta: (_k = toolCallDelta.function.arguments) != null ? _k : ""
|
551
592
|
});
|
552
|
-
if (((_l = toolCall.function) == null ? void 0 : _l.name) != null && ((_m = toolCall.function) == null ? void 0 : _m.arguments) != null && (0,
|
593
|
+
if (((_l = toolCall.function) == null ? void 0 : _l.name) != null && ((_m = toolCall.function) == null ? void 0 : _m.arguments) != null && (0, import_provider_utils2.isParsableJson)(toolCall.function.arguments)) {
|
553
594
|
controller.enqueue({
|
554
595
|
type: "tool-call",
|
555
596
|
toolCallType: "function",
|
556
|
-
toolCallId: (_n = toolCall.id) != null ? _n : (0,
|
597
|
+
toolCallId: (_n = toolCall.id) != null ? _n : (0, import_provider_utils2.generateId)(),
|
557
598
|
toolName: toolCall.function.name,
|
558
599
|
args: toolCall.function.arguments
|
559
600
|
});
|
@@ -611,7 +652,7 @@ var OpenAICompatibleChatResponseSchema = import_zod2.z.object({
|
|
611
652
|
completion_tokens: import_zod2.z.number().nullish()
|
612
653
|
}).nullish()
|
613
654
|
});
|
614
|
-
var
|
655
|
+
var createOpenAICompatibleChatChunkSchema = (errorSchema) => import_zod2.z.union([
|
615
656
|
import_zod2.z.object({
|
616
657
|
id: import_zod2.z.string().nullish(),
|
617
658
|
created: import_zod2.z.number().nullish(),
|
@@ -641,12 +682,12 @@ var OpenAICompatibleChatChunkSchema = import_zod2.z.union([
|
|
641
682
|
completion_tokens: import_zod2.z.number().nullish()
|
642
683
|
}).nullish()
|
643
684
|
}),
|
644
|
-
|
685
|
+
errorSchema
|
645
686
|
]);
|
646
687
|
|
647
688
|
// src/openai-compatible-completion-language-model.ts
|
648
689
|
var import_provider5 = require("@ai-sdk/provider");
|
649
|
-
var
|
690
|
+
var import_provider_utils3 = require("@ai-sdk/provider-utils");
|
650
691
|
var import_zod3 = require("zod");
|
651
692
|
|
652
693
|
// src/convert-to-openai-compatible-completion-prompt.ts
|
@@ -735,12 +776,19 @@ ${user}:`]
|
|
735
776
|
|
736
777
|
// src/openai-compatible-completion-language-model.ts
|
737
778
|
var OpenAICompatibleCompletionLanguageModel = class {
|
779
|
+
// type inferred via constructor
|
738
780
|
constructor(modelId, settings, config) {
|
739
781
|
this.specificationVersion = "v1";
|
740
782
|
this.defaultObjectGenerationMode = void 0;
|
783
|
+
var _a;
|
741
784
|
this.modelId = modelId;
|
742
785
|
this.settings = settings;
|
743
786
|
this.config = config;
|
787
|
+
const errorStructure = (_a = config.errorStructure) != null ? _a : defaultOpenAICompatibleErrorStructure;
|
788
|
+
this.chunkSchema = createOpenAICompatibleCompletionChunkSchema(
|
789
|
+
errorStructure.errorSchema
|
790
|
+
);
|
791
|
+
this.failedResponseHandler = (0, import_provider_utils3.createJsonErrorResponseHandler)(errorStructure);
|
744
792
|
}
|
745
793
|
get provider() {
|
746
794
|
return this.config.provider;
|
@@ -830,15 +878,15 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
830
878
|
async doGenerate(options) {
|
831
879
|
var _a, _b, _c, _d;
|
832
880
|
const { args, warnings } = this.getArgs(options);
|
833
|
-
const { responseHeaders, value: response } = await (0,
|
881
|
+
const { responseHeaders, value: response } = await (0, import_provider_utils3.postJsonToApi)({
|
834
882
|
url: this.config.url({
|
835
883
|
path: "/completions",
|
836
884
|
modelId: this.modelId
|
837
885
|
}),
|
838
|
-
headers: (0,
|
886
|
+
headers: (0, import_provider_utils3.combineHeaders)(this.config.headers(), options.headers),
|
839
887
|
body: args,
|
840
|
-
failedResponseHandler:
|
841
|
-
successfulResponseHandler: (0,
|
888
|
+
failedResponseHandler: this.failedResponseHandler,
|
889
|
+
successfulResponseHandler: (0, import_provider_utils3.createJsonResponseHandler)(
|
842
890
|
openaiCompatibleCompletionResponseSchema
|
843
891
|
),
|
844
892
|
abortSignal: options.abortSignal,
|
@@ -866,16 +914,16 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
866
914
|
...args,
|
867
915
|
stream: true
|
868
916
|
};
|
869
|
-
const { responseHeaders, value: response } = await (0,
|
917
|
+
const { responseHeaders, value: response } = await (0, import_provider_utils3.postJsonToApi)({
|
870
918
|
url: this.config.url({
|
871
919
|
path: "/completions",
|
872
920
|
modelId: this.modelId
|
873
921
|
}),
|
874
|
-
headers: (0,
|
922
|
+
headers: (0, import_provider_utils3.combineHeaders)(this.config.headers(), options.headers),
|
875
923
|
body,
|
876
|
-
failedResponseHandler:
|
877
|
-
successfulResponseHandler: (0,
|
878
|
-
|
924
|
+
failedResponseHandler: this.failedResponseHandler,
|
925
|
+
successfulResponseHandler: (0, import_provider_utils3.createEventSourceResponseHandler)(
|
926
|
+
this.chunkSchema
|
879
927
|
),
|
880
928
|
abortSignal: options.abortSignal,
|
881
929
|
fetch: this.config.fetch
|
@@ -959,7 +1007,7 @@ var openaiCompatibleCompletionResponseSchema = import_zod3.z.object({
|
|
959
1007
|
completion_tokens: import_zod3.z.number()
|
960
1008
|
}).nullish()
|
961
1009
|
});
|
962
|
-
var
|
1010
|
+
var createOpenAICompatibleCompletionChunkSchema = (errorSchema) => import_zod3.z.union([
|
963
1011
|
import_zod3.z.object({
|
964
1012
|
id: import_zod3.z.string().nullish(),
|
965
1013
|
created: import_zod3.z.number().nullish(),
|
@@ -976,12 +1024,12 @@ var openaiCompatibleCompletionChunkSchema = import_zod3.z.union([
|
|
976
1024
|
completion_tokens: import_zod3.z.number()
|
977
1025
|
}).nullish()
|
978
1026
|
}),
|
979
|
-
|
1027
|
+
errorSchema
|
980
1028
|
]);
|
981
1029
|
|
982
1030
|
// src/openai-compatible-embedding-model.ts
|
983
1031
|
var import_provider6 = require("@ai-sdk/provider");
|
984
|
-
var
|
1032
|
+
var import_provider_utils4 = require("@ai-sdk/provider-utils");
|
985
1033
|
var import_zod4 = require("zod");
|
986
1034
|
var OpenAICompatibleEmbeddingModel = class {
|
987
1035
|
constructor(modelId, settings, config) {
|
@@ -1006,6 +1054,7 @@ var OpenAICompatibleEmbeddingModel = class {
|
|
1006
1054
|
headers,
|
1007
1055
|
abortSignal
|
1008
1056
|
}) {
|
1057
|
+
var _a;
|
1009
1058
|
if (values.length > this.maxEmbeddingsPerCall) {
|
1010
1059
|
throw new import_provider6.TooManyEmbeddingValuesForCallError({
|
1011
1060
|
provider: this.provider,
|
@@ -1014,12 +1063,12 @@ var OpenAICompatibleEmbeddingModel = class {
|
|
1014
1063
|
values
|
1015
1064
|
});
|
1016
1065
|
}
|
1017
|
-
const { responseHeaders, value: response } = await (0,
|
1066
|
+
const { responseHeaders, value: response } = await (0, import_provider_utils4.postJsonToApi)({
|
1018
1067
|
url: this.config.url({
|
1019
1068
|
path: "/embeddings",
|
1020
1069
|
modelId: this.modelId
|
1021
1070
|
}),
|
1022
|
-
headers: (0,
|
1071
|
+
headers: (0, import_provider_utils4.combineHeaders)(this.config.headers(), headers),
|
1023
1072
|
body: {
|
1024
1073
|
model: this.modelId,
|
1025
1074
|
input: values,
|
@@ -1027,8 +1076,10 @@ var OpenAICompatibleEmbeddingModel = class {
|
|
1027
1076
|
dimensions: this.settings.dimensions,
|
1028
1077
|
user: this.settings.user
|
1029
1078
|
},
|
1030
|
-
failedResponseHandler:
|
1031
|
-
|
1079
|
+
failedResponseHandler: (0, import_provider_utils4.createJsonErrorResponseHandler)(
|
1080
|
+
(_a = this.config.errorStructure) != null ? _a : defaultOpenAICompatibleErrorStructure
|
1081
|
+
),
|
1082
|
+
successfulResponseHandler: (0, import_provider_utils4.createJsonResponseHandler)(
|
1032
1083
|
openaiTextEmbeddingResponseSchema
|
1033
1084
|
),
|
1034
1085
|
abortSignal,
|
@@ -1051,7 +1102,7 @@ function createOpenAICompatible(options) {
|
|
1051
1102
|
if (!options.baseURL) {
|
1052
1103
|
throw new Error("Base URL is required");
|
1053
1104
|
}
|
1054
|
-
const baseURL = (0,
|
1105
|
+
const baseURL = (0, import_provider_utils5.withoutTrailingSlash)(options.baseURL);
|
1055
1106
|
if (!options.name) {
|
1056
1107
|
throw new Error("Provider name is required");
|
1057
1108
|
}
|