@amanm/vertex-maas 0.0.1
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 +5 -0
- package/README.md +35 -0
- package/dist/index.d.mts +64 -0
- package/dist/index.d.ts +64 -0
- package/dist/index.js +190 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +170 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +72 -0
- package/src/index.ts +6 -0
- package/src/version.ts +6 -0
- package/src/vertex-maas-auth-fetch.ts +35 -0
- package/src/vertex-maas-auth-google-auth-library.ts +27 -0
- package/src/vertex-maas-base-url.ts +25 -0
- package/src/vertex-maas-provider.ts +217 -0
package/CHANGELOG.md
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# @ai-sdk/vertex-maas
|
|
2
|
+
|
|
3
|
+
Vertex MaaS provider for the Vercel AI SDK.
|
|
4
|
+
|
|
5
|
+
This provider targets **Vertex AI models exposed via OpenAI-compatible endpoints** and authenticates using **Google Application Default Credentials (ADC)** (service account JSON via `GOOGLE_APPLICATION_CREDENTIALS`, workload identity, etc.).
|
|
6
|
+
|
|
7
|
+
## Requirements
|
|
8
|
+
|
|
9
|
+
- Node.js runtime (not Edge)
|
|
10
|
+
- ADC configured (commonly via `GOOGLE_APPLICATION_CREDENTIALS`)
|
|
11
|
+
- Set `GOOGLE_VERTEX_PROJECT` and `GOOGLE_VERTEX_LOCATION` (or pass `project`/`location` to `createVertexMaaS`)
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { generateText } from 'ai';
|
|
17
|
+
import { createVertexMaaS } from '@amanm/vertex-maas';
|
|
18
|
+
|
|
19
|
+
const vertex = createVertexMaaS({
|
|
20
|
+
project: process.env.GOOGLE_VERTEX_PROJECT!,
|
|
21
|
+
location: process.env.GOOGLE_VERTEX_LOCATION!, // e.g. "global" or "us-central1"
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
const result = await generateText({
|
|
25
|
+
model: vertex('glm-4.7'),
|
|
26
|
+
prompt: 'Explain Amdahl\\'s Law in simple terms.',
|
|
27
|
+
});
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Settings
|
|
31
|
+
|
|
32
|
+
- `baseURL`: Optional. If set, used directly.
|
|
33
|
+
- `project` / `location`: Used to compute the Vertex OpenAI-compatible base URL if `baseURL` is not provided.
|
|
34
|
+
- `apiKey`: Optional. If set, uses a static `Authorization: Bearer <apiKey>` header (no ADC).
|
|
35
|
+
- `googleAuthOptions`: Optional. Passed to `google-auth-library` `GoogleAuth`.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { MetadataExtractor, OpenAICompatibleProvider } from '@ai-sdk/openai-compatible';
|
|
2
|
+
import { FetchFunction } from '@ai-sdk/provider-utils';
|
|
3
|
+
import { GoogleAuthOptions } from 'google-auth-library';
|
|
4
|
+
|
|
5
|
+
interface VertexMaaSProviderSettings {
|
|
6
|
+
/**
|
|
7
|
+
* Base URL for the API calls. If not set, it is derived from `project` and
|
|
8
|
+
* `location`.
|
|
9
|
+
*/
|
|
10
|
+
baseURL?: string;
|
|
11
|
+
/**
|
|
12
|
+
* Vertex project id. Defaults to `GOOGLE_VERTEX_PROJECT` if `baseURL` is not
|
|
13
|
+
* provided.
|
|
14
|
+
*/
|
|
15
|
+
project?: string;
|
|
16
|
+
/**
|
|
17
|
+
* Vertex location. Defaults to `GOOGLE_VERTEX_LOCATION` if `baseURL` is not
|
|
18
|
+
* provided.
|
|
19
|
+
*/
|
|
20
|
+
location?: string;
|
|
21
|
+
/**
|
|
22
|
+
* API version used when computing baseURL. Defaults to `v1beta1`.
|
|
23
|
+
*/
|
|
24
|
+
apiVersion?: 'v1' | 'v1beta1';
|
|
25
|
+
/**
|
|
26
|
+
* Override the base host for Vertex API calls (e.g. "https://aiplatform.googleapis.com").
|
|
27
|
+
* If not set, it is computed from location.
|
|
28
|
+
*/
|
|
29
|
+
apiBase?: string;
|
|
30
|
+
/**
|
|
31
|
+
* Optional. Use a static bearer token instead of ADC.
|
|
32
|
+
*/
|
|
33
|
+
apiKey?: string;
|
|
34
|
+
/**
|
|
35
|
+
* Optional. Passed to google-auth-library for ADC token generation.
|
|
36
|
+
*/
|
|
37
|
+
googleAuthOptions?: GoogleAuthOptions;
|
|
38
|
+
/**
|
|
39
|
+
* Optional custom headers.
|
|
40
|
+
*/
|
|
41
|
+
headers?: Record<string, string>;
|
|
42
|
+
/**
|
|
43
|
+
* Optional custom url query parameters to include in request urls.
|
|
44
|
+
*/
|
|
45
|
+
queryParams?: Record<string, string>;
|
|
46
|
+
/**
|
|
47
|
+
* Custom fetch implementation.
|
|
48
|
+
*/
|
|
49
|
+
fetch?: FetchFunction;
|
|
50
|
+
includeUsage?: boolean;
|
|
51
|
+
supportsStructuredOutputs?: boolean;
|
|
52
|
+
transformRequestBody?: (args: Record<string, any>) => Record<string, any>;
|
|
53
|
+
metadataExtractor?: MetadataExtractor;
|
|
54
|
+
}
|
|
55
|
+
type VertexMaaSProvider = OpenAICompatibleProvider;
|
|
56
|
+
declare function createVertexMaaS(options?: VertexMaaSProviderSettings): VertexMaaSProvider;
|
|
57
|
+
/**
|
|
58
|
+
* Default Vertex MaaS provider instance.
|
|
59
|
+
*/
|
|
60
|
+
declare const vertexMaaS: VertexMaaSProvider;
|
|
61
|
+
|
|
62
|
+
declare const VERSION: string;
|
|
63
|
+
|
|
64
|
+
export { VERSION, type VertexMaaSProvider, type VertexMaaSProviderSettings, createVertexMaaS, vertexMaaS };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { MetadataExtractor, OpenAICompatibleProvider } from '@ai-sdk/openai-compatible';
|
|
2
|
+
import { FetchFunction } from '@ai-sdk/provider-utils';
|
|
3
|
+
import { GoogleAuthOptions } from 'google-auth-library';
|
|
4
|
+
|
|
5
|
+
interface VertexMaaSProviderSettings {
|
|
6
|
+
/**
|
|
7
|
+
* Base URL for the API calls. If not set, it is derived from `project` and
|
|
8
|
+
* `location`.
|
|
9
|
+
*/
|
|
10
|
+
baseURL?: string;
|
|
11
|
+
/**
|
|
12
|
+
* Vertex project id. Defaults to `GOOGLE_VERTEX_PROJECT` if `baseURL` is not
|
|
13
|
+
* provided.
|
|
14
|
+
*/
|
|
15
|
+
project?: string;
|
|
16
|
+
/**
|
|
17
|
+
* Vertex location. Defaults to `GOOGLE_VERTEX_LOCATION` if `baseURL` is not
|
|
18
|
+
* provided.
|
|
19
|
+
*/
|
|
20
|
+
location?: string;
|
|
21
|
+
/**
|
|
22
|
+
* API version used when computing baseURL. Defaults to `v1beta1`.
|
|
23
|
+
*/
|
|
24
|
+
apiVersion?: 'v1' | 'v1beta1';
|
|
25
|
+
/**
|
|
26
|
+
* Override the base host for Vertex API calls (e.g. "https://aiplatform.googleapis.com").
|
|
27
|
+
* If not set, it is computed from location.
|
|
28
|
+
*/
|
|
29
|
+
apiBase?: string;
|
|
30
|
+
/**
|
|
31
|
+
* Optional. Use a static bearer token instead of ADC.
|
|
32
|
+
*/
|
|
33
|
+
apiKey?: string;
|
|
34
|
+
/**
|
|
35
|
+
* Optional. Passed to google-auth-library for ADC token generation.
|
|
36
|
+
*/
|
|
37
|
+
googleAuthOptions?: GoogleAuthOptions;
|
|
38
|
+
/**
|
|
39
|
+
* Optional custom headers.
|
|
40
|
+
*/
|
|
41
|
+
headers?: Record<string, string>;
|
|
42
|
+
/**
|
|
43
|
+
* Optional custom url query parameters to include in request urls.
|
|
44
|
+
*/
|
|
45
|
+
queryParams?: Record<string, string>;
|
|
46
|
+
/**
|
|
47
|
+
* Custom fetch implementation.
|
|
48
|
+
*/
|
|
49
|
+
fetch?: FetchFunction;
|
|
50
|
+
includeUsage?: boolean;
|
|
51
|
+
supportsStructuredOutputs?: boolean;
|
|
52
|
+
transformRequestBody?: (args: Record<string, any>) => Record<string, any>;
|
|
53
|
+
metadataExtractor?: MetadataExtractor;
|
|
54
|
+
}
|
|
55
|
+
type VertexMaaSProvider = OpenAICompatibleProvider;
|
|
56
|
+
declare function createVertexMaaS(options?: VertexMaaSProviderSettings): VertexMaaSProvider;
|
|
57
|
+
/**
|
|
58
|
+
* Default Vertex MaaS provider instance.
|
|
59
|
+
*/
|
|
60
|
+
declare const vertexMaaS: VertexMaaSProvider;
|
|
61
|
+
|
|
62
|
+
declare const VERSION: string;
|
|
63
|
+
|
|
64
|
+
export { VERSION, type VertexMaaSProvider, type VertexMaaSProviderSettings, createVertexMaaS, vertexMaaS };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
VERSION: () => VERSION,
|
|
24
|
+
createVertexMaaS: () => createVertexMaaS,
|
|
25
|
+
vertexMaaS: () => vertexMaaS
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(src_exports);
|
|
28
|
+
|
|
29
|
+
// src/vertex-maas-provider.ts
|
|
30
|
+
var import_openai_compatible = require("@ai-sdk/openai-compatible");
|
|
31
|
+
var import_provider_utils3 = require("@ai-sdk/provider-utils");
|
|
32
|
+
|
|
33
|
+
// src/version.ts
|
|
34
|
+
var VERSION = true ? "0.0.1" : "0.0.0-test";
|
|
35
|
+
|
|
36
|
+
// src/vertex-maas-auth-fetch.ts
|
|
37
|
+
var import_provider_utils = require("@ai-sdk/provider-utils");
|
|
38
|
+
|
|
39
|
+
// src/vertex-maas-auth-google-auth-library.ts
|
|
40
|
+
var import_google_auth_library = require("google-auth-library");
|
|
41
|
+
var authInstance = null;
|
|
42
|
+
var authOptions = null;
|
|
43
|
+
function getAuth(options) {
|
|
44
|
+
if (!authInstance || options !== authOptions) {
|
|
45
|
+
authInstance = new import_google_auth_library.GoogleAuth({
|
|
46
|
+
scopes: ["https://www.googleapis.com/auth/cloud-platform"],
|
|
47
|
+
...options
|
|
48
|
+
});
|
|
49
|
+
authOptions = options;
|
|
50
|
+
}
|
|
51
|
+
return authInstance;
|
|
52
|
+
}
|
|
53
|
+
async function generateAuthToken(options) {
|
|
54
|
+
const auth = getAuth(options || {});
|
|
55
|
+
const client = await auth.getClient();
|
|
56
|
+
const token = await client.getAccessToken();
|
|
57
|
+
return (token == null ? void 0 : token.token) || null;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// src/vertex-maas-auth-fetch.ts
|
|
61
|
+
function createVertexMaaSAuthFetch({
|
|
62
|
+
customFetch,
|
|
63
|
+
googleAuthOptions
|
|
64
|
+
}) {
|
|
65
|
+
return async (url, init) => {
|
|
66
|
+
const existingHeaders = (0, import_provider_utils.normalizeHeaders)(init == null ? void 0 : init.headers);
|
|
67
|
+
if (existingHeaders.authorization != null) {
|
|
68
|
+
return (customFetch != null ? customFetch : fetch)(url, init);
|
|
69
|
+
}
|
|
70
|
+
const token = await generateAuthToken(googleAuthOptions);
|
|
71
|
+
if (!token) {
|
|
72
|
+
throw new Error("Failed to get access token from ADC");
|
|
73
|
+
}
|
|
74
|
+
const modifiedInit = {
|
|
75
|
+
...init,
|
|
76
|
+
headers: {
|
|
77
|
+
...existingHeaders,
|
|
78
|
+
authorization: `Bearer ${token}`
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
return (customFetch != null ? customFetch : fetch)(url, modifiedInit);
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// src/vertex-maas-base-url.ts
|
|
86
|
+
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
|
87
|
+
function getDefaultVertexApiBase(location) {
|
|
88
|
+
return location === "global" ? "https://aiplatform.googleapis.com" : `https://${location}-aiplatform.googleapis.com`;
|
|
89
|
+
}
|
|
90
|
+
function getVertexOpenAICompatibleBaseURL({
|
|
91
|
+
project,
|
|
92
|
+
location,
|
|
93
|
+
apiVersion,
|
|
94
|
+
apiBase
|
|
95
|
+
}) {
|
|
96
|
+
const resolvedApiBase = (0, import_provider_utils2.withoutTrailingSlash)(
|
|
97
|
+
apiBase != null ? apiBase : getDefaultVertexApiBase(location)
|
|
98
|
+
);
|
|
99
|
+
return `${resolvedApiBase}/${apiVersion}/projects/${project}/locations/${location}/endpoints/openapi`;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// src/vertex-maas-provider.ts
|
|
103
|
+
function createVertexMaaS(options = {}) {
|
|
104
|
+
const providerName = "vertexMaas";
|
|
105
|
+
let baseURLCache = null;
|
|
106
|
+
const getBaseURL = () => {
|
|
107
|
+
var _a, _b;
|
|
108
|
+
if (baseURLCache != null) {
|
|
109
|
+
return baseURLCache;
|
|
110
|
+
}
|
|
111
|
+
baseURLCache = (_b = (0, import_provider_utils3.withoutTrailingSlash)(options.baseURL)) != null ? _b : getVertexOpenAICompatibleBaseURL({
|
|
112
|
+
project: (0, import_provider_utils3.loadSetting)({
|
|
113
|
+
settingValue: options.project,
|
|
114
|
+
settingName: "project",
|
|
115
|
+
environmentVariableName: "GOOGLE_VERTEX_PROJECT",
|
|
116
|
+
description: "Google Vertex project"
|
|
117
|
+
}),
|
|
118
|
+
location: (0, import_provider_utils3.loadSetting)({
|
|
119
|
+
settingValue: options.location,
|
|
120
|
+
settingName: "location",
|
|
121
|
+
environmentVariableName: "GOOGLE_VERTEX_LOCATION",
|
|
122
|
+
description: "Google Vertex location"
|
|
123
|
+
}),
|
|
124
|
+
apiVersion: (_a = options.apiVersion) != null ? _a : "v1beta1",
|
|
125
|
+
apiBase: options.apiBase
|
|
126
|
+
});
|
|
127
|
+
return baseURLCache;
|
|
128
|
+
};
|
|
129
|
+
const staticHeaders = {
|
|
130
|
+
...options.apiKey && { Authorization: `Bearer ${options.apiKey}` },
|
|
131
|
+
...options.headers
|
|
132
|
+
};
|
|
133
|
+
const getHeaders = () => (0, import_provider_utils3.withUserAgentSuffix)(staticHeaders, `ai-sdk/vertex-maas/${VERSION}`);
|
|
134
|
+
const fetch2 = options.apiKey != null ? options.fetch : createVertexMaaSAuthFetch({
|
|
135
|
+
customFetch: options.fetch,
|
|
136
|
+
googleAuthOptions: options.googleAuthOptions
|
|
137
|
+
});
|
|
138
|
+
const getCommonModelConfig = (modelType) => ({
|
|
139
|
+
provider: `${providerName}.${modelType}`,
|
|
140
|
+
url: ({ path }) => {
|
|
141
|
+
const url = new URL(`${getBaseURL()}${path}`);
|
|
142
|
+
if (options.queryParams) {
|
|
143
|
+
url.search = new URLSearchParams(options.queryParams).toString();
|
|
144
|
+
}
|
|
145
|
+
return url.toString();
|
|
146
|
+
},
|
|
147
|
+
headers: getHeaders,
|
|
148
|
+
fetch: fetch2
|
|
149
|
+
});
|
|
150
|
+
const createLanguageModel = (modelId) => createChatModel(modelId);
|
|
151
|
+
const createChatModel = (modelId) => new import_openai_compatible.OpenAICompatibleChatLanguageModel(modelId, {
|
|
152
|
+
...getCommonModelConfig("chat"),
|
|
153
|
+
includeUsage: options.includeUsage,
|
|
154
|
+
supportsStructuredOutputs: options.supportsStructuredOutputs,
|
|
155
|
+
transformRequestBody: options.transformRequestBody,
|
|
156
|
+
metadataExtractor: options.metadataExtractor
|
|
157
|
+
});
|
|
158
|
+
const createCompletionModel = (modelId) => new import_openai_compatible.OpenAICompatibleCompletionLanguageModel(modelId, {
|
|
159
|
+
...getCommonModelConfig("completion"),
|
|
160
|
+
includeUsage: options.includeUsage
|
|
161
|
+
});
|
|
162
|
+
const createEmbeddingModel = (modelId) => new import_openai_compatible.OpenAICompatibleEmbeddingModel(modelId, {
|
|
163
|
+
...getCommonModelConfig("embedding")
|
|
164
|
+
});
|
|
165
|
+
const createImageModel = (modelId) => new import_openai_compatible.OpenAICompatibleImageModel(modelId, getCommonModelConfig("image"));
|
|
166
|
+
const provider = (modelId) => createLanguageModel(modelId);
|
|
167
|
+
provider.specificationVersion = "v3";
|
|
168
|
+
provider.languageModel = (modelId, config) => new import_openai_compatible.OpenAICompatibleChatLanguageModel(modelId, {
|
|
169
|
+
...getCommonModelConfig("chat"),
|
|
170
|
+
includeUsage: options.includeUsage,
|
|
171
|
+
supportsStructuredOutputs: options.supportsStructuredOutputs,
|
|
172
|
+
transformRequestBody: options.transformRequestBody,
|
|
173
|
+
metadataExtractor: options.metadataExtractor,
|
|
174
|
+
...config
|
|
175
|
+
});
|
|
176
|
+
provider.chatModel = createChatModel;
|
|
177
|
+
provider.completionModel = createCompletionModel;
|
|
178
|
+
provider.embeddingModel = createEmbeddingModel;
|
|
179
|
+
provider.textEmbeddingModel = createEmbeddingModel;
|
|
180
|
+
provider.imageModel = createImageModel;
|
|
181
|
+
return provider;
|
|
182
|
+
}
|
|
183
|
+
var vertexMaaS = createVertexMaaS();
|
|
184
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
185
|
+
0 && (module.exports = {
|
|
186
|
+
VERSION,
|
|
187
|
+
createVertexMaaS,
|
|
188
|
+
vertexMaaS
|
|
189
|
+
});
|
|
190
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/vertex-maas-provider.ts","../src/version.ts","../src/vertex-maas-auth-fetch.ts","../src/vertex-maas-auth-google-auth-library.ts","../src/vertex-maas-base-url.ts"],"sourcesContent":["export { createVertexMaaS, vertexMaaS } from './vertex-maas-provider';\nexport type {\n VertexMaaSProvider,\n VertexMaaSProviderSettings,\n} from './vertex-maas-provider';\nexport { VERSION } from './version';\n","import {\n MetadataExtractor,\n OpenAICompatibleProvider,\n OpenAICompatibleChatLanguageModel,\n OpenAICompatibleCompletionLanguageModel,\n OpenAICompatibleEmbeddingModel,\n OpenAICompatibleImageModel,\n} from '@ai-sdk/openai-compatible';\nimport {\n FetchFunction,\n loadSetting,\n withoutTrailingSlash,\n withUserAgentSuffix,\n} from '@ai-sdk/provider-utils';\nimport type { GoogleAuthOptions } from 'google-auth-library';\nimport { VERSION } from './version';\nimport { createVertexMaaSAuthFetch } from './vertex-maas-auth-fetch';\nimport { getVertexOpenAICompatibleBaseURL } from './vertex-maas-base-url';\nimport type {\n EmbeddingModelV3,\n ImageModelV3,\n LanguageModelV3,\n} from '@ai-sdk/provider';\n\ntype OpenAICompatibleChatConfig = ConstructorParameters<\n typeof OpenAICompatibleChatLanguageModel\n>[1];\n\nexport interface VertexMaaSProviderSettings {\n /**\n * Base URL for the API calls. If not set, it is derived from `project` and\n * `location`.\n */\n baseURL?: string;\n\n /**\n * Vertex project id. Defaults to `GOOGLE_VERTEX_PROJECT` if `baseURL` is not\n * provided.\n */\n project?: string;\n\n /**\n * Vertex location. Defaults to `GOOGLE_VERTEX_LOCATION` if `baseURL` is not\n * provided.\n */\n location?: string;\n\n /**\n * API version used when computing baseURL. Defaults to `v1beta1`.\n */\n apiVersion?: 'v1' | 'v1beta1';\n\n /**\n * Override the base host for Vertex API calls (e.g. \"https://aiplatform.googleapis.com\").\n * If not set, it is computed from location.\n */\n apiBase?: string;\n\n /**\n * Optional. Use a static bearer token instead of ADC.\n */\n apiKey?: string;\n\n /**\n * Optional. Passed to google-auth-library for ADC token generation.\n */\n googleAuthOptions?: GoogleAuthOptions;\n\n /**\n * Optional custom headers.\n */\n headers?: Record<string, string>;\n\n /**\n * Optional custom url query parameters to include in request urls.\n */\n queryParams?: Record<string, string>;\n\n /**\n * Custom fetch implementation.\n */\n fetch?: FetchFunction;\n\n includeUsage?: boolean;\n supportsStructuredOutputs?: boolean;\n transformRequestBody?: (args: Record<string, any>) => Record<string, any>;\n metadataExtractor?: MetadataExtractor;\n}\n\nexport type VertexMaaSProvider = OpenAICompatibleProvider;\n\nexport function createVertexMaaS(\n options: VertexMaaSProviderSettings = {},\n): VertexMaaSProvider {\n const providerName = 'vertexMaas';\n\n let baseURLCache: string | null = null;\n const getBaseURL = () => {\n if (baseURLCache != null) {\n return baseURLCache;\n }\n\n baseURLCache =\n withoutTrailingSlash(options.baseURL) ??\n getVertexOpenAICompatibleBaseURL({\n project: loadSetting({\n settingValue: options.project,\n settingName: 'project',\n environmentVariableName: 'GOOGLE_VERTEX_PROJECT',\n description: 'Google Vertex project',\n }),\n location: loadSetting({\n settingValue: options.location,\n settingName: 'location',\n environmentVariableName: 'GOOGLE_VERTEX_LOCATION',\n description: 'Google Vertex location',\n }),\n apiVersion: options.apiVersion ?? 'v1beta1',\n apiBase: options.apiBase,\n });\n\n return baseURLCache;\n };\n\n interface CommonModelConfig {\n provider: string;\n url: ({ path }: { path: string }) => string;\n headers: () => Record<string, string>;\n fetch?: FetchFunction;\n }\n\n const staticHeaders = {\n ...(options.apiKey && { Authorization: `Bearer ${options.apiKey}` }),\n ...options.headers,\n };\n\n const getHeaders = () =>\n withUserAgentSuffix(staticHeaders, `ai-sdk/vertex-maas/${VERSION}`);\n\n const fetch =\n options.apiKey != null\n ? options.fetch\n : createVertexMaaSAuthFetch({\n customFetch: options.fetch,\n googleAuthOptions: options.googleAuthOptions,\n });\n\n const getCommonModelConfig = (modelType: string): CommonModelConfig => ({\n provider: `${providerName}.${modelType}`,\n url: ({ path }) => {\n const url = new URL(`${getBaseURL()}${path}`);\n if (options.queryParams) {\n url.search = new URLSearchParams(options.queryParams).toString();\n }\n return url.toString();\n },\n headers: getHeaders,\n fetch,\n });\n\n const createLanguageModel = (modelId: string) => createChatModel(modelId);\n\n const createChatModel = (modelId: string) =>\n new OpenAICompatibleChatLanguageModel(modelId, {\n ...getCommonModelConfig('chat'),\n includeUsage: options.includeUsage,\n supportsStructuredOutputs: options.supportsStructuredOutputs,\n transformRequestBody: options.transformRequestBody,\n metadataExtractor: options.metadataExtractor,\n });\n\n const createCompletionModel = (modelId: string) =>\n new OpenAICompatibleCompletionLanguageModel(modelId, {\n ...getCommonModelConfig('completion'),\n includeUsage: options.includeUsage,\n });\n\n const createEmbeddingModel = (modelId: string) =>\n new OpenAICompatibleEmbeddingModel(modelId, {\n ...getCommonModelConfig('embedding'),\n });\n\n const createImageModel = (modelId: string) =>\n new OpenAICompatibleImageModel(modelId, getCommonModelConfig('image'));\n\n const provider = (modelId: string) => createLanguageModel(modelId);\n\n provider.specificationVersion = 'v3' as const;\n provider.languageModel = (\n modelId: string,\n config?: Partial<OpenAICompatibleChatConfig>,\n ): LanguageModelV3 =>\n new OpenAICompatibleChatLanguageModel(modelId, {\n ...getCommonModelConfig('chat'),\n includeUsage: options.includeUsage,\n supportsStructuredOutputs: options.supportsStructuredOutputs,\n transformRequestBody: options.transformRequestBody,\n metadataExtractor: options.metadataExtractor,\n ...config,\n });\n provider.chatModel = createChatModel;\n provider.completionModel = createCompletionModel;\n provider.embeddingModel = createEmbeddingModel as (\n modelId: string,\n ) => EmbeddingModelV3;\n provider.textEmbeddingModel = createEmbeddingModel as (\n modelId: string,\n ) => EmbeddingModelV3;\n provider.imageModel = createImageModel as (modelId: string) => ImageModelV3;\n\n return provider as unknown as OpenAICompatibleProvider;\n}\n\n/**\n * Default Vertex MaaS provider instance.\n */\nexport const vertexMaaS = createVertexMaaS();\n","// Version string of this package injected at build time.\ndeclare const __PACKAGE_VERSION__: string | undefined;\nexport const VERSION: string =\n typeof __PACKAGE_VERSION__ !== 'undefined'\n ? __PACKAGE_VERSION__\n : '0.0.0-test';\n","import { FetchFunction, normalizeHeaders } from '@ai-sdk/provider-utils';\nimport type { GoogleAuthOptions } from 'google-auth-library';\nimport { generateAuthToken } from './vertex-maas-auth-google-auth-library';\n\nexport function createVertexMaaSAuthFetch({\n customFetch,\n googleAuthOptions,\n}: {\n customFetch?: FetchFunction;\n googleAuthOptions?: GoogleAuthOptions;\n}): FetchFunction {\n return async (url, init) => {\n const existingHeaders = normalizeHeaders(init?.headers as any);\n\n // Respect caller-provided auth (including static api keys).\n if (existingHeaders.authorization != null) {\n return (customFetch ?? fetch)(url as any, init);\n }\n\n const token = await generateAuthToken(googleAuthOptions);\n if (!token) {\n throw new Error('Failed to get access token from ADC');\n }\n\n const modifiedInit: RequestInit = {\n ...init,\n headers: {\n ...existingHeaders,\n authorization: `Bearer ${token}`,\n },\n };\n\n return (customFetch ?? fetch)(url as any, modifiedInit);\n };\n}\n","import { GoogleAuth, GoogleAuthOptions } from 'google-auth-library';\n\nlet authInstance: GoogleAuth | null = null;\nlet authOptions: GoogleAuthOptions | null = null;\n\nfunction getAuth(options: GoogleAuthOptions) {\n if (!authInstance || options !== authOptions) {\n authInstance = new GoogleAuth({\n scopes: ['https://www.googleapis.com/auth/cloud-platform'],\n ...options,\n });\n authOptions = options;\n }\n return authInstance;\n}\n\nexport async function generateAuthToken(options?: GoogleAuthOptions) {\n const auth = getAuth(options || {});\n const client = await auth.getClient();\n const token = await client.getAccessToken();\n return token?.token || null;\n}\n\n// For testing purposes only\nexport function _resetAuthInstance() {\n authInstance = null;\n}\n","import { withoutTrailingSlash } from '@ai-sdk/provider-utils';\n\nfunction getDefaultVertexApiBase(location: string): string {\n return location === 'global'\n ? 'https://aiplatform.googleapis.com'\n : `https://${location}-aiplatform.googleapis.com`;\n}\n\nexport function getVertexOpenAICompatibleBaseURL({\n project,\n location,\n apiVersion,\n apiBase,\n}: {\n project: string;\n location: string;\n apiVersion: 'v1' | 'v1beta1';\n apiBase?: string;\n}): string {\n const resolvedApiBase = withoutTrailingSlash(\n apiBase ?? getDefaultVertexApiBase(location),\n );\n\n return `${resolvedApiBase}/${apiVersion}/projects/${project}/locations/${location}/endpoints/openapi`;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,+BAOO;AACP,IAAAA,yBAKO;;;ACXA,IAAM,UACX,OACI,UACA;;;ACLN,4BAAgD;;;ACAhD,iCAA8C;AAE9C,IAAI,eAAkC;AACtC,IAAI,cAAwC;AAE5C,SAAS,QAAQ,SAA4B;AAC3C,MAAI,CAAC,gBAAgB,YAAY,aAAa;AAC5C,mBAAe,IAAI,sCAAW;AAAA,MAC5B,QAAQ,CAAC,gDAAgD;AAAA,MACzD,GAAG;AAAA,IACL,CAAC;AACD,kBAAc;AAAA,EAChB;AACA,SAAO;AACT;AAEA,eAAsB,kBAAkB,SAA6B;AACnE,QAAM,OAAO,QAAQ,WAAW,CAAC,CAAC;AAClC,QAAM,SAAS,MAAM,KAAK,UAAU;AACpC,QAAM,QAAQ,MAAM,OAAO,eAAe;AAC1C,UAAO,+BAAO,UAAS;AACzB;;;ADjBO,SAAS,0BAA0B;AAAA,EACxC;AAAA,EACA;AACF,GAGkB;AAChB,SAAO,OAAO,KAAK,SAAS;AAC1B,UAAM,sBAAkB,wCAAiB,6BAAM,OAAc;AAG7D,QAAI,gBAAgB,iBAAiB,MAAM;AACzC,cAAQ,oCAAe,OAAO,KAAY,IAAI;AAAA,IAChD;AAEA,UAAM,QAAQ,MAAM,kBAAkB,iBAAiB;AACvD,QAAI,CAAC,OAAO;AACV,YAAM,IAAI,MAAM,qCAAqC;AAAA,IACvD;AAEA,UAAM,eAA4B;AAAA,MAChC,GAAG;AAAA,MACH,SAAS;AAAA,QACP,GAAG;AAAA,QACH,eAAe,UAAU,KAAK;AAAA,MAChC;AAAA,IACF;AAE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G;AAAA,EACL,CAAC;AACH,WAAS,YAAY;AACrB,WAAS,kBAAkB;AAC3B,WAAS,iBAAiB;AAG1B,WAAS,qBAAqB;AAG9B,WAAS,aAAa;AAEtB,SAAO;AACT;AAKO,IAAM,aAAa,iBAAiB;","names":["import_provider_utils","import_provider_utils","fetch"]}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
// src/vertex-maas-provider.ts
|
|
2
|
+
import {
|
|
3
|
+
OpenAICompatibleChatLanguageModel,
|
|
4
|
+
OpenAICompatibleCompletionLanguageModel,
|
|
5
|
+
OpenAICompatibleEmbeddingModel,
|
|
6
|
+
OpenAICompatibleImageModel
|
|
7
|
+
} from "@ai-sdk/openai-compatible";
|
|
8
|
+
import {
|
|
9
|
+
loadSetting,
|
|
10
|
+
withoutTrailingSlash as withoutTrailingSlash2,
|
|
11
|
+
withUserAgentSuffix
|
|
12
|
+
} from "@ai-sdk/provider-utils";
|
|
13
|
+
|
|
14
|
+
// src/version.ts
|
|
15
|
+
var VERSION = true ? "0.0.1" : "0.0.0-test";
|
|
16
|
+
|
|
17
|
+
// src/vertex-maas-auth-fetch.ts
|
|
18
|
+
import { normalizeHeaders } from "@ai-sdk/provider-utils";
|
|
19
|
+
|
|
20
|
+
// src/vertex-maas-auth-google-auth-library.ts
|
|
21
|
+
import { GoogleAuth } from "google-auth-library";
|
|
22
|
+
var authInstance = null;
|
|
23
|
+
var authOptions = null;
|
|
24
|
+
function getAuth(options) {
|
|
25
|
+
if (!authInstance || options !== authOptions) {
|
|
26
|
+
authInstance = new GoogleAuth({
|
|
27
|
+
scopes: ["https://www.googleapis.com/auth/cloud-platform"],
|
|
28
|
+
...options
|
|
29
|
+
});
|
|
30
|
+
authOptions = options;
|
|
31
|
+
}
|
|
32
|
+
return authInstance;
|
|
33
|
+
}
|
|
34
|
+
async function generateAuthToken(options) {
|
|
35
|
+
const auth = getAuth(options || {});
|
|
36
|
+
const client = await auth.getClient();
|
|
37
|
+
const token = await client.getAccessToken();
|
|
38
|
+
return (token == null ? void 0 : token.token) || null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// src/vertex-maas-auth-fetch.ts
|
|
42
|
+
function createVertexMaaSAuthFetch({
|
|
43
|
+
customFetch,
|
|
44
|
+
googleAuthOptions
|
|
45
|
+
}) {
|
|
46
|
+
return async (url, init) => {
|
|
47
|
+
const existingHeaders = normalizeHeaders(init == null ? void 0 : init.headers);
|
|
48
|
+
if (existingHeaders.authorization != null) {
|
|
49
|
+
return (customFetch != null ? customFetch : fetch)(url, init);
|
|
50
|
+
}
|
|
51
|
+
const token = await generateAuthToken(googleAuthOptions);
|
|
52
|
+
if (!token) {
|
|
53
|
+
throw new Error("Failed to get access token from ADC");
|
|
54
|
+
}
|
|
55
|
+
const modifiedInit = {
|
|
56
|
+
...init,
|
|
57
|
+
headers: {
|
|
58
|
+
...existingHeaders,
|
|
59
|
+
authorization: `Bearer ${token}`
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
return (customFetch != null ? customFetch : fetch)(url, modifiedInit);
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// src/vertex-maas-base-url.ts
|
|
67
|
+
import { withoutTrailingSlash } from "@ai-sdk/provider-utils";
|
|
68
|
+
function getDefaultVertexApiBase(location) {
|
|
69
|
+
return location === "global" ? "https://aiplatform.googleapis.com" : `https://${location}-aiplatform.googleapis.com`;
|
|
70
|
+
}
|
|
71
|
+
function getVertexOpenAICompatibleBaseURL({
|
|
72
|
+
project,
|
|
73
|
+
location,
|
|
74
|
+
apiVersion,
|
|
75
|
+
apiBase
|
|
76
|
+
}) {
|
|
77
|
+
const resolvedApiBase = withoutTrailingSlash(
|
|
78
|
+
apiBase != null ? apiBase : getDefaultVertexApiBase(location)
|
|
79
|
+
);
|
|
80
|
+
return `${resolvedApiBase}/${apiVersion}/projects/${project}/locations/${location}/endpoints/openapi`;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// src/vertex-maas-provider.ts
|
|
84
|
+
function createVertexMaaS(options = {}) {
|
|
85
|
+
const providerName = "vertexMaas";
|
|
86
|
+
let baseURLCache = null;
|
|
87
|
+
const getBaseURL = () => {
|
|
88
|
+
var _a, _b;
|
|
89
|
+
if (baseURLCache != null) {
|
|
90
|
+
return baseURLCache;
|
|
91
|
+
}
|
|
92
|
+
baseURLCache = (_b = withoutTrailingSlash2(options.baseURL)) != null ? _b : getVertexOpenAICompatibleBaseURL({
|
|
93
|
+
project: loadSetting({
|
|
94
|
+
settingValue: options.project,
|
|
95
|
+
settingName: "project",
|
|
96
|
+
environmentVariableName: "GOOGLE_VERTEX_PROJECT",
|
|
97
|
+
description: "Google Vertex project"
|
|
98
|
+
}),
|
|
99
|
+
location: loadSetting({
|
|
100
|
+
settingValue: options.location,
|
|
101
|
+
settingName: "location",
|
|
102
|
+
environmentVariableName: "GOOGLE_VERTEX_LOCATION",
|
|
103
|
+
description: "Google Vertex location"
|
|
104
|
+
}),
|
|
105
|
+
apiVersion: (_a = options.apiVersion) != null ? _a : "v1beta1",
|
|
106
|
+
apiBase: options.apiBase
|
|
107
|
+
});
|
|
108
|
+
return baseURLCache;
|
|
109
|
+
};
|
|
110
|
+
const staticHeaders = {
|
|
111
|
+
...options.apiKey && { Authorization: `Bearer ${options.apiKey}` },
|
|
112
|
+
...options.headers
|
|
113
|
+
};
|
|
114
|
+
const getHeaders = () => withUserAgentSuffix(staticHeaders, `ai-sdk/vertex-maas/${VERSION}`);
|
|
115
|
+
const fetch2 = options.apiKey != null ? options.fetch : createVertexMaaSAuthFetch({
|
|
116
|
+
customFetch: options.fetch,
|
|
117
|
+
googleAuthOptions: options.googleAuthOptions
|
|
118
|
+
});
|
|
119
|
+
const getCommonModelConfig = (modelType) => ({
|
|
120
|
+
provider: `${providerName}.${modelType}`,
|
|
121
|
+
url: ({ path }) => {
|
|
122
|
+
const url = new URL(`${getBaseURL()}${path}`);
|
|
123
|
+
if (options.queryParams) {
|
|
124
|
+
url.search = new URLSearchParams(options.queryParams).toString();
|
|
125
|
+
}
|
|
126
|
+
return url.toString();
|
|
127
|
+
},
|
|
128
|
+
headers: getHeaders,
|
|
129
|
+
fetch: fetch2
|
|
130
|
+
});
|
|
131
|
+
const createLanguageModel = (modelId) => createChatModel(modelId);
|
|
132
|
+
const createChatModel = (modelId) => new OpenAICompatibleChatLanguageModel(modelId, {
|
|
133
|
+
...getCommonModelConfig("chat"),
|
|
134
|
+
includeUsage: options.includeUsage,
|
|
135
|
+
supportsStructuredOutputs: options.supportsStructuredOutputs,
|
|
136
|
+
transformRequestBody: options.transformRequestBody,
|
|
137
|
+
metadataExtractor: options.metadataExtractor
|
|
138
|
+
});
|
|
139
|
+
const createCompletionModel = (modelId) => new OpenAICompatibleCompletionLanguageModel(modelId, {
|
|
140
|
+
...getCommonModelConfig("completion"),
|
|
141
|
+
includeUsage: options.includeUsage
|
|
142
|
+
});
|
|
143
|
+
const createEmbeddingModel = (modelId) => new OpenAICompatibleEmbeddingModel(modelId, {
|
|
144
|
+
...getCommonModelConfig("embedding")
|
|
145
|
+
});
|
|
146
|
+
const createImageModel = (modelId) => new OpenAICompatibleImageModel(modelId, getCommonModelConfig("image"));
|
|
147
|
+
const provider = (modelId) => createLanguageModel(modelId);
|
|
148
|
+
provider.specificationVersion = "v3";
|
|
149
|
+
provider.languageModel = (modelId, config) => new OpenAICompatibleChatLanguageModel(modelId, {
|
|
150
|
+
...getCommonModelConfig("chat"),
|
|
151
|
+
includeUsage: options.includeUsage,
|
|
152
|
+
supportsStructuredOutputs: options.supportsStructuredOutputs,
|
|
153
|
+
transformRequestBody: options.transformRequestBody,
|
|
154
|
+
metadataExtractor: options.metadataExtractor,
|
|
155
|
+
...config
|
|
156
|
+
});
|
|
157
|
+
provider.chatModel = createChatModel;
|
|
158
|
+
provider.completionModel = createCompletionModel;
|
|
159
|
+
provider.embeddingModel = createEmbeddingModel;
|
|
160
|
+
provider.textEmbeddingModel = createEmbeddingModel;
|
|
161
|
+
provider.imageModel = createImageModel;
|
|
162
|
+
return provider;
|
|
163
|
+
}
|
|
164
|
+
var vertexMaaS = createVertexMaaS();
|
|
165
|
+
export {
|
|
166
|
+
VERSION,
|
|
167
|
+
createVertexMaaS,
|
|
168
|
+
vertexMaaS
|
|
169
|
+
};
|
|
170
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/vertex-maas-provider.ts","../src/version.ts","../src/vertex-maas-auth-fetch.ts","../src/vertex-maas-auth-google-auth-library.ts","../src/vertex-maas-base-url.ts"],"sourcesContent":["import {\n MetadataExtractor,\n OpenAICompatibleProvider,\n OpenAICompatibleChatLanguageModel,\n OpenAICompatibleCompletionLanguageModel,\n OpenAICompatibleEmbeddingModel,\n OpenAICompatibleImageModel,\n} from '@ai-sdk/openai-compatible';\nimport {\n FetchFunction,\n loadSetting,\n withoutTrailingSlash,\n withUserAgentSuffix,\n} from '@ai-sdk/provider-utils';\nimport type { GoogleAuthOptions } from 'google-auth-library';\nimport { VERSION } from './version';\nimport { createVertexMaaSAuthFetch } from './vertex-maas-auth-fetch';\nimport { getVertexOpenAICompatibleBaseURL } from './vertex-maas-base-url';\nimport type {\n EmbeddingModelV3,\n ImageModelV3,\n LanguageModelV3,\n} from '@ai-sdk/provider';\n\ntype OpenAICompatibleChatConfig = ConstructorParameters<\n typeof OpenAICompatibleChatLanguageModel\n>[1];\n\nexport interface VertexMaaSProviderSettings {\n /**\n * Base URL for the API calls. If not set, it is derived from `project` and\n * `location`.\n */\n baseURL?: string;\n\n /**\n * Vertex project id. Defaults to `GOOGLE_VERTEX_PROJECT` if `baseURL` is not\n * provided.\n */\n project?: string;\n\n /**\n * Vertex location. Defaults to `GOOGLE_VERTEX_LOCATION` if `baseURL` is not\n * provided.\n */\n location?: string;\n\n /**\n * API version used when computing baseURL. Defaults to `v1beta1`.\n */\n apiVersion?: 'v1' | 'v1beta1';\n\n /**\n * Override the base host for Vertex API calls (e.g. \"https://aiplatform.googleapis.com\").\n * If not set, it is computed from location.\n */\n apiBase?: string;\n\n /**\n * Optional. Use a static bearer token instead of ADC.\n */\n apiKey?: string;\n\n /**\n * Optional. Passed to google-auth-library for ADC token generation.\n */\n googleAuthOptions?: GoogleAuthOptions;\n\n /**\n * Optional custom headers.\n */\n headers?: Record<string, string>;\n\n /**\n * Optional custom url query parameters to include in request urls.\n */\n queryParams?: Record<string, string>;\n\n /**\n * Custom fetch implementation.\n */\n fetch?: FetchFunction;\n\n includeUsage?: boolean;\n supportsStructuredOutputs?: boolean;\n transformRequestBody?: (args: Record<string, any>) => Record<string, any>;\n metadataExtractor?: MetadataExtractor;\n}\n\nexport type VertexMaaSProvider = OpenAICompatibleProvider;\n\nexport function createVertexMaaS(\n options: VertexMaaSProviderSettings = {},\n): VertexMaaSProvider {\n const providerName = 'vertexMaas';\n\n let baseURLCache: string | null = null;\n const getBaseURL = () => {\n if (baseURLCache != null) {\n return baseURLCache;\n }\n\n baseURLCache =\n withoutTrailingSlash(options.baseURL) ??\n getVertexOpenAICompatibleBaseURL({\n project: loadSetting({\n settingValue: options.project,\n settingName: 'project',\n environmentVariableName: 'GOOGLE_VERTEX_PROJECT',\n description: 'Google Vertex project',\n }),\n location: loadSetting({\n settingValue: options.location,\n settingName: 'location',\n environmentVariableName: 'GOOGLE_VERTEX_LOCATION',\n description: 'Google Vertex location',\n }),\n apiVersion: options.apiVersion ?? 'v1beta1',\n apiBase: options.apiBase,\n });\n\n return baseURLCache;\n };\n\n interface CommonModelConfig {\n provider: string;\n url: ({ path }: { path: string }) => string;\n headers: () => Record<string, string>;\n fetch?: FetchFunction;\n }\n\n const staticHeaders = {\n ...(options.apiKey && { Authorization: `Bearer ${options.apiKey}` }),\n ...options.headers,\n };\n\n const getHeaders = () =>\n withUserAgentSuffix(staticHeaders, `ai-sdk/vertex-maas/${VERSION}`);\n\n const fetch =\n options.apiKey != null\n ? options.fetch\n : createVertexMaaSAuthFetch({\n customFetch: options.fetch,\n googleAuthOptions: options.googleAuthOptions,\n });\n\n const getCommonModelConfig = (modelType: string): CommonModelConfig => ({\n provider: `${providerName}.${modelType}`,\n url: ({ path }) => {\n const url = new URL(`${getBaseURL()}${path}`);\n if (options.queryParams) {\n url.search = new URLSearchParams(options.queryParams).toString();\n }\n return url.toString();\n },\n headers: getHeaders,\n fetch,\n });\n\n const createLanguageModel = (modelId: string) => createChatModel(modelId);\n\n const createChatModel = (modelId: string) =>\n new OpenAICompatibleChatLanguageModel(modelId, {\n ...getCommonModelConfig('chat'),\n includeUsage: options.includeUsage,\n supportsStructuredOutputs: options.supportsStructuredOutputs,\n transformRequestBody: options.transformRequestBody,\n metadataExtractor: options.metadataExtractor,\n });\n\n const createCompletionModel = (modelId: string) =>\n new OpenAICompatibleCompletionLanguageModel(modelId, {\n ...getCommonModelConfig('completion'),\n includeUsage: options.includeUsage,\n });\n\n const createEmbeddingModel = (modelId: string) =>\n new OpenAICompatibleEmbeddingModel(modelId, {\n ...getCommonModelConfig('embedding'),\n });\n\n const createImageModel = (modelId: string) =>\n new OpenAICompatibleImageModel(modelId, getCommonModelConfig('image'));\n\n const provider = (modelId: string) => createLanguageModel(modelId);\n\n provider.specificationVersion = 'v3' as const;\n provider.languageModel = (\n modelId: string,\n config?: Partial<OpenAICompatibleChatConfig>,\n ): LanguageModelV3 =>\n new OpenAICompatibleChatLanguageModel(modelId, {\n ...getCommonModelConfig('chat'),\n includeUsage: options.includeUsage,\n supportsStructuredOutputs: options.supportsStructuredOutputs,\n transformRequestBody: options.transformRequestBody,\n metadataExtractor: options.metadataExtractor,\n ...config,\n });\n provider.chatModel = createChatModel;\n provider.completionModel = createCompletionModel;\n provider.embeddingModel = createEmbeddingModel as (\n modelId: string,\n ) => EmbeddingModelV3;\n provider.textEmbeddingModel = createEmbeddingModel as (\n modelId: string,\n ) => EmbeddingModelV3;\n provider.imageModel = createImageModel as (modelId: string) => ImageModelV3;\n\n return provider as unknown as OpenAICompatibleProvider;\n}\n\n/**\n * Default Vertex MaaS provider instance.\n */\nexport const vertexMaaS = createVertexMaaS();\n","// Version string of this package injected at build time.\ndeclare const __PACKAGE_VERSION__: string | undefined;\nexport const VERSION: string =\n typeof __PACKAGE_VERSION__ !== 'undefined'\n ? __PACKAGE_VERSION__\n : '0.0.0-test';\n","import { FetchFunction, normalizeHeaders } from '@ai-sdk/provider-utils';\nimport type { GoogleAuthOptions } from 'google-auth-library';\nimport { generateAuthToken } from './vertex-maas-auth-google-auth-library';\n\nexport function createVertexMaaSAuthFetch({\n customFetch,\n googleAuthOptions,\n}: {\n customFetch?: FetchFunction;\n googleAuthOptions?: GoogleAuthOptions;\n}): FetchFunction {\n return async (url, init) => {\n const existingHeaders = normalizeHeaders(init?.headers as any);\n\n // Respect caller-provided auth (including static api keys).\n if (existingHeaders.authorization != null) {\n return (customFetch ?? fetch)(url as any, init);\n }\n\n const token = await generateAuthToken(googleAuthOptions);\n if (!token) {\n throw new Error('Failed to get access token from ADC');\n }\n\n const modifiedInit: RequestInit = {\n ...init,\n headers: {\n ...existingHeaders,\n authorization: `Bearer ${token}`,\n },\n };\n\n return (customFetch ?? fetch)(url as any, modifiedInit);\n };\n}\n","import { GoogleAuth, GoogleAuthOptions } from 'google-auth-library';\n\nlet authInstance: GoogleAuth | null = null;\nlet authOptions: GoogleAuthOptions | null = null;\n\nfunction getAuth(options: GoogleAuthOptions) {\n if (!authInstance || options !== authOptions) {\n authInstance = new GoogleAuth({\n scopes: ['https://www.googleapis.com/auth/cloud-platform'],\n ...options,\n });\n authOptions = options;\n }\n return authInstance;\n}\n\nexport async function generateAuthToken(options?: GoogleAuthOptions) {\n const auth = getAuth(options || {});\n const client = await auth.getClient();\n const token = await client.getAccessToken();\n return token?.token || null;\n}\n\n// For testing purposes only\nexport function _resetAuthInstance() {\n authInstance = null;\n}\n","import { withoutTrailingSlash } from '@ai-sdk/provider-utils';\n\nfunction getDefaultVertexApiBase(location: string): string {\n return location === 'global'\n ? 'https://aiplatform.googleapis.com'\n : `https://${location}-aiplatform.googleapis.com`;\n}\n\nexport function getVertexOpenAICompatibleBaseURL({\n project,\n location,\n apiVersion,\n apiBase,\n}: {\n project: string;\n location: string;\n apiVersion: 'v1' | 'v1beta1';\n apiBase?: string;\n}): string {\n const resolvedApiBase = withoutTrailingSlash(\n apiBase ?? getDefaultVertexApiBase(location),\n );\n\n return `${resolvedApiBase}/${apiVersion}/projects/${project}/locations/${location}/endpoints/openapi`;\n}\n"],"mappings":";AAAA;AAAA,EAGE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP;AAAA,EAEE;AAAA,EACA,wBAAAA;AAAA,EACA;AAAA,OACK;;;ACXA,IAAM,UACX,OACI,UACA;;;ACLN,SAAwB,wBAAwB;;;ACAhD,SAAS,kBAAqC;AAE9C,IAAI,eAAkC;AACtC,IAAI,cAAwC;AAE5C,SAAS,QAAQ,SAA4B;AAC3C,MAAI,CAAC,gBAAgB,YAAY,aAAa;AAC5C,mBAAe,IAAI,WAAW;AAAA,MAC5B,QAAQ,CAAC,gDAAgD;AAAA,MACzD,GAAG;AAAA,IACL,CAAC;AACD,kBAAc;AAAA,EAChB;AACA,SAAO;AACT;AAEA,eAAsB,kBAAkB,SAA6B;AACnE,QAAM,OAAO,QAAQ,WAAW,CAAC,CAAC;AAClC,QAAM,SAAS,MAAM,KAAK,UAAU;AACpC,QAAM,QAAQ,MAAM,OAAO,eAAe;AAC1C,UAAO,+BAAO,UAAS;AACzB;;;ADjBO,SAAS,0BAA0B;AAAA,EACxC;AAAA,EACA;AACF,GAGkB;AAChB,SAAO,OAAO,KAAK,SAAS;AAC1B,UAAM,kBAAkB,iBAAiB,6BAAM,OAAc;AAG7D,QAAI,gBAAgB,iBAAiB,MAAM;AACzC,cAAQ,oCAAe,OAAO,KAAY,IAAI;AAAA,IAChD;AAEA,UAAM,QAAQ,MAAM,kBAAkB,iBAAiB;AACvD,QAAI,CAAC,OAAO;AACV,YAAM,IAAI,MAAM,qCAAqC;AAAA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qB,MAAM;AAAA,IAC9B,cAAc,QAAQ;AAAA,IACtB,2BAA2B,QAAQ;AAAA,IACnC,sBAAsB,QAAQ;AAAA,IAC9B,mBAAmB,QAAQ;AAAA,IAC3B,GAAG;AAAA,EACL,CAAC;AACH,WAAS,YAAY;AACrB,WAAS,kBAAkB;AAC3B,WAAS,iBAAiB;AAG1B,WAAS,qBAAqB;AAG9B,WAAS,aAAa;AAEtB,SAAO;AACT;AAKO,IAAM,aAAa,iBAAiB;","names":["withoutTrailingSlash","withoutTrailingSlash","fetch"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@amanm/vertex-maas",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
|
+
"sideEffects": false,
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"module": "./dist/index.mjs",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"files": [
|
|
10
|
+
"dist/**/*",
|
|
11
|
+
"src",
|
|
12
|
+
"!src/**/*.test.ts",
|
|
13
|
+
"!src/**/*.test-d.ts",
|
|
14
|
+
"!src/**/__snapshots__",
|
|
15
|
+
"!src/**/__fixtures__",
|
|
16
|
+
"CHANGELOG.md",
|
|
17
|
+
"README.md"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "pnpm clean && tsup --tsconfig tsconfig.build.json",
|
|
21
|
+
"build:watch": "pnpm clean && tsup --watch",
|
|
22
|
+
"clean": "del-cli dist *.tsbuildinfo",
|
|
23
|
+
"lint": "eslint \"./**/*.ts*\"",
|
|
24
|
+
"type-check": "tsc --build",
|
|
25
|
+
"prettier-check": "prettier --check \"./**/*.ts*\"",
|
|
26
|
+
"test": "vitest --config vitest.node.config.js --run",
|
|
27
|
+
"test:update": "vitest --config vitest.node.config.js -u",
|
|
28
|
+
"test:watch": "vitest --config vitest.node.config.js"
|
|
29
|
+
},
|
|
30
|
+
"exports": {
|
|
31
|
+
"./package.json": "./package.json",
|
|
32
|
+
".": {
|
|
33
|
+
"types": "./dist/index.d.ts",
|
|
34
|
+
"import": "./dist/index.mjs",
|
|
35
|
+
"require": "./dist/index.js"
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@ai-sdk/openai-compatible": "^2.0.30",
|
|
40
|
+
"@ai-sdk/provider": "^3.0.8",
|
|
41
|
+
"@ai-sdk/provider-utils": "^4.0.15",
|
|
42
|
+
"google-auth-library": "^10.5.0"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@ai-sdk/test-server": "workspace:*",
|
|
46
|
+
"@types/node": "20.17.24",
|
|
47
|
+
"@vercel/ai-tsconfig": "workspace:*",
|
|
48
|
+
"tsup": "^8",
|
|
49
|
+
"typescript": "5.8.3",
|
|
50
|
+
"zod": "3.25.76"
|
|
51
|
+
},
|
|
52
|
+
"peerDependencies": {
|
|
53
|
+
"zod": "^3.25.76 || ^4.1.8"
|
|
54
|
+
},
|
|
55
|
+
"engines": {
|
|
56
|
+
"node": ">=18"
|
|
57
|
+
},
|
|
58
|
+
"publishConfig": {
|
|
59
|
+
"access": "public"
|
|
60
|
+
},
|
|
61
|
+
"homepage": "https://ai-sdk.dev/docs",
|
|
62
|
+
"repository": {
|
|
63
|
+
"type": "git",
|
|
64
|
+
"url": "git+https://github.com/vercel/ai.git"
|
|
65
|
+
},
|
|
66
|
+
"bugs": {
|
|
67
|
+
"url": "https://github.com/vercel/ai/issues"
|
|
68
|
+
},
|
|
69
|
+
"keywords": [
|
|
70
|
+
"ai"
|
|
71
|
+
]
|
|
72
|
+
}
|
package/src/index.ts
ADDED
package/src/version.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { FetchFunction, normalizeHeaders } from '@ai-sdk/provider-utils';
|
|
2
|
+
import type { GoogleAuthOptions } from 'google-auth-library';
|
|
3
|
+
import { generateAuthToken } from './vertex-maas-auth-google-auth-library';
|
|
4
|
+
|
|
5
|
+
export function createVertexMaaSAuthFetch({
|
|
6
|
+
customFetch,
|
|
7
|
+
googleAuthOptions,
|
|
8
|
+
}: {
|
|
9
|
+
customFetch?: FetchFunction;
|
|
10
|
+
googleAuthOptions?: GoogleAuthOptions;
|
|
11
|
+
}): FetchFunction {
|
|
12
|
+
return async (url, init) => {
|
|
13
|
+
const existingHeaders = normalizeHeaders(init?.headers as any);
|
|
14
|
+
|
|
15
|
+
// Respect caller-provided auth (including static api keys).
|
|
16
|
+
if (existingHeaders.authorization != null) {
|
|
17
|
+
return (customFetch ?? fetch)(url as any, init);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const token = await generateAuthToken(googleAuthOptions);
|
|
21
|
+
if (!token) {
|
|
22
|
+
throw new Error('Failed to get access token from ADC');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const modifiedInit: RequestInit = {
|
|
26
|
+
...init,
|
|
27
|
+
headers: {
|
|
28
|
+
...existingHeaders,
|
|
29
|
+
authorization: `Bearer ${token}`,
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
return (customFetch ?? fetch)(url as any, modifiedInit);
|
|
34
|
+
};
|
|
35
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { GoogleAuth, GoogleAuthOptions } from 'google-auth-library';
|
|
2
|
+
|
|
3
|
+
let authInstance: GoogleAuth | null = null;
|
|
4
|
+
let authOptions: GoogleAuthOptions | null = null;
|
|
5
|
+
|
|
6
|
+
function getAuth(options: GoogleAuthOptions) {
|
|
7
|
+
if (!authInstance || options !== authOptions) {
|
|
8
|
+
authInstance = new GoogleAuth({
|
|
9
|
+
scopes: ['https://www.googleapis.com/auth/cloud-platform'],
|
|
10
|
+
...options,
|
|
11
|
+
});
|
|
12
|
+
authOptions = options;
|
|
13
|
+
}
|
|
14
|
+
return authInstance;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export async function generateAuthToken(options?: GoogleAuthOptions) {
|
|
18
|
+
const auth = getAuth(options || {});
|
|
19
|
+
const client = await auth.getClient();
|
|
20
|
+
const token = await client.getAccessToken();
|
|
21
|
+
return token?.token || null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// For testing purposes only
|
|
25
|
+
export function _resetAuthInstance() {
|
|
26
|
+
authInstance = null;
|
|
27
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { withoutTrailingSlash } from '@ai-sdk/provider-utils';
|
|
2
|
+
|
|
3
|
+
function getDefaultVertexApiBase(location: string): string {
|
|
4
|
+
return location === 'global'
|
|
5
|
+
? 'https://aiplatform.googleapis.com'
|
|
6
|
+
: `https://${location}-aiplatform.googleapis.com`;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function getVertexOpenAICompatibleBaseURL({
|
|
10
|
+
project,
|
|
11
|
+
location,
|
|
12
|
+
apiVersion,
|
|
13
|
+
apiBase,
|
|
14
|
+
}: {
|
|
15
|
+
project: string;
|
|
16
|
+
location: string;
|
|
17
|
+
apiVersion: 'v1' | 'v1beta1';
|
|
18
|
+
apiBase?: string;
|
|
19
|
+
}): string {
|
|
20
|
+
const resolvedApiBase = withoutTrailingSlash(
|
|
21
|
+
apiBase ?? getDefaultVertexApiBase(location),
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
return `${resolvedApiBase}/${apiVersion}/projects/${project}/locations/${location}/endpoints/openapi`;
|
|
25
|
+
}
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import {
|
|
2
|
+
MetadataExtractor,
|
|
3
|
+
OpenAICompatibleProvider,
|
|
4
|
+
OpenAICompatibleChatLanguageModel,
|
|
5
|
+
OpenAICompatibleCompletionLanguageModel,
|
|
6
|
+
OpenAICompatibleEmbeddingModel,
|
|
7
|
+
OpenAICompatibleImageModel,
|
|
8
|
+
} from '@ai-sdk/openai-compatible';
|
|
9
|
+
import {
|
|
10
|
+
FetchFunction,
|
|
11
|
+
loadSetting,
|
|
12
|
+
withoutTrailingSlash,
|
|
13
|
+
withUserAgentSuffix,
|
|
14
|
+
} from '@ai-sdk/provider-utils';
|
|
15
|
+
import type { GoogleAuthOptions } from 'google-auth-library';
|
|
16
|
+
import { VERSION } from './version';
|
|
17
|
+
import { createVertexMaaSAuthFetch } from './vertex-maas-auth-fetch';
|
|
18
|
+
import { getVertexOpenAICompatibleBaseURL } from './vertex-maas-base-url';
|
|
19
|
+
import type {
|
|
20
|
+
EmbeddingModelV3,
|
|
21
|
+
ImageModelV3,
|
|
22
|
+
LanguageModelV3,
|
|
23
|
+
} from '@ai-sdk/provider';
|
|
24
|
+
|
|
25
|
+
type OpenAICompatibleChatConfig = ConstructorParameters<
|
|
26
|
+
typeof OpenAICompatibleChatLanguageModel
|
|
27
|
+
>[1];
|
|
28
|
+
|
|
29
|
+
export interface VertexMaaSProviderSettings {
|
|
30
|
+
/**
|
|
31
|
+
* Base URL for the API calls. If not set, it is derived from `project` and
|
|
32
|
+
* `location`.
|
|
33
|
+
*/
|
|
34
|
+
baseURL?: string;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Vertex project id. Defaults to `GOOGLE_VERTEX_PROJECT` if `baseURL` is not
|
|
38
|
+
* provided.
|
|
39
|
+
*/
|
|
40
|
+
project?: string;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Vertex location. Defaults to `GOOGLE_VERTEX_LOCATION` if `baseURL` is not
|
|
44
|
+
* provided.
|
|
45
|
+
*/
|
|
46
|
+
location?: string;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* API version used when computing baseURL. Defaults to `v1beta1`.
|
|
50
|
+
*/
|
|
51
|
+
apiVersion?: 'v1' | 'v1beta1';
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Override the base host for Vertex API calls (e.g. "https://aiplatform.googleapis.com").
|
|
55
|
+
* If not set, it is computed from location.
|
|
56
|
+
*/
|
|
57
|
+
apiBase?: string;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Optional. Use a static bearer token instead of ADC.
|
|
61
|
+
*/
|
|
62
|
+
apiKey?: string;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Optional. Passed to google-auth-library for ADC token generation.
|
|
66
|
+
*/
|
|
67
|
+
googleAuthOptions?: GoogleAuthOptions;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Optional custom headers.
|
|
71
|
+
*/
|
|
72
|
+
headers?: Record<string, string>;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Optional custom url query parameters to include in request urls.
|
|
76
|
+
*/
|
|
77
|
+
queryParams?: Record<string, string>;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Custom fetch implementation.
|
|
81
|
+
*/
|
|
82
|
+
fetch?: FetchFunction;
|
|
83
|
+
|
|
84
|
+
includeUsage?: boolean;
|
|
85
|
+
supportsStructuredOutputs?: boolean;
|
|
86
|
+
transformRequestBody?: (args: Record<string, any>) => Record<string, any>;
|
|
87
|
+
metadataExtractor?: MetadataExtractor;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export type VertexMaaSProvider = OpenAICompatibleProvider;
|
|
91
|
+
|
|
92
|
+
export function createVertexMaaS(
|
|
93
|
+
options: VertexMaaSProviderSettings = {},
|
|
94
|
+
): VertexMaaSProvider {
|
|
95
|
+
const providerName = 'vertexMaas';
|
|
96
|
+
|
|
97
|
+
let baseURLCache: string | null = null;
|
|
98
|
+
const getBaseURL = () => {
|
|
99
|
+
if (baseURLCache != null) {
|
|
100
|
+
return baseURLCache;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
baseURLCache =
|
|
104
|
+
withoutTrailingSlash(options.baseURL) ??
|
|
105
|
+
getVertexOpenAICompatibleBaseURL({
|
|
106
|
+
project: loadSetting({
|
|
107
|
+
settingValue: options.project,
|
|
108
|
+
settingName: 'project',
|
|
109
|
+
environmentVariableName: 'GOOGLE_VERTEX_PROJECT',
|
|
110
|
+
description: 'Google Vertex project',
|
|
111
|
+
}),
|
|
112
|
+
location: loadSetting({
|
|
113
|
+
settingValue: options.location,
|
|
114
|
+
settingName: 'location',
|
|
115
|
+
environmentVariableName: 'GOOGLE_VERTEX_LOCATION',
|
|
116
|
+
description: 'Google Vertex location',
|
|
117
|
+
}),
|
|
118
|
+
apiVersion: options.apiVersion ?? 'v1beta1',
|
|
119
|
+
apiBase: options.apiBase,
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
return baseURLCache;
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
interface CommonModelConfig {
|
|
126
|
+
provider: string;
|
|
127
|
+
url: ({ path }: { path: string }) => string;
|
|
128
|
+
headers: () => Record<string, string>;
|
|
129
|
+
fetch?: FetchFunction;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const staticHeaders = {
|
|
133
|
+
...(options.apiKey && { Authorization: `Bearer ${options.apiKey}` }),
|
|
134
|
+
...options.headers,
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
const getHeaders = () =>
|
|
138
|
+
withUserAgentSuffix(staticHeaders, `ai-sdk/vertex-maas/${VERSION}`);
|
|
139
|
+
|
|
140
|
+
const fetch =
|
|
141
|
+
options.apiKey != null
|
|
142
|
+
? options.fetch
|
|
143
|
+
: createVertexMaaSAuthFetch({
|
|
144
|
+
customFetch: options.fetch,
|
|
145
|
+
googleAuthOptions: options.googleAuthOptions,
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
const getCommonModelConfig = (modelType: string): CommonModelConfig => ({
|
|
149
|
+
provider: `${providerName}.${modelType}`,
|
|
150
|
+
url: ({ path }) => {
|
|
151
|
+
const url = new URL(`${getBaseURL()}${path}`);
|
|
152
|
+
if (options.queryParams) {
|
|
153
|
+
url.search = new URLSearchParams(options.queryParams).toString();
|
|
154
|
+
}
|
|
155
|
+
return url.toString();
|
|
156
|
+
},
|
|
157
|
+
headers: getHeaders,
|
|
158
|
+
fetch,
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
const createLanguageModel = (modelId: string) => createChatModel(modelId);
|
|
162
|
+
|
|
163
|
+
const createChatModel = (modelId: string) =>
|
|
164
|
+
new OpenAICompatibleChatLanguageModel(modelId, {
|
|
165
|
+
...getCommonModelConfig('chat'),
|
|
166
|
+
includeUsage: options.includeUsage,
|
|
167
|
+
supportsStructuredOutputs: options.supportsStructuredOutputs,
|
|
168
|
+
transformRequestBody: options.transformRequestBody,
|
|
169
|
+
metadataExtractor: options.metadataExtractor,
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
const createCompletionModel = (modelId: string) =>
|
|
173
|
+
new OpenAICompatibleCompletionLanguageModel(modelId, {
|
|
174
|
+
...getCommonModelConfig('completion'),
|
|
175
|
+
includeUsage: options.includeUsage,
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
const createEmbeddingModel = (modelId: string) =>
|
|
179
|
+
new OpenAICompatibleEmbeddingModel(modelId, {
|
|
180
|
+
...getCommonModelConfig('embedding'),
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
const createImageModel = (modelId: string) =>
|
|
184
|
+
new OpenAICompatibleImageModel(modelId, getCommonModelConfig('image'));
|
|
185
|
+
|
|
186
|
+
const provider = (modelId: string) => createLanguageModel(modelId);
|
|
187
|
+
|
|
188
|
+
provider.specificationVersion = 'v3' as const;
|
|
189
|
+
provider.languageModel = (
|
|
190
|
+
modelId: string,
|
|
191
|
+
config?: Partial<OpenAICompatibleChatConfig>,
|
|
192
|
+
): LanguageModelV3 =>
|
|
193
|
+
new OpenAICompatibleChatLanguageModel(modelId, {
|
|
194
|
+
...getCommonModelConfig('chat'),
|
|
195
|
+
includeUsage: options.includeUsage,
|
|
196
|
+
supportsStructuredOutputs: options.supportsStructuredOutputs,
|
|
197
|
+
transformRequestBody: options.transformRequestBody,
|
|
198
|
+
metadataExtractor: options.metadataExtractor,
|
|
199
|
+
...config,
|
|
200
|
+
});
|
|
201
|
+
provider.chatModel = createChatModel;
|
|
202
|
+
provider.completionModel = createCompletionModel;
|
|
203
|
+
provider.embeddingModel = createEmbeddingModel as (
|
|
204
|
+
modelId: string,
|
|
205
|
+
) => EmbeddingModelV3;
|
|
206
|
+
provider.textEmbeddingModel = createEmbeddingModel as (
|
|
207
|
+
modelId: string,
|
|
208
|
+
) => EmbeddingModelV3;
|
|
209
|
+
provider.imageModel = createImageModel as (modelId: string) => ImageModelV3;
|
|
210
|
+
|
|
211
|
+
return provider as unknown as OpenAICompatibleProvider;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Default Vertex MaaS provider instance.
|
|
216
|
+
*/
|
|
217
|
+
export const vertexMaaS = createVertexMaaS();
|