@ai-sdk/openai-compatible 0.1.1 → 0.1.3
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 +16 -0
- package/dist/index.d.mts +134 -90
- package/dist/index.d.ts +134 -90
- package/dist/index.js +27 -18
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +29 -20
- package/dist/index.mjs.map +1 -1
- package/internal/dist/index.d.mts +45 -1
- package/internal/dist/index.d.ts +45 -1
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
@@ -1,5 +1,21 @@
|
|
1
1
|
# @ai-sdk/openai-compatible
|
2
2
|
|
3
|
+
## 0.1.3
|
4
|
+
|
5
|
+
### Patch Changes
|
6
|
+
|
7
|
+
- 361fd08: chore: update a few add'l processor references to extractor
|
8
|
+
|
9
|
+
## 0.1.2
|
10
|
+
|
11
|
+
### Patch Changes
|
12
|
+
|
13
|
+
- ed012d2: feat (provider): add metadata extraction mechanism to openai-compatible providers
|
14
|
+
- Updated dependencies [ed012d2]
|
15
|
+
- Updated dependencies [3a58a2e]
|
16
|
+
- @ai-sdk/provider-utils@2.1.2
|
17
|
+
- @ai-sdk/provider@1.0.6
|
18
|
+
|
3
19
|
## 0.1.1
|
4
20
|
|
5
21
|
### Patch Changes
|
package/dist/index.d.mts
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
import {
|
1
|
+
import { LanguageModelV1ProviderMetadata, LanguageModelV1, LanguageModelV1ObjectGenerationMode, EmbeddingModelV1, ProviderV1 } from '@ai-sdk/provider';
|
2
2
|
import { FetchFunction } from '@ai-sdk/provider-utils';
|
3
3
|
import { z, ZodSchema } from 'zod';
|
4
4
|
|
@@ -18,94 +18,6 @@ interface OpenAICompatibleChatSettings {
|
|
18
18
|
simulateStreaming?: boolean;
|
19
19
|
}
|
20
20
|
|
21
|
-
type OpenAICompatibleCompletionModelId = string;
|
22
|
-
interface OpenAICompatibleCompletionSettings {
|
23
|
-
/**
|
24
|
-
Echo back the prompt in addition to the completion.
|
25
|
-
*/
|
26
|
-
echo?: boolean;
|
27
|
-
/**
|
28
|
-
Modify the likelihood of specified tokens appearing in the completion.
|
29
|
-
|
30
|
-
Accepts a JSON object that maps tokens (specified by their token ID in
|
31
|
-
the GPT tokenizer) to an associated bias value from -100 to 100. You
|
32
|
-
can use this tokenizer tool to convert text to token IDs. Mathematically,
|
33
|
-
the bias is added to the logits generated by the model prior to sampling.
|
34
|
-
The exact effect will vary per model, but values between -1 and 1 should
|
35
|
-
decrease or increase likelihood of selection; values like -100 or 100
|
36
|
-
should result in a ban or exclusive selection of the relevant token.
|
37
|
-
|
38
|
-
As an example, you can pass {"50256": -100} to prevent the <|endoftext|>
|
39
|
-
token from being generated.
|
40
|
-
*/
|
41
|
-
logitBias?: Record<number, number>;
|
42
|
-
/**
|
43
|
-
The suffix that comes after a completion of inserted text.
|
44
|
-
*/
|
45
|
-
suffix?: string;
|
46
|
-
/**
|
47
|
-
A unique identifier representing your end-user, which can help OpenAI to
|
48
|
-
monitor and detect abuse. Learn more.
|
49
|
-
*/
|
50
|
-
user?: string;
|
51
|
-
}
|
52
|
-
|
53
|
-
type OpenAICompatibleEmbeddingModelId = string;
|
54
|
-
interface OpenAICompatibleEmbeddingSettings {
|
55
|
-
/**
|
56
|
-
The number of dimensions the resulting output embeddings should have.
|
57
|
-
Only supported in text-embedding-3 and later models.
|
58
|
-
*/
|
59
|
-
dimensions?: number;
|
60
|
-
/**
|
61
|
-
A unique identifier representing your end-user, which can help OpenAI to
|
62
|
-
monitor and detect abuse. Learn more.
|
63
|
-
*/
|
64
|
-
user?: string;
|
65
|
-
}
|
66
|
-
|
67
|
-
interface OpenAICompatibleProvider<CHAT_MODEL_IDS extends string = string, COMPLETION_MODEL_IDS extends string = string, EMBEDDING_MODEL_IDS extends string = string> extends ProviderV1 {
|
68
|
-
(modelId: CHAT_MODEL_IDS, settings?: OpenAICompatibleChatSettings): LanguageModelV1;
|
69
|
-
languageModel(modelId: CHAT_MODEL_IDS, settings?: OpenAICompatibleChatSettings): LanguageModelV1;
|
70
|
-
chatModel(modelId: CHAT_MODEL_IDS, settings?: OpenAICompatibleChatSettings): LanguageModelV1;
|
71
|
-
completionModel(modelId: COMPLETION_MODEL_IDS, settings?: OpenAICompatibleCompletionSettings): LanguageModelV1;
|
72
|
-
textEmbeddingModel(modelId: EMBEDDING_MODEL_IDS, settings?: OpenAICompatibleEmbeddingSettings): EmbeddingModelV1<string>;
|
73
|
-
}
|
74
|
-
interface OpenAICompatibleProviderSettings {
|
75
|
-
/**
|
76
|
-
Base URL for the API calls.
|
77
|
-
*/
|
78
|
-
baseURL?: string;
|
79
|
-
/**
|
80
|
-
API key for authenticating requests. If specified, adds an `Authorization`
|
81
|
-
header to request headers with the value `Bearer <apiKey>`. This will be added
|
82
|
-
before any headers potentially specified in the `headers` option.
|
83
|
-
*/
|
84
|
-
apiKey?: string;
|
85
|
-
/**
|
86
|
-
Optional custom headers to include in requests. These will be added to request headers
|
87
|
-
after any headers potentially added by use of the `apiKey` option.
|
88
|
-
*/
|
89
|
-
headers?: Record<string, string>;
|
90
|
-
/**
|
91
|
-
Optional custom url query parameters to include in request urls.
|
92
|
-
*/
|
93
|
-
queryParams?: Record<string, string>;
|
94
|
-
/**
|
95
|
-
Custom fetch implementation. You can use it as a middleware to intercept requests,
|
96
|
-
or to provide a custom fetch implementation for e.g. testing.
|
97
|
-
*/
|
98
|
-
fetch?: FetchFunction;
|
99
|
-
/**
|
100
|
-
Provider name.
|
101
|
-
*/
|
102
|
-
name?: string;
|
103
|
-
}
|
104
|
-
/**
|
105
|
-
Create an OpenAICompatible provider instance.
|
106
|
-
*/
|
107
|
-
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>;
|
108
|
-
|
109
21
|
declare const openaiCompatibleErrorDataSchema: z.ZodObject<{
|
110
22
|
error: z.ZodObject<{
|
111
23
|
message: z.ZodString;
|
@@ -145,6 +57,49 @@ type ProviderErrorStructure<T> = {
|
|
145
57
|
isRetryable?: (response: Response, error?: T) => boolean;
|
146
58
|
};
|
147
59
|
|
60
|
+
/**
|
61
|
+
Extracts provider-specific metadata from API responses.
|
62
|
+
Used to standardize metadata handling across different LLM providers while allowing
|
63
|
+
provider-specific metadata to be captured.
|
64
|
+
*/
|
65
|
+
type MetadataExtractor = {
|
66
|
+
/**
|
67
|
+
* Extracts provider metadata from a complete, non-streaming response.
|
68
|
+
*
|
69
|
+
* @param parsedBody - The parsed response JSON body from the provider's API.
|
70
|
+
*
|
71
|
+
* @returns Provider-specific metadata or undefined if no metadata is available.
|
72
|
+
* The metadata should be under a key indicating the provider id.
|
73
|
+
*/
|
74
|
+
extractMetadata: ({ parsedBody, }: {
|
75
|
+
parsedBody: unknown;
|
76
|
+
}) => LanguageModelV1ProviderMetadata | undefined;
|
77
|
+
/**
|
78
|
+
* Creates an extractor for handling streaming responses. The returned object provides
|
79
|
+
* methods to process individual chunks and build the final metadata from the accumulated
|
80
|
+
* stream data.
|
81
|
+
*
|
82
|
+
* @returns An object with methods to process chunks and build metadata from a stream
|
83
|
+
*/
|
84
|
+
createStreamExtractor: () => {
|
85
|
+
/**
|
86
|
+
* Process an individual chunk from the stream. Called for each chunk in the response stream
|
87
|
+
* to accumulate metadata throughout the streaming process.
|
88
|
+
*
|
89
|
+
* @param parsedChunk - The parsed JSON response chunk from the provider's API
|
90
|
+
*/
|
91
|
+
processChunk(parsedChunk: unknown): void;
|
92
|
+
/**
|
93
|
+
* Builds the metadata object after all chunks have been processed.
|
94
|
+
* Called at the end of the stream to generate the complete provider metadata.
|
95
|
+
*
|
96
|
+
* @returns Provider-specific metadata or undefined if no metadata is available.
|
97
|
+
* The metadata should be under a key indicating the provider id.
|
98
|
+
*/
|
99
|
+
buildMetadata(): LanguageModelV1ProviderMetadata | undefined;
|
100
|
+
};
|
101
|
+
};
|
102
|
+
|
148
103
|
type OpenAICompatibleChatConfig = {
|
149
104
|
provider: string;
|
150
105
|
headers: () => Record<string, string | undefined>;
|
@@ -154,6 +109,7 @@ type OpenAICompatibleChatConfig = {
|
|
154
109
|
}) => string;
|
155
110
|
fetch?: FetchFunction;
|
156
111
|
errorStructure?: ProviderErrorStructure<any>;
|
112
|
+
metadataExtractor?: MetadataExtractor;
|
157
113
|
/**
|
158
114
|
Default object generation mode that should be used with this model when
|
159
115
|
no mode is specified. Should be the mode with the best results for this
|
@@ -181,6 +137,38 @@ declare class OpenAICompatibleChatLanguageModel implements LanguageModelV1 {
|
|
181
137
|
doStream(options: Parameters<LanguageModelV1['doStream']>[0]): Promise<Awaited<ReturnType<LanguageModelV1['doStream']>>>;
|
182
138
|
}
|
183
139
|
|
140
|
+
type OpenAICompatibleCompletionModelId = string;
|
141
|
+
interface OpenAICompatibleCompletionSettings {
|
142
|
+
/**
|
143
|
+
Echo back the prompt in addition to the completion.
|
144
|
+
*/
|
145
|
+
echo?: boolean;
|
146
|
+
/**
|
147
|
+
Modify the likelihood of specified tokens appearing in the completion.
|
148
|
+
|
149
|
+
Accepts a JSON object that maps tokens (specified by their token ID in
|
150
|
+
the GPT tokenizer) to an associated bias value from -100 to 100. You
|
151
|
+
can use this tokenizer tool to convert text to token IDs. Mathematically,
|
152
|
+
the bias is added to the logits generated by the model prior to sampling.
|
153
|
+
The exact effect will vary per model, but values between -1 and 1 should
|
154
|
+
decrease or increase likelihood of selection; values like -100 or 100
|
155
|
+
should result in a ban or exclusive selection of the relevant token.
|
156
|
+
|
157
|
+
As an example, you can pass {"50256": -100} to prevent the <|endoftext|>
|
158
|
+
token from being generated.
|
159
|
+
*/
|
160
|
+
logitBias?: Record<number, number>;
|
161
|
+
/**
|
162
|
+
The suffix that comes after a completion of inserted text.
|
163
|
+
*/
|
164
|
+
suffix?: string;
|
165
|
+
/**
|
166
|
+
A unique identifier representing your end-user, which can help OpenAI to
|
167
|
+
monitor and detect abuse. Learn more.
|
168
|
+
*/
|
169
|
+
user?: string;
|
170
|
+
}
|
171
|
+
|
184
172
|
type OpenAICompatibleCompletionConfig = {
|
185
173
|
provider: string;
|
186
174
|
headers: () => Record<string, string | undefined>;
|
@@ -206,6 +194,20 @@ declare class OpenAICompatibleCompletionLanguageModel implements LanguageModelV1
|
|
206
194
|
doStream(options: Parameters<LanguageModelV1['doStream']>[0]): Promise<Awaited<ReturnType<LanguageModelV1['doStream']>>>;
|
207
195
|
}
|
208
196
|
|
197
|
+
type OpenAICompatibleEmbeddingModelId = string;
|
198
|
+
interface OpenAICompatibleEmbeddingSettings {
|
199
|
+
/**
|
200
|
+
The number of dimensions the resulting output embeddings should have.
|
201
|
+
Only supported in text-embedding-3 and later models.
|
202
|
+
*/
|
203
|
+
dimensions?: number;
|
204
|
+
/**
|
205
|
+
A unique identifier representing your end-user, which can help OpenAI to
|
206
|
+
monitor and detect abuse. Learn more.
|
207
|
+
*/
|
208
|
+
user?: string;
|
209
|
+
}
|
210
|
+
|
209
211
|
type OpenAICompatibleEmbeddingConfig = {
|
210
212
|
/**
|
211
213
|
Override the maximum number of embeddings per call.
|
@@ -236,4 +238,46 @@ declare class OpenAICompatibleEmbeddingModel implements EmbeddingModelV1<string>
|
|
236
238
|
doEmbed({ values, headers, abortSignal, }: Parameters<EmbeddingModelV1<string>['doEmbed']>[0]): Promise<Awaited<ReturnType<EmbeddingModelV1<string>['doEmbed']>>>;
|
237
239
|
}
|
238
240
|
|
239
|
-
|
241
|
+
interface OpenAICompatibleProvider<CHAT_MODEL_IDS extends string = string, COMPLETION_MODEL_IDS extends string = string, EMBEDDING_MODEL_IDS extends string = string> extends ProviderV1 {
|
242
|
+
(modelId: CHAT_MODEL_IDS, settings?: OpenAICompatibleChatSettings): LanguageModelV1;
|
243
|
+
languageModel(modelId: CHAT_MODEL_IDS, settings?: OpenAICompatibleChatSettings): LanguageModelV1;
|
244
|
+
chatModel(modelId: CHAT_MODEL_IDS, settings?: OpenAICompatibleChatSettings): LanguageModelV1;
|
245
|
+
completionModel(modelId: COMPLETION_MODEL_IDS, settings?: OpenAICompatibleCompletionSettings): LanguageModelV1;
|
246
|
+
textEmbeddingModel(modelId: EMBEDDING_MODEL_IDS, settings?: OpenAICompatibleEmbeddingSettings): EmbeddingModelV1<string>;
|
247
|
+
}
|
248
|
+
interface OpenAICompatibleProviderSettings {
|
249
|
+
/**
|
250
|
+
Base URL for the API calls.
|
251
|
+
*/
|
252
|
+
baseURL?: string;
|
253
|
+
/**
|
254
|
+
API key for authenticating requests. If specified, adds an `Authorization`
|
255
|
+
header to request headers with the value `Bearer <apiKey>`. This will be added
|
256
|
+
before any headers potentially specified in the `headers` option.
|
257
|
+
*/
|
258
|
+
apiKey?: string;
|
259
|
+
/**
|
260
|
+
Optional custom headers to include in requests. These will be added to request headers
|
261
|
+
after any headers potentially added by use of the `apiKey` option.
|
262
|
+
*/
|
263
|
+
headers?: Record<string, string>;
|
264
|
+
/**
|
265
|
+
Optional custom url query parameters to include in request urls.
|
266
|
+
*/
|
267
|
+
queryParams?: Record<string, string>;
|
268
|
+
/**
|
269
|
+
Custom fetch implementation. You can use it as a middleware to intercept requests,
|
270
|
+
or to provide a custom fetch implementation for e.g. testing.
|
271
|
+
*/
|
272
|
+
fetch?: FetchFunction;
|
273
|
+
/**
|
274
|
+
Provider name.
|
275
|
+
*/
|
276
|
+
name?: string;
|
277
|
+
}
|
278
|
+
/**
|
279
|
+
Create an OpenAICompatible provider instance.
|
280
|
+
*/
|
281
|
+
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>;
|
282
|
+
|
283
|
+
export { type MetadataExtractor, OpenAICompatibleChatLanguageModel, type OpenAICompatibleChatSettings, OpenAICompatibleCompletionLanguageModel, type OpenAICompatibleCompletionSettings, OpenAICompatibleEmbeddingModel, type OpenAICompatibleEmbeddingSettings, type OpenAICompatibleErrorData, type OpenAICompatibleProvider, type OpenAICompatibleProviderSettings, type ProviderErrorStructure, createOpenAICompatible };
|
package/dist/index.d.ts
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
import {
|
1
|
+
import { LanguageModelV1ProviderMetadata, LanguageModelV1, LanguageModelV1ObjectGenerationMode, EmbeddingModelV1, ProviderV1 } from '@ai-sdk/provider';
|
2
2
|
import { FetchFunction } from '@ai-sdk/provider-utils';
|
3
3
|
import { z, ZodSchema } from 'zod';
|
4
4
|
|
@@ -18,94 +18,6 @@ interface OpenAICompatibleChatSettings {
|
|
18
18
|
simulateStreaming?: boolean;
|
19
19
|
}
|
20
20
|
|
21
|
-
type OpenAICompatibleCompletionModelId = string;
|
22
|
-
interface OpenAICompatibleCompletionSettings {
|
23
|
-
/**
|
24
|
-
Echo back the prompt in addition to the completion.
|
25
|
-
*/
|
26
|
-
echo?: boolean;
|
27
|
-
/**
|
28
|
-
Modify the likelihood of specified tokens appearing in the completion.
|
29
|
-
|
30
|
-
Accepts a JSON object that maps tokens (specified by their token ID in
|
31
|
-
the GPT tokenizer) to an associated bias value from -100 to 100. You
|
32
|
-
can use this tokenizer tool to convert text to token IDs. Mathematically,
|
33
|
-
the bias is added to the logits generated by the model prior to sampling.
|
34
|
-
The exact effect will vary per model, but values between -1 and 1 should
|
35
|
-
decrease or increase likelihood of selection; values like -100 or 100
|
36
|
-
should result in a ban or exclusive selection of the relevant token.
|
37
|
-
|
38
|
-
As an example, you can pass {"50256": -100} to prevent the <|endoftext|>
|
39
|
-
token from being generated.
|
40
|
-
*/
|
41
|
-
logitBias?: Record<number, number>;
|
42
|
-
/**
|
43
|
-
The suffix that comes after a completion of inserted text.
|
44
|
-
*/
|
45
|
-
suffix?: string;
|
46
|
-
/**
|
47
|
-
A unique identifier representing your end-user, which can help OpenAI to
|
48
|
-
monitor and detect abuse. Learn more.
|
49
|
-
*/
|
50
|
-
user?: string;
|
51
|
-
}
|
52
|
-
|
53
|
-
type OpenAICompatibleEmbeddingModelId = string;
|
54
|
-
interface OpenAICompatibleEmbeddingSettings {
|
55
|
-
/**
|
56
|
-
The number of dimensions the resulting output embeddings should have.
|
57
|
-
Only supported in text-embedding-3 and later models.
|
58
|
-
*/
|
59
|
-
dimensions?: number;
|
60
|
-
/**
|
61
|
-
A unique identifier representing your end-user, which can help OpenAI to
|
62
|
-
monitor and detect abuse. Learn more.
|
63
|
-
*/
|
64
|
-
user?: string;
|
65
|
-
}
|
66
|
-
|
67
|
-
interface OpenAICompatibleProvider<CHAT_MODEL_IDS extends string = string, COMPLETION_MODEL_IDS extends string = string, EMBEDDING_MODEL_IDS extends string = string> extends ProviderV1 {
|
68
|
-
(modelId: CHAT_MODEL_IDS, settings?: OpenAICompatibleChatSettings): LanguageModelV1;
|
69
|
-
languageModel(modelId: CHAT_MODEL_IDS, settings?: OpenAICompatibleChatSettings): LanguageModelV1;
|
70
|
-
chatModel(modelId: CHAT_MODEL_IDS, settings?: OpenAICompatibleChatSettings): LanguageModelV1;
|
71
|
-
completionModel(modelId: COMPLETION_MODEL_IDS, settings?: OpenAICompatibleCompletionSettings): LanguageModelV1;
|
72
|
-
textEmbeddingModel(modelId: EMBEDDING_MODEL_IDS, settings?: OpenAICompatibleEmbeddingSettings): EmbeddingModelV1<string>;
|
73
|
-
}
|
74
|
-
interface OpenAICompatibleProviderSettings {
|
75
|
-
/**
|
76
|
-
Base URL for the API calls.
|
77
|
-
*/
|
78
|
-
baseURL?: string;
|
79
|
-
/**
|
80
|
-
API key for authenticating requests. If specified, adds an `Authorization`
|
81
|
-
header to request headers with the value `Bearer <apiKey>`. This will be added
|
82
|
-
before any headers potentially specified in the `headers` option.
|
83
|
-
*/
|
84
|
-
apiKey?: string;
|
85
|
-
/**
|
86
|
-
Optional custom headers to include in requests. These will be added to request headers
|
87
|
-
after any headers potentially added by use of the `apiKey` option.
|
88
|
-
*/
|
89
|
-
headers?: Record<string, string>;
|
90
|
-
/**
|
91
|
-
Optional custom url query parameters to include in request urls.
|
92
|
-
*/
|
93
|
-
queryParams?: Record<string, string>;
|
94
|
-
/**
|
95
|
-
Custom fetch implementation. You can use it as a middleware to intercept requests,
|
96
|
-
or to provide a custom fetch implementation for e.g. testing.
|
97
|
-
*/
|
98
|
-
fetch?: FetchFunction;
|
99
|
-
/**
|
100
|
-
Provider name.
|
101
|
-
*/
|
102
|
-
name?: string;
|
103
|
-
}
|
104
|
-
/**
|
105
|
-
Create an OpenAICompatible provider instance.
|
106
|
-
*/
|
107
|
-
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>;
|
108
|
-
|
109
21
|
declare const openaiCompatibleErrorDataSchema: z.ZodObject<{
|
110
22
|
error: z.ZodObject<{
|
111
23
|
message: z.ZodString;
|
@@ -145,6 +57,49 @@ type ProviderErrorStructure<T> = {
|
|
145
57
|
isRetryable?: (response: Response, error?: T) => boolean;
|
146
58
|
};
|
147
59
|
|
60
|
+
/**
|
61
|
+
Extracts provider-specific metadata from API responses.
|
62
|
+
Used to standardize metadata handling across different LLM providers while allowing
|
63
|
+
provider-specific metadata to be captured.
|
64
|
+
*/
|
65
|
+
type MetadataExtractor = {
|
66
|
+
/**
|
67
|
+
* Extracts provider metadata from a complete, non-streaming response.
|
68
|
+
*
|
69
|
+
* @param parsedBody - The parsed response JSON body from the provider's API.
|
70
|
+
*
|
71
|
+
* @returns Provider-specific metadata or undefined if no metadata is available.
|
72
|
+
* The metadata should be under a key indicating the provider id.
|
73
|
+
*/
|
74
|
+
extractMetadata: ({ parsedBody, }: {
|
75
|
+
parsedBody: unknown;
|
76
|
+
}) => LanguageModelV1ProviderMetadata | undefined;
|
77
|
+
/**
|
78
|
+
* Creates an extractor for handling streaming responses. The returned object provides
|
79
|
+
* methods to process individual chunks and build the final metadata from the accumulated
|
80
|
+
* stream data.
|
81
|
+
*
|
82
|
+
* @returns An object with methods to process chunks and build metadata from a stream
|
83
|
+
*/
|
84
|
+
createStreamExtractor: () => {
|
85
|
+
/**
|
86
|
+
* Process an individual chunk from the stream. Called for each chunk in the response stream
|
87
|
+
* to accumulate metadata throughout the streaming process.
|
88
|
+
*
|
89
|
+
* @param parsedChunk - The parsed JSON response chunk from the provider's API
|
90
|
+
*/
|
91
|
+
processChunk(parsedChunk: unknown): void;
|
92
|
+
/**
|
93
|
+
* Builds the metadata object after all chunks have been processed.
|
94
|
+
* Called at the end of the stream to generate the complete provider metadata.
|
95
|
+
*
|
96
|
+
* @returns Provider-specific metadata or undefined if no metadata is available.
|
97
|
+
* The metadata should be under a key indicating the provider id.
|
98
|
+
*/
|
99
|
+
buildMetadata(): LanguageModelV1ProviderMetadata | undefined;
|
100
|
+
};
|
101
|
+
};
|
102
|
+
|
148
103
|
type OpenAICompatibleChatConfig = {
|
149
104
|
provider: string;
|
150
105
|
headers: () => Record<string, string | undefined>;
|
@@ -154,6 +109,7 @@ type OpenAICompatibleChatConfig = {
|
|
154
109
|
}) => string;
|
155
110
|
fetch?: FetchFunction;
|
156
111
|
errorStructure?: ProviderErrorStructure<any>;
|
112
|
+
metadataExtractor?: MetadataExtractor;
|
157
113
|
/**
|
158
114
|
Default object generation mode that should be used with this model when
|
159
115
|
no mode is specified. Should be the mode with the best results for this
|
@@ -181,6 +137,38 @@ declare class OpenAICompatibleChatLanguageModel implements LanguageModelV1 {
|
|
181
137
|
doStream(options: Parameters<LanguageModelV1['doStream']>[0]): Promise<Awaited<ReturnType<LanguageModelV1['doStream']>>>;
|
182
138
|
}
|
183
139
|
|
140
|
+
type OpenAICompatibleCompletionModelId = string;
|
141
|
+
interface OpenAICompatibleCompletionSettings {
|
142
|
+
/**
|
143
|
+
Echo back the prompt in addition to the completion.
|
144
|
+
*/
|
145
|
+
echo?: boolean;
|
146
|
+
/**
|
147
|
+
Modify the likelihood of specified tokens appearing in the completion.
|
148
|
+
|
149
|
+
Accepts a JSON object that maps tokens (specified by their token ID in
|
150
|
+
the GPT tokenizer) to an associated bias value from -100 to 100. You
|
151
|
+
can use this tokenizer tool to convert text to token IDs. Mathematically,
|
152
|
+
the bias is added to the logits generated by the model prior to sampling.
|
153
|
+
The exact effect will vary per model, but values between -1 and 1 should
|
154
|
+
decrease or increase likelihood of selection; values like -100 or 100
|
155
|
+
should result in a ban or exclusive selection of the relevant token.
|
156
|
+
|
157
|
+
As an example, you can pass {"50256": -100} to prevent the <|endoftext|>
|
158
|
+
token from being generated.
|
159
|
+
*/
|
160
|
+
logitBias?: Record<number, number>;
|
161
|
+
/**
|
162
|
+
The suffix that comes after a completion of inserted text.
|
163
|
+
*/
|
164
|
+
suffix?: string;
|
165
|
+
/**
|
166
|
+
A unique identifier representing your end-user, which can help OpenAI to
|
167
|
+
monitor and detect abuse. Learn more.
|
168
|
+
*/
|
169
|
+
user?: string;
|
170
|
+
}
|
171
|
+
|
184
172
|
type OpenAICompatibleCompletionConfig = {
|
185
173
|
provider: string;
|
186
174
|
headers: () => Record<string, string | undefined>;
|
@@ -206,6 +194,20 @@ declare class OpenAICompatibleCompletionLanguageModel implements LanguageModelV1
|
|
206
194
|
doStream(options: Parameters<LanguageModelV1['doStream']>[0]): Promise<Awaited<ReturnType<LanguageModelV1['doStream']>>>;
|
207
195
|
}
|
208
196
|
|
197
|
+
type OpenAICompatibleEmbeddingModelId = string;
|
198
|
+
interface OpenAICompatibleEmbeddingSettings {
|
199
|
+
/**
|
200
|
+
The number of dimensions the resulting output embeddings should have.
|
201
|
+
Only supported in text-embedding-3 and later models.
|
202
|
+
*/
|
203
|
+
dimensions?: number;
|
204
|
+
/**
|
205
|
+
A unique identifier representing your end-user, which can help OpenAI to
|
206
|
+
monitor and detect abuse. Learn more.
|
207
|
+
*/
|
208
|
+
user?: string;
|
209
|
+
}
|
210
|
+
|
209
211
|
type OpenAICompatibleEmbeddingConfig = {
|
210
212
|
/**
|
211
213
|
Override the maximum number of embeddings per call.
|
@@ -236,4 +238,46 @@ declare class OpenAICompatibleEmbeddingModel implements EmbeddingModelV1<string>
|
|
236
238
|
doEmbed({ values, headers, abortSignal, }: Parameters<EmbeddingModelV1<string>['doEmbed']>[0]): Promise<Awaited<ReturnType<EmbeddingModelV1<string>['doEmbed']>>>;
|
237
239
|
}
|
238
240
|
|
239
|
-
|
241
|
+
interface OpenAICompatibleProvider<CHAT_MODEL_IDS extends string = string, COMPLETION_MODEL_IDS extends string = string, EMBEDDING_MODEL_IDS extends string = string> extends ProviderV1 {
|
242
|
+
(modelId: CHAT_MODEL_IDS, settings?: OpenAICompatibleChatSettings): LanguageModelV1;
|
243
|
+
languageModel(modelId: CHAT_MODEL_IDS, settings?: OpenAICompatibleChatSettings): LanguageModelV1;
|
244
|
+
chatModel(modelId: CHAT_MODEL_IDS, settings?: OpenAICompatibleChatSettings): LanguageModelV1;
|
245
|
+
completionModel(modelId: COMPLETION_MODEL_IDS, settings?: OpenAICompatibleCompletionSettings): LanguageModelV1;
|
246
|
+
textEmbeddingModel(modelId: EMBEDDING_MODEL_IDS, settings?: OpenAICompatibleEmbeddingSettings): EmbeddingModelV1<string>;
|
247
|
+
}
|
248
|
+
interface OpenAICompatibleProviderSettings {
|
249
|
+
/**
|
250
|
+
Base URL for the API calls.
|
251
|
+
*/
|
252
|
+
baseURL?: string;
|
253
|
+
/**
|
254
|
+
API key for authenticating requests. If specified, adds an `Authorization`
|
255
|
+
header to request headers with the value `Bearer <apiKey>`. This will be added
|
256
|
+
before any headers potentially specified in the `headers` option.
|
257
|
+
*/
|
258
|
+
apiKey?: string;
|
259
|
+
/**
|
260
|
+
Optional custom headers to include in requests. These will be added to request headers
|
261
|
+
after any headers potentially added by use of the `apiKey` option.
|
262
|
+
*/
|
263
|
+
headers?: Record<string, string>;
|
264
|
+
/**
|
265
|
+
Optional custom url query parameters to include in request urls.
|
266
|
+
*/
|
267
|
+
queryParams?: Record<string, string>;
|
268
|
+
/**
|
269
|
+
Custom fetch implementation. You can use it as a middleware to intercept requests,
|
270
|
+
or to provide a custom fetch implementation for e.g. testing.
|
271
|
+
*/
|
272
|
+
fetch?: FetchFunction;
|
273
|
+
/**
|
274
|
+
Provider name.
|
275
|
+
*/
|
276
|
+
name?: string;
|
277
|
+
}
|
278
|
+
/**
|
279
|
+
Create an OpenAICompatible provider instance.
|
280
|
+
*/
|
281
|
+
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>;
|
282
|
+
|
283
|
+
export { type MetadataExtractor, OpenAICompatibleChatLanguageModel, type OpenAICompatibleChatSettings, OpenAICompatibleCompletionLanguageModel, type OpenAICompatibleCompletionSettings, OpenAICompatibleEmbeddingModel, type OpenAICompatibleEmbeddingSettings, type OpenAICompatibleErrorData, type OpenAICompatibleProvider, type OpenAICompatibleProviderSettings, type ProviderErrorStructure, createOpenAICompatible };
|
package/dist/index.js
CHANGED
@@ -27,9 +27,6 @@ __export(src_exports, {
|
|
27
27
|
});
|
28
28
|
module.exports = __toCommonJS(src_exports);
|
29
29
|
|
30
|
-
// src/openai-compatible-provider.ts
|
31
|
-
var import_provider_utils5 = require("@ai-sdk/provider-utils");
|
32
|
-
|
33
30
|
// src/openai-compatible-chat-language-model.ts
|
34
31
|
var import_provider3 = require("@ai-sdk/provider");
|
35
32
|
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
@@ -382,10 +379,14 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
382
379
|
}
|
383
380
|
}
|
384
381
|
async doGenerate(options) {
|
385
|
-
var _a, _b, _c, _d, _e, _f, _g;
|
382
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
386
383
|
const { args, warnings } = this.getArgs({ ...options });
|
387
384
|
const body = JSON.stringify(args);
|
388
|
-
const {
|
385
|
+
const {
|
386
|
+
responseHeaders,
|
387
|
+
value: responseBody,
|
388
|
+
rawValue: parsedBody
|
389
|
+
} = await (0, import_provider_utils2.postJsonToApi)({
|
389
390
|
url: this.config.url({
|
390
391
|
path: "/chat/completions",
|
391
392
|
modelId: this.modelId
|
@@ -400,11 +401,14 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
400
401
|
fetch: this.config.fetch
|
401
402
|
});
|
402
403
|
const { messages: rawPrompt, ...rawSettings } = args;
|
403
|
-
const choice =
|
404
|
+
const choice = responseBody.choices[0];
|
405
|
+
const providerMetadata = (_b = (_a = this.config.metadataExtractor) == null ? void 0 : _a.extractMetadata) == null ? void 0 : _b.call(_a, {
|
406
|
+
parsedBody
|
407
|
+
});
|
404
408
|
return {
|
405
|
-
text: (
|
406
|
-
reasoning: (
|
407
|
-
toolCalls: (
|
409
|
+
text: (_c = choice.message.content) != null ? _c : void 0,
|
410
|
+
reasoning: (_d = choice.message.reasoning_content) != null ? _d : void 0,
|
411
|
+
toolCalls: (_e = choice.message.tool_calls) == null ? void 0 : _e.map((toolCall) => {
|
408
412
|
var _a2;
|
409
413
|
return {
|
410
414
|
toolCallType: "function",
|
@@ -415,17 +419,19 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
415
419
|
}),
|
416
420
|
finishReason: mapOpenAICompatibleFinishReason(choice.finish_reason),
|
417
421
|
usage: {
|
418
|
-
promptTokens: (
|
419
|
-
completionTokens: (
|
422
|
+
promptTokens: (_g = (_f = responseBody.usage) == null ? void 0 : _f.prompt_tokens) != null ? _g : NaN,
|
423
|
+
completionTokens: (_i = (_h = responseBody.usage) == null ? void 0 : _h.completion_tokens) != null ? _i : NaN
|
420
424
|
},
|
425
|
+
...providerMetadata && { providerMetadata },
|
421
426
|
rawCall: { rawPrompt, rawSettings },
|
422
427
|
rawResponse: { headers: responseHeaders },
|
423
|
-
response: getResponseMetadata(
|
428
|
+
response: getResponseMetadata(responseBody),
|
424
429
|
warnings,
|
425
430
|
request: { body }
|
426
431
|
};
|
427
432
|
}
|
428
433
|
async doStream(options) {
|
434
|
+
var _a;
|
429
435
|
if (this.settings.simulateStreaming) {
|
430
436
|
const result = await this.doGenerate(options);
|
431
437
|
const simulatedStream = new ReadableStream({
|
@@ -470,6 +476,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
470
476
|
}
|
471
477
|
const { args, warnings } = this.getArgs({ ...options });
|
472
478
|
const body = JSON.stringify({ ...args, stream: true });
|
479
|
+
const metadataExtractor = (_a = this.config.metadataExtractor) == null ? void 0 : _a.createStreamExtractor();
|
473
480
|
const { responseHeaders, value: response } = await (0, import_provider_utils2.postJsonToApi)({
|
474
481
|
url: this.config.url({
|
475
482
|
path: "/chat/completions",
|
@@ -495,19 +502,19 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
495
502
|
completionTokens: void 0
|
496
503
|
};
|
497
504
|
let isFirstChunk = true;
|
498
|
-
let providerMetadata;
|
499
505
|
return {
|
500
506
|
stream: response.pipeThrough(
|
501
507
|
new TransformStream({
|
502
508
|
// TODO we lost type safety on Chunk, most likely due to the error schema. MUST FIX
|
503
509
|
transform(chunk, controller) {
|
504
|
-
var
|
510
|
+
var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
|
505
511
|
if (!chunk.success) {
|
506
512
|
finishReason = "error";
|
507
513
|
controller.enqueue({ type: "error", error: chunk.error });
|
508
514
|
return;
|
509
515
|
}
|
510
516
|
const value = chunk.value;
|
517
|
+
metadataExtractor == null ? void 0 : metadataExtractor.processChunk(chunk.rawValue);
|
511
518
|
if ("error" in value) {
|
512
519
|
finishReason = "error";
|
513
520
|
controller.enqueue({ type: "error", error: value.error.message });
|
@@ -522,7 +529,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
522
529
|
}
|
523
530
|
if (value.usage != null) {
|
524
531
|
usage = {
|
525
|
-
promptTokens: (
|
532
|
+
promptTokens: (_a2 = value.usage.prompt_tokens) != null ? _a2 : void 0,
|
526
533
|
completionTokens: (_b = value.usage.completion_tokens) != null ? _b : void 0
|
527
534
|
};
|
528
535
|
}
|
@@ -631,15 +638,16 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
631
638
|
}
|
632
639
|
},
|
633
640
|
flush(controller) {
|
634
|
-
var
|
641
|
+
var _a2, _b;
|
642
|
+
const metadata = metadataExtractor == null ? void 0 : metadataExtractor.buildMetadata();
|
635
643
|
controller.enqueue({
|
636
644
|
type: "finish",
|
637
645
|
finishReason,
|
638
646
|
usage: {
|
639
|
-
promptTokens: (
|
647
|
+
promptTokens: (_a2 = usage.promptTokens) != null ? _a2 : NaN,
|
640
648
|
completionTokens: (_b = usage.completionTokens) != null ? _b : NaN
|
641
649
|
},
|
642
|
-
...
|
650
|
+
...metadata && { providerMetadata: metadata }
|
643
651
|
});
|
644
652
|
}
|
645
653
|
})
|
@@ -1127,6 +1135,7 @@ var openaiTextEmbeddingResponseSchema = import_zod4.z.object({
|
|
1127
1135
|
});
|
1128
1136
|
|
1129
1137
|
// src/openai-compatible-provider.ts
|
1138
|
+
var import_provider_utils5 = require("@ai-sdk/provider-utils");
|
1130
1139
|
function createOpenAICompatible(options) {
|
1131
1140
|
if (!options.baseURL) {
|
1132
1141
|
throw new Error("Base URL is required");
|