@ai-sdk/openai-compatible 0.1.0 → 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 +20 -0
- package/dist/index.d.mts +133 -90
- package/dist/index.d.ts +133 -90
- package/dist/index.js +42 -17
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +44 -19
- package/dist/index.mjs.map +1 -1
- package/internal/dist/index.d.mts +44 -1
- package/internal/dist/index.d.ts +44 -1
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
@@ -1,5 +1,25 @@
|
|
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
|
+
|
13
|
+
## 0.1.1
|
14
|
+
|
15
|
+
### Patch Changes
|
16
|
+
|
17
|
+
- 0a699f1: feat: add reasoning token support
|
18
|
+
- Updated dependencies [e7a9ec9]
|
19
|
+
- Updated dependencies [0a699f1]
|
20
|
+
- @ai-sdk/provider-utils@2.1.1
|
21
|
+
- @ai-sdk/provider@1.0.5
|
22
|
+
|
3
23
|
## 0.1.0
|
4
24
|
|
5
25
|
### Minor 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,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
|
-
|
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 {
|
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
|
-
|
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 };
|