@ai-sdk/openai-compatible 2.0.15 → 2.0.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/dist/index.d.mts +5 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +23 -6
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +23 -6
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -2
- package/src/chat/convert-openai-compatible-chat-usage.ts +55 -0
- package/src/chat/convert-to-openai-compatible-chat-messages.test.ts +1238 -0
- package/src/chat/convert-to-openai-compatible-chat-messages.ts +246 -0
- package/src/chat/get-response-metadata.ts +15 -0
- package/src/chat/map-openai-compatible-finish-reason.ts +19 -0
- package/src/chat/openai-compatible-api-types.ts +86 -0
- package/src/chat/openai-compatible-chat-language-model.test.ts +3292 -0
- package/src/chat/openai-compatible-chat-language-model.ts +830 -0
- package/src/chat/openai-compatible-chat-options.ts +34 -0
- package/src/chat/openai-compatible-metadata-extractor.ts +48 -0
- package/src/chat/openai-compatible-prepare-tools.test.ts +336 -0
- package/src/chat/openai-compatible-prepare-tools.ts +98 -0
- package/src/completion/convert-openai-compatible-completion-usage.ts +46 -0
- package/src/completion/convert-to-openai-compatible-completion-prompt.ts +93 -0
- package/src/completion/get-response-metadata.ts +15 -0
- package/src/completion/map-openai-compatible-finish-reason.ts +19 -0
- package/src/completion/openai-compatible-completion-language-model.test.ts +773 -0
- package/src/completion/openai-compatible-completion-language-model.ts +390 -0
- package/src/completion/openai-compatible-completion-options.ts +33 -0
- package/src/embedding/openai-compatible-embedding-model.test.ts +171 -0
- package/src/embedding/openai-compatible-embedding-model.ts +166 -0
- package/src/embedding/openai-compatible-embedding-options.ts +21 -0
- package/src/image/openai-compatible-image-model.test.ts +494 -0
- package/src/image/openai-compatible-image-model.ts +205 -0
- package/src/image/openai-compatible-image-settings.ts +1 -0
- package/src/index.ts +27 -0
- package/src/internal/index.ts +4 -0
- package/src/openai-compatible-error.ts +30 -0
- package/src/openai-compatible-provider.test.ts +329 -0
- package/src/openai-compatible-provider.ts +189 -0
- package/src/version.ts +5 -0
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import {
|
|
2
|
+
EmbeddingModelV3,
|
|
3
|
+
ImageModelV3,
|
|
4
|
+
LanguageModelV3,
|
|
5
|
+
ProviderV3,
|
|
6
|
+
} from '@ai-sdk/provider';
|
|
7
|
+
import {
|
|
8
|
+
FetchFunction,
|
|
9
|
+
withoutTrailingSlash,
|
|
10
|
+
withUserAgentSuffix,
|
|
11
|
+
} from '@ai-sdk/provider-utils';
|
|
12
|
+
import {
|
|
13
|
+
OpenAICompatibleChatConfig,
|
|
14
|
+
OpenAICompatibleChatLanguageModel,
|
|
15
|
+
} from './chat/openai-compatible-chat-language-model';
|
|
16
|
+
import { OpenAICompatibleCompletionLanguageModel } from './completion/openai-compatible-completion-language-model';
|
|
17
|
+
import { OpenAICompatibleEmbeddingModel } from './embedding/openai-compatible-embedding-model';
|
|
18
|
+
import { OpenAICompatibleImageModel } from './image/openai-compatible-image-model';
|
|
19
|
+
import { VERSION } from './version';
|
|
20
|
+
|
|
21
|
+
export interface OpenAICompatibleProvider<
|
|
22
|
+
CHAT_MODEL_IDS extends string = string,
|
|
23
|
+
COMPLETION_MODEL_IDS extends string = string,
|
|
24
|
+
EMBEDDING_MODEL_IDS extends string = string,
|
|
25
|
+
IMAGE_MODEL_IDS extends string = string,
|
|
26
|
+
> extends Omit<ProviderV3, 'imageModel'> {
|
|
27
|
+
(modelId: CHAT_MODEL_IDS): LanguageModelV3;
|
|
28
|
+
|
|
29
|
+
languageModel(
|
|
30
|
+
modelId: CHAT_MODEL_IDS,
|
|
31
|
+
config?: Partial<OpenAICompatibleChatConfig>,
|
|
32
|
+
): LanguageModelV3;
|
|
33
|
+
|
|
34
|
+
chatModel(modelId: CHAT_MODEL_IDS): LanguageModelV3;
|
|
35
|
+
|
|
36
|
+
completionModel(modelId: COMPLETION_MODEL_IDS): LanguageModelV3;
|
|
37
|
+
|
|
38
|
+
embeddingModel(modelId: EMBEDDING_MODEL_IDS): EmbeddingModelV3;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* @deprecated Use `embeddingModel` instead.
|
|
42
|
+
*/
|
|
43
|
+
textEmbeddingModel(modelId: EMBEDDING_MODEL_IDS): EmbeddingModelV3;
|
|
44
|
+
|
|
45
|
+
imageModel(modelId: IMAGE_MODEL_IDS): ImageModelV3;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface OpenAICompatibleProviderSettings {
|
|
49
|
+
/**
|
|
50
|
+
Base URL for the API calls.
|
|
51
|
+
*/
|
|
52
|
+
baseURL: string;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
Provider name.
|
|
56
|
+
*/
|
|
57
|
+
name: string;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
API key for authenticating requests. If specified, adds an `Authorization`
|
|
61
|
+
header to request headers with the value `Bearer <apiKey>`. This will be added
|
|
62
|
+
before any headers potentially specified in the `headers` option.
|
|
63
|
+
*/
|
|
64
|
+
apiKey?: string;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
Optional custom headers to include in requests. These will be added to request headers
|
|
68
|
+
after any headers potentially added by use of the `apiKey` option.
|
|
69
|
+
*/
|
|
70
|
+
headers?: Record<string, string>;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
Optional custom url query parameters to include in request urls.
|
|
74
|
+
*/
|
|
75
|
+
queryParams?: Record<string, string>;
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
Custom fetch implementation. You can use it as a middleware to intercept requests,
|
|
79
|
+
or to provide a custom fetch implementation for e.g. testing.
|
|
80
|
+
*/
|
|
81
|
+
fetch?: FetchFunction;
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
Include usage information in streaming responses.
|
|
85
|
+
*/
|
|
86
|
+
includeUsage?: boolean;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Whether the provider supports structured outputs in chat models.
|
|
90
|
+
*/
|
|
91
|
+
supportsStructuredOutputs?: boolean;
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Optional function to transform the request body before sending it to the API.
|
|
95
|
+
* This is useful for proxy providers that may require a different request format
|
|
96
|
+
* than the official OpenAI API.
|
|
97
|
+
*/
|
|
98
|
+
transformRequestBody?: (args: Record<string, any>) => Record<string, any>;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
Create an OpenAICompatible provider instance.
|
|
103
|
+
*/
|
|
104
|
+
export function createOpenAICompatible<
|
|
105
|
+
CHAT_MODEL_IDS extends string,
|
|
106
|
+
COMPLETION_MODEL_IDS extends string,
|
|
107
|
+
EMBEDDING_MODEL_IDS extends string,
|
|
108
|
+
IMAGE_MODEL_IDS extends string,
|
|
109
|
+
>(
|
|
110
|
+
options: OpenAICompatibleProviderSettings,
|
|
111
|
+
): OpenAICompatibleProvider<
|
|
112
|
+
CHAT_MODEL_IDS,
|
|
113
|
+
COMPLETION_MODEL_IDS,
|
|
114
|
+
EMBEDDING_MODEL_IDS,
|
|
115
|
+
IMAGE_MODEL_IDS
|
|
116
|
+
> {
|
|
117
|
+
const baseURL = withoutTrailingSlash(options.baseURL);
|
|
118
|
+
const providerName = options.name;
|
|
119
|
+
|
|
120
|
+
interface CommonModelConfig {
|
|
121
|
+
provider: string;
|
|
122
|
+
url: ({ path }: { path: string }) => string;
|
|
123
|
+
headers: () => Record<string, string>;
|
|
124
|
+
fetch?: FetchFunction;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const headers = {
|
|
128
|
+
...(options.apiKey && { Authorization: `Bearer ${options.apiKey}` }),
|
|
129
|
+
...options.headers,
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
const getHeaders = () =>
|
|
133
|
+
withUserAgentSuffix(headers, `ai-sdk/openai-compatible/${VERSION}`);
|
|
134
|
+
|
|
135
|
+
const getCommonModelConfig = (modelType: string): CommonModelConfig => ({
|
|
136
|
+
provider: `${providerName}.${modelType}`,
|
|
137
|
+
url: ({ path }) => {
|
|
138
|
+
const url = new URL(`${baseURL}${path}`);
|
|
139
|
+
if (options.queryParams) {
|
|
140
|
+
url.search = new URLSearchParams(options.queryParams).toString();
|
|
141
|
+
}
|
|
142
|
+
return url.toString();
|
|
143
|
+
},
|
|
144
|
+
headers: getHeaders,
|
|
145
|
+
fetch: options.fetch,
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
const createLanguageModel = (modelId: CHAT_MODEL_IDS) =>
|
|
149
|
+
createChatModel(modelId);
|
|
150
|
+
|
|
151
|
+
const createChatModel = (modelId: CHAT_MODEL_IDS) =>
|
|
152
|
+
new OpenAICompatibleChatLanguageModel(modelId, {
|
|
153
|
+
...getCommonModelConfig('chat'),
|
|
154
|
+
includeUsage: options.includeUsage,
|
|
155
|
+
supportsStructuredOutputs: options.supportsStructuredOutputs,
|
|
156
|
+
transformRequestBody: options.transformRequestBody,
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
const createCompletionModel = (modelId: COMPLETION_MODEL_IDS) =>
|
|
160
|
+
new OpenAICompatibleCompletionLanguageModel(modelId, {
|
|
161
|
+
...getCommonModelConfig('completion'),
|
|
162
|
+
includeUsage: options.includeUsage,
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
const createEmbeddingModel = (modelId: EMBEDDING_MODEL_IDS) =>
|
|
166
|
+
new OpenAICompatibleEmbeddingModel(modelId, {
|
|
167
|
+
...getCommonModelConfig('embedding'),
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
const createImageModel = (modelId: IMAGE_MODEL_IDS) =>
|
|
171
|
+
new OpenAICompatibleImageModel(modelId, getCommonModelConfig('image'));
|
|
172
|
+
|
|
173
|
+
const provider = (modelId: CHAT_MODEL_IDS) => createLanguageModel(modelId);
|
|
174
|
+
|
|
175
|
+
provider.specificationVersion = 'v3' as const;
|
|
176
|
+
provider.languageModel = createLanguageModel;
|
|
177
|
+
provider.chatModel = createChatModel;
|
|
178
|
+
provider.completionModel = createCompletionModel;
|
|
179
|
+
provider.embeddingModel = createEmbeddingModel;
|
|
180
|
+
provider.textEmbeddingModel = createEmbeddingModel;
|
|
181
|
+
provider.imageModel = createImageModel;
|
|
182
|
+
|
|
183
|
+
return provider as OpenAICompatibleProvider<
|
|
184
|
+
CHAT_MODEL_IDS,
|
|
185
|
+
COMPLETION_MODEL_IDS,
|
|
186
|
+
EMBEDDING_MODEL_IDS,
|
|
187
|
+
IMAGE_MODEL_IDS
|
|
188
|
+
>;
|
|
189
|
+
}
|