@ai-sdk/openai-compatible 0.1.1 → 0.1.2

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 CHANGED
@@ -1,5 +1,15 @@
1
1
  # @ai-sdk/openai-compatible
2
2
 
3
+ ## 0.1.2
4
+
5
+ ### Patch Changes
6
+
7
+ - ed012d2: feat (provider): add metadata extraction mechanism to openai-compatible providers
8
+ - Updated dependencies [ed012d2]
9
+ - Updated dependencies [3a58a2e]
10
+ - @ai-sdk/provider-utils@2.1.2
11
+ - @ai-sdk/provider@1.0.6
12
+
3
13
  ## 0.1.1
4
14
 
5
15
  ### Patch Changes
package/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { ProviderV1, LanguageModelV1, EmbeddingModelV1, LanguageModelV1ObjectGenerationMode } from '@ai-sdk/provider';
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,48 @@ 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 a streaming metadata processor that can accumulate and process chunks
79
+ * of a streaming response. Used to build metadata progressively during streaming.
80
+ *
81
+ * @returns A new StreamingMetadataProcessor instance
82
+ */
83
+ createStreamExtractor: () => {
84
+ /**
85
+ * Process an individual chunk from the stream. Called for each chunk in the response stream
86
+ * to accumulate metadata throughout the streaming process.
87
+ *
88
+ * @param parsedChunk - The parsed JSON response chunk from the provider's API
89
+ */
90
+ processChunk(parsedChunk: unknown): void;
91
+ /**
92
+ * Builds the metadata object after all chunks have been processed.
93
+ * Called at the end of the stream to generate the complete provider metadata.
94
+ *
95
+ * @returns Provider-specific metadata or undefined if no metadata is available.
96
+ * The metadata should be under a key indicating the provider id.
97
+ */
98
+ buildMetadata(): LanguageModelV1ProviderMetadata | undefined;
99
+ };
100
+ };
101
+
148
102
  type OpenAICompatibleChatConfig = {
149
103
  provider: string;
150
104
  headers: () => Record<string, string | undefined>;
@@ -154,6 +108,7 @@ type OpenAICompatibleChatConfig = {
154
108
  }) => string;
155
109
  fetch?: FetchFunction;
156
110
  errorStructure?: ProviderErrorStructure<any>;
111
+ metadataExtractor?: MetadataExtractor;
157
112
  /**
158
113
  Default object generation mode that should be used with this model when
159
114
  no mode is specified. Should be the mode with the best results for this
@@ -181,6 +136,38 @@ declare class OpenAICompatibleChatLanguageModel implements LanguageModelV1 {
181
136
  doStream(options: Parameters<LanguageModelV1['doStream']>[0]): Promise<Awaited<ReturnType<LanguageModelV1['doStream']>>>;
182
137
  }
183
138
 
139
+ type OpenAICompatibleCompletionModelId = string;
140
+ interface OpenAICompatibleCompletionSettings {
141
+ /**
142
+ Echo back the prompt in addition to the completion.
143
+ */
144
+ echo?: boolean;
145
+ /**
146
+ Modify the likelihood of specified tokens appearing in the completion.
147
+
148
+ Accepts a JSON object that maps tokens (specified by their token ID in
149
+ the GPT tokenizer) to an associated bias value from -100 to 100. You
150
+ can use this tokenizer tool to convert text to token IDs. Mathematically,
151
+ the bias is added to the logits generated by the model prior to sampling.
152
+ The exact effect will vary per model, but values between -1 and 1 should
153
+ decrease or increase likelihood of selection; values like -100 or 100
154
+ should result in a ban or exclusive selection of the relevant token.
155
+
156
+ As an example, you can pass {"50256": -100} to prevent the <|endoftext|>
157
+ token from being generated.
158
+ */
159
+ logitBias?: Record<number, number>;
160
+ /**
161
+ The suffix that comes after a completion of inserted text.
162
+ */
163
+ suffix?: string;
164
+ /**
165
+ A unique identifier representing your end-user, which can help OpenAI to
166
+ monitor and detect abuse. Learn more.
167
+ */
168
+ user?: string;
169
+ }
170
+
184
171
  type OpenAICompatibleCompletionConfig = {
185
172
  provider: string;
186
173
  headers: () => Record<string, string | undefined>;
@@ -206,6 +193,20 @@ declare class OpenAICompatibleCompletionLanguageModel implements LanguageModelV1
206
193
  doStream(options: Parameters<LanguageModelV1['doStream']>[0]): Promise<Awaited<ReturnType<LanguageModelV1['doStream']>>>;
207
194
  }
208
195
 
196
+ type OpenAICompatibleEmbeddingModelId = string;
197
+ interface OpenAICompatibleEmbeddingSettings {
198
+ /**
199
+ The number of dimensions the resulting output embeddings should have.
200
+ Only supported in text-embedding-3 and later models.
201
+ */
202
+ dimensions?: number;
203
+ /**
204
+ A unique identifier representing your end-user, which can help OpenAI to
205
+ monitor and detect abuse. Learn more.
206
+ */
207
+ user?: string;
208
+ }
209
+
209
210
  type OpenAICompatibleEmbeddingConfig = {
210
211
  /**
211
212
  Override the maximum number of embeddings per call.
@@ -236,4 +237,46 @@ declare class OpenAICompatibleEmbeddingModel implements EmbeddingModelV1<string>
236
237
  doEmbed({ values, headers, abortSignal, }: Parameters<EmbeddingModelV1<string>['doEmbed']>[0]): Promise<Awaited<ReturnType<EmbeddingModelV1<string>['doEmbed']>>>;
237
238
  }
238
239
 
239
- export { OpenAICompatibleChatLanguageModel, type OpenAICompatibleChatSettings, OpenAICompatibleCompletionLanguageModel, type OpenAICompatibleCompletionSettings, OpenAICompatibleEmbeddingModel, type OpenAICompatibleEmbeddingSettings, type OpenAICompatibleErrorData, type OpenAICompatibleProvider, type OpenAICompatibleProviderSettings, type ProviderErrorStructure, createOpenAICompatible };
240
+ interface OpenAICompatibleProvider<CHAT_MODEL_IDS extends string = string, COMPLETION_MODEL_IDS extends string = string, EMBEDDING_MODEL_IDS extends string = string> extends ProviderV1 {
241
+ (modelId: CHAT_MODEL_IDS, settings?: OpenAICompatibleChatSettings): LanguageModelV1;
242
+ languageModel(modelId: CHAT_MODEL_IDS, settings?: OpenAICompatibleChatSettings): LanguageModelV1;
243
+ chatModel(modelId: CHAT_MODEL_IDS, settings?: OpenAICompatibleChatSettings): LanguageModelV1;
244
+ completionModel(modelId: COMPLETION_MODEL_IDS, settings?: OpenAICompatibleCompletionSettings): LanguageModelV1;
245
+ textEmbeddingModel(modelId: EMBEDDING_MODEL_IDS, settings?: OpenAICompatibleEmbeddingSettings): EmbeddingModelV1<string>;
246
+ }
247
+ interface OpenAICompatibleProviderSettings {
248
+ /**
249
+ Base URL for the API calls.
250
+ */
251
+ baseURL?: string;
252
+ /**
253
+ API key for authenticating requests. If specified, adds an `Authorization`
254
+ header to request headers with the value `Bearer <apiKey>`. This will be added
255
+ before any headers potentially specified in the `headers` option.
256
+ */
257
+ apiKey?: string;
258
+ /**
259
+ Optional custom headers to include in requests. These will be added to request headers
260
+ after any headers potentially added by use of the `apiKey` option.
261
+ */
262
+ headers?: Record<string, string>;
263
+ /**
264
+ Optional custom url query parameters to include in request urls.
265
+ */
266
+ queryParams?: Record<string, string>;
267
+ /**
268
+ Custom fetch implementation. You can use it as a middleware to intercept requests,
269
+ or to provide a custom fetch implementation for e.g. testing.
270
+ */
271
+ fetch?: FetchFunction;
272
+ /**
273
+ Provider name.
274
+ */
275
+ name?: string;
276
+ }
277
+ /**
278
+ Create an OpenAICompatible provider instance.
279
+ */
280
+ 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>;
281
+
282
+ 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 { ProviderV1, LanguageModelV1, EmbeddingModelV1, LanguageModelV1ObjectGenerationMode } from '@ai-sdk/provider';
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,48 @@ 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 a streaming metadata processor that can accumulate and process chunks
79
+ * of a streaming response. Used to build metadata progressively during streaming.
80
+ *
81
+ * @returns A new StreamingMetadataProcessor instance
82
+ */
83
+ createStreamExtractor: () => {
84
+ /**
85
+ * Process an individual chunk from the stream. Called for each chunk in the response stream
86
+ * to accumulate metadata throughout the streaming process.
87
+ *
88
+ * @param parsedChunk - The parsed JSON response chunk from the provider's API
89
+ */
90
+ processChunk(parsedChunk: unknown): void;
91
+ /**
92
+ * Builds the metadata object after all chunks have been processed.
93
+ * Called at the end of the stream to generate the complete provider metadata.
94
+ *
95
+ * @returns Provider-specific metadata or undefined if no metadata is available.
96
+ * The metadata should be under a key indicating the provider id.
97
+ */
98
+ buildMetadata(): LanguageModelV1ProviderMetadata | undefined;
99
+ };
100
+ };
101
+
148
102
  type OpenAICompatibleChatConfig = {
149
103
  provider: string;
150
104
  headers: () => Record<string, string | undefined>;
@@ -154,6 +108,7 @@ type OpenAICompatibleChatConfig = {
154
108
  }) => string;
155
109
  fetch?: FetchFunction;
156
110
  errorStructure?: ProviderErrorStructure<any>;
111
+ metadataExtractor?: MetadataExtractor;
157
112
  /**
158
113
  Default object generation mode that should be used with this model when
159
114
  no mode is specified. Should be the mode with the best results for this
@@ -181,6 +136,38 @@ declare class OpenAICompatibleChatLanguageModel implements LanguageModelV1 {
181
136
  doStream(options: Parameters<LanguageModelV1['doStream']>[0]): Promise<Awaited<ReturnType<LanguageModelV1['doStream']>>>;
182
137
  }
183
138
 
139
+ type OpenAICompatibleCompletionModelId = string;
140
+ interface OpenAICompatibleCompletionSettings {
141
+ /**
142
+ Echo back the prompt in addition to the completion.
143
+ */
144
+ echo?: boolean;
145
+ /**
146
+ Modify the likelihood of specified tokens appearing in the completion.
147
+
148
+ Accepts a JSON object that maps tokens (specified by their token ID in
149
+ the GPT tokenizer) to an associated bias value from -100 to 100. You
150
+ can use this tokenizer tool to convert text to token IDs. Mathematically,
151
+ the bias is added to the logits generated by the model prior to sampling.
152
+ The exact effect will vary per model, but values between -1 and 1 should
153
+ decrease or increase likelihood of selection; values like -100 or 100
154
+ should result in a ban or exclusive selection of the relevant token.
155
+
156
+ As an example, you can pass {"50256": -100} to prevent the <|endoftext|>
157
+ token from being generated.
158
+ */
159
+ logitBias?: Record<number, number>;
160
+ /**
161
+ The suffix that comes after a completion of inserted text.
162
+ */
163
+ suffix?: string;
164
+ /**
165
+ A unique identifier representing your end-user, which can help OpenAI to
166
+ monitor and detect abuse. Learn more.
167
+ */
168
+ user?: string;
169
+ }
170
+
184
171
  type OpenAICompatibleCompletionConfig = {
185
172
  provider: string;
186
173
  headers: () => Record<string, string | undefined>;
@@ -206,6 +193,20 @@ declare class OpenAICompatibleCompletionLanguageModel implements LanguageModelV1
206
193
  doStream(options: Parameters<LanguageModelV1['doStream']>[0]): Promise<Awaited<ReturnType<LanguageModelV1['doStream']>>>;
207
194
  }
208
195
 
196
+ type OpenAICompatibleEmbeddingModelId = string;
197
+ interface OpenAICompatibleEmbeddingSettings {
198
+ /**
199
+ The number of dimensions the resulting output embeddings should have.
200
+ Only supported in text-embedding-3 and later models.
201
+ */
202
+ dimensions?: number;
203
+ /**
204
+ A unique identifier representing your end-user, which can help OpenAI to
205
+ monitor and detect abuse. Learn more.
206
+ */
207
+ user?: string;
208
+ }
209
+
209
210
  type OpenAICompatibleEmbeddingConfig = {
210
211
  /**
211
212
  Override the maximum number of embeddings per call.
@@ -236,4 +237,46 @@ declare class OpenAICompatibleEmbeddingModel implements EmbeddingModelV1<string>
236
237
  doEmbed({ values, headers, abortSignal, }: Parameters<EmbeddingModelV1<string>['doEmbed']>[0]): Promise<Awaited<ReturnType<EmbeddingModelV1<string>['doEmbed']>>>;
237
238
  }
238
239
 
239
- export { OpenAICompatibleChatLanguageModel, type OpenAICompatibleChatSettings, OpenAICompatibleCompletionLanguageModel, type OpenAICompatibleCompletionSettings, OpenAICompatibleEmbeddingModel, type OpenAICompatibleEmbeddingSettings, type OpenAICompatibleErrorData, type OpenAICompatibleProvider, type OpenAICompatibleProviderSettings, type ProviderErrorStructure, createOpenAICompatible };
240
+ interface OpenAICompatibleProvider<CHAT_MODEL_IDS extends string = string, COMPLETION_MODEL_IDS extends string = string, EMBEDDING_MODEL_IDS extends string = string> extends ProviderV1 {
241
+ (modelId: CHAT_MODEL_IDS, settings?: OpenAICompatibleChatSettings): LanguageModelV1;
242
+ languageModel(modelId: CHAT_MODEL_IDS, settings?: OpenAICompatibleChatSettings): LanguageModelV1;
243
+ chatModel(modelId: CHAT_MODEL_IDS, settings?: OpenAICompatibleChatSettings): LanguageModelV1;
244
+ completionModel(modelId: COMPLETION_MODEL_IDS, settings?: OpenAICompatibleCompletionSettings): LanguageModelV1;
245
+ textEmbeddingModel(modelId: EMBEDDING_MODEL_IDS, settings?: OpenAICompatibleEmbeddingSettings): EmbeddingModelV1<string>;
246
+ }
247
+ interface OpenAICompatibleProviderSettings {
248
+ /**
249
+ Base URL for the API calls.
250
+ */
251
+ baseURL?: string;
252
+ /**
253
+ API key for authenticating requests. If specified, adds an `Authorization`
254
+ header to request headers with the value `Bearer <apiKey>`. This will be added
255
+ before any headers potentially specified in the `headers` option.
256
+ */
257
+ apiKey?: string;
258
+ /**
259
+ Optional custom headers to include in requests. These will be added to request headers
260
+ after any headers potentially added by use of the `apiKey` option.
261
+ */
262
+ headers?: Record<string, string>;
263
+ /**
264
+ Optional custom url query parameters to include in request urls.
265
+ */
266
+ queryParams?: Record<string, string>;
267
+ /**
268
+ Custom fetch implementation. You can use it as a middleware to intercept requests,
269
+ or to provide a custom fetch implementation for e.g. testing.
270
+ */
271
+ fetch?: FetchFunction;
272
+ /**
273
+ Provider name.
274
+ */
275
+ name?: string;
276
+ }
277
+ /**
278
+ Create an OpenAICompatible provider instance.
279
+ */
280
+ 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>;
281
+
282
+ 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 { responseHeaders, value: response } = await (0, import_provider_utils2.postJsonToApi)({
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 = response.choices[0];
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: (_a = choice.message.content) != null ? _a : void 0,
406
- reasoning: (_b = choice.message.reasoning_content) != null ? _b : void 0,
407
- toolCalls: (_c = choice.message.tool_calls) == null ? void 0 : _c.map((toolCall) => {
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: (_e = (_d = response.usage) == null ? void 0 : _d.prompt_tokens) != null ? _e : NaN,
419
- completionTokens: (_g = (_f = response.usage) == null ? void 0 : _f.completion_tokens) != null ? _g : NaN
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(response),
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 _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
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: (_a = value.usage.prompt_tokens) != null ? _a : void 0,
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 _a, _b;
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: (_a = usage.promptTokens) != null ? _a : NaN,
647
+ promptTokens: (_a2 = usage.promptTokens) != null ? _a2 : NaN,
640
648
  completionTokens: (_b = usage.completionTokens) != null ? _b : NaN
641
649
  },
642
- ...providerMetadata != null ? { providerMetadata } : {}
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");