@ai-sdk/google-vertex 1.0.4 → 2.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 +22 -0
- package/README.md +141 -9
- package/anthropic/dist/index.d.mts +62 -0
- package/anthropic/dist/index.d.ts +62 -0
- package/anthropic/dist/index.js +121 -0
- package/anthropic/dist/index.js.map +1 -0
- package/anthropic/dist/index.mjs +101 -0
- package/anthropic/dist/index.mjs.map +1 -0
- package/anthropic/edge/dist/index.d.mts +78 -0
- package/anthropic/edge/dist/index.d.ts +78 -0
- package/anthropic/edge/dist/index.js +202 -0
- package/anthropic/edge/dist/index.js.map +1 -0
- package/anthropic/edge/dist/index.mjs +182 -0
- package/anthropic/edge/dist/index.mjs.map +1 -0
- package/dist/index.d.mts +25 -35
- package/dist/index.d.ts +25 -35
- package/dist/index.js +69 -639
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +65 -641
- package/dist/index.mjs.map +1 -1
- package/edge/dist/index.d.mts +73 -0
- package/edge/dist/index.d.ts +73 -0
- package/edge/dist/index.js +298 -0
- package/edge/dist/index.js.map +1 -0
- package/edge/dist/index.mjs +280 -0
- package/edge/dist/index.mjs.map +1 -0
- package/package.json +25 -6
@@ -0,0 +1,78 @@
|
|
1
|
+
import { ProviderV1, LanguageModelV1 } from '@ai-sdk/provider';
|
2
|
+
import { Resolvable, FetchFunction } from '@ai-sdk/provider-utils';
|
3
|
+
import { AnthropicMessagesSettings, anthropicTools } from '@ai-sdk/anthropic/internal';
|
4
|
+
|
5
|
+
interface GoogleCredentials {
|
6
|
+
/**
|
7
|
+
* The client email for the Google Cloud service account. Defaults to the
|
8
|
+
* value of the `GOOGLE_CLIENT_EMAIL` environment variable.
|
9
|
+
*/
|
10
|
+
clientEmail: string;
|
11
|
+
/**
|
12
|
+
* The private key for the Google Cloud service account. Defaults to the
|
13
|
+
* value of the `GOOGLE_PRIVATE_KEY` environment variable.
|
14
|
+
*/
|
15
|
+
privateKey: string;
|
16
|
+
/**
|
17
|
+
* Optional. The private key ID for the Google Cloud service account. Defaults
|
18
|
+
* to the value of the `GOOGLE_PRIVATE_KEY_ID` environment variable.
|
19
|
+
*/
|
20
|
+
privateKeyId?: string;
|
21
|
+
}
|
22
|
+
|
23
|
+
type GoogleVertexAnthropicMessagesModelId = 'claude-3-5-sonnet-v2@20241022' | 'claude-3-5-haiku@20241022' | 'claude-3-5-sonnet@20240620' | 'claude-3-haiku@20240307' | 'claude-3-sonnet@20240229' | 'claude-3-opus@20240229' | (string & {});
|
24
|
+
|
25
|
+
interface GoogleVertexAnthropicProvider extends ProviderV1 {
|
26
|
+
/**
|
27
|
+
Creates a model for text generation.
|
28
|
+
*/
|
29
|
+
(modelId: GoogleVertexAnthropicMessagesModelId, settings?: AnthropicMessagesSettings): LanguageModelV1;
|
30
|
+
/**
|
31
|
+
Creates a model for text generation.
|
32
|
+
*/
|
33
|
+
languageModel(modelId: GoogleVertexAnthropicMessagesModelId, settings?: AnthropicMessagesSettings): LanguageModelV1;
|
34
|
+
/**
|
35
|
+
Anthropic-specific computer use tool.
|
36
|
+
*/
|
37
|
+
tools: typeof anthropicTools;
|
38
|
+
}
|
39
|
+
interface GoogleVertexAnthropicProviderSettings$1 {
|
40
|
+
/**
|
41
|
+
* Google Cloud project ID. Defaults to the value of the `GOOGLE_VERTEX_PROJECT` environment variable.
|
42
|
+
*/
|
43
|
+
project?: string;
|
44
|
+
/**
|
45
|
+
* Google Cloud region. Defaults to the value of the `GOOGLE_VERTEX_LOCATION` environment variable.
|
46
|
+
*/
|
47
|
+
location?: string;
|
48
|
+
/**
|
49
|
+
Use a different URL prefix for API calls, e.g. to use proxy servers.
|
50
|
+
The default prefix is `https://api.anthropic.com/v1`.
|
51
|
+
*/
|
52
|
+
baseURL?: string;
|
53
|
+
/**
|
54
|
+
Custom headers to include in the requests.
|
55
|
+
*/
|
56
|
+
headers?: Resolvable<Record<string, string | undefined>>;
|
57
|
+
/**
|
58
|
+
Custom fetch implementation. You can use it as a middleware to intercept requests,
|
59
|
+
or to provide a custom fetch implementation for e.g. testing.
|
60
|
+
*/
|
61
|
+
fetch?: FetchFunction;
|
62
|
+
}
|
63
|
+
|
64
|
+
interface GoogleVertexAnthropicProviderSettings extends GoogleVertexAnthropicProviderSettings$1 {
|
65
|
+
/**
|
66
|
+
* Optional. The Google credentials for the Google Cloud service account. If
|
67
|
+
* not provided, the Google Vertex provider will use environment variables to
|
68
|
+
* load the credentials.
|
69
|
+
*/
|
70
|
+
googleCredentials?: GoogleCredentials;
|
71
|
+
}
|
72
|
+
declare function createVertexAnthropic(options?: GoogleVertexAnthropicProviderSettings): GoogleVertexAnthropicProvider;
|
73
|
+
/**
|
74
|
+
* Default Google Vertex AI Anthropic provider instance.
|
75
|
+
*/
|
76
|
+
declare const vertexAnthropic: GoogleVertexAnthropicProvider;
|
77
|
+
|
78
|
+
export { type GoogleVertexAnthropicProvider, type GoogleVertexAnthropicProviderSettings, createVertexAnthropic, vertexAnthropic };
|
@@ -0,0 +1,202 @@
|
|
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/anthropic/edge/index.ts
|
21
|
+
var edge_exports = {};
|
22
|
+
__export(edge_exports, {
|
23
|
+
createVertexAnthropic: () => createVertexAnthropic2,
|
24
|
+
vertexAnthropic: () => vertexAnthropic
|
25
|
+
});
|
26
|
+
module.exports = __toCommonJS(edge_exports);
|
27
|
+
|
28
|
+
// src/edge/google-vertex-auth-edge.ts
|
29
|
+
var import_provider_utils = require("@ai-sdk/provider-utils");
|
30
|
+
var loadCredentials = async () => {
|
31
|
+
try {
|
32
|
+
return {
|
33
|
+
clientEmail: (0, import_provider_utils.loadSetting)({
|
34
|
+
settingValue: void 0,
|
35
|
+
settingName: "clientEmail",
|
36
|
+
environmentVariableName: "GOOGLE_CLIENT_EMAIL",
|
37
|
+
description: "Google client email"
|
38
|
+
}),
|
39
|
+
privateKey: (0, import_provider_utils.loadSetting)({
|
40
|
+
settingValue: void 0,
|
41
|
+
settingName: "privateKey",
|
42
|
+
environmentVariableName: "GOOGLE_PRIVATE_KEY",
|
43
|
+
description: "Google private key"
|
44
|
+
}),
|
45
|
+
privateKeyId: (0, import_provider_utils.loadOptionalSetting)({
|
46
|
+
settingValue: void 0,
|
47
|
+
environmentVariableName: "GOOGLE_PRIVATE_KEY_ID"
|
48
|
+
})
|
49
|
+
};
|
50
|
+
} catch (error) {
|
51
|
+
throw new Error(`Failed to load Google credentials: ${error.message}`);
|
52
|
+
}
|
53
|
+
};
|
54
|
+
var base64url = (str) => {
|
55
|
+
return btoa(str).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
|
56
|
+
};
|
57
|
+
var importPrivateKey = async (pemKey) => {
|
58
|
+
const pemHeader = "-----BEGIN PRIVATE KEY-----";
|
59
|
+
const pemFooter = "-----END PRIVATE KEY-----";
|
60
|
+
const pemContents = pemKey.replace(pemHeader, "").replace(pemFooter, "").replace(/\s/g, "");
|
61
|
+
const binaryString = atob(pemContents);
|
62
|
+
const binaryData = new Uint8Array(binaryString.length);
|
63
|
+
for (let i = 0; i < binaryString.length; i++) {
|
64
|
+
binaryData[i] = binaryString.charCodeAt(i);
|
65
|
+
}
|
66
|
+
return await crypto.subtle.importKey(
|
67
|
+
"pkcs8",
|
68
|
+
binaryData,
|
69
|
+
{ name: "RSASSA-PKCS1-v1_5", hash: "SHA-256" },
|
70
|
+
true,
|
71
|
+
["sign"]
|
72
|
+
);
|
73
|
+
};
|
74
|
+
var buildJwt = async (credentials) => {
|
75
|
+
const now = Math.floor(Date.now() / 1e3);
|
76
|
+
const header = {
|
77
|
+
alg: "RS256",
|
78
|
+
typ: "JWT"
|
79
|
+
};
|
80
|
+
if (credentials.privateKeyId) {
|
81
|
+
header.kid = credentials.privateKeyId;
|
82
|
+
}
|
83
|
+
const payload = {
|
84
|
+
iss: credentials.clientEmail,
|
85
|
+
scope: "https://www.googleapis.com/auth/cloud-platform",
|
86
|
+
aud: "https://oauth2.googleapis.com/token",
|
87
|
+
exp: now + 3600,
|
88
|
+
iat: now
|
89
|
+
};
|
90
|
+
const privateKey = await importPrivateKey(credentials.privateKey);
|
91
|
+
const signingInput = `${base64url(JSON.stringify(header))}.${base64url(
|
92
|
+
JSON.stringify(payload)
|
93
|
+
)}`;
|
94
|
+
const encoder = new TextEncoder();
|
95
|
+
const data = encoder.encode(signingInput);
|
96
|
+
const signature = await crypto.subtle.sign(
|
97
|
+
"RSASSA-PKCS1-v1_5",
|
98
|
+
privateKey,
|
99
|
+
data
|
100
|
+
);
|
101
|
+
const signatureBase64 = base64url(
|
102
|
+
String.fromCharCode(...new Uint8Array(signature))
|
103
|
+
);
|
104
|
+
return `${base64url(JSON.stringify(header))}.${base64url(
|
105
|
+
JSON.stringify(payload)
|
106
|
+
)}.${signatureBase64}`;
|
107
|
+
};
|
108
|
+
async function generateAuthToken(credentials) {
|
109
|
+
try {
|
110
|
+
const creds = credentials || await loadCredentials();
|
111
|
+
const jwt = await buildJwt(creds);
|
112
|
+
const response = await fetch("https://oauth2.googleapis.com/token", {
|
113
|
+
method: "POST",
|
114
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
115
|
+
body: new URLSearchParams({
|
116
|
+
grant_type: "urn:ietf:params:oauth:grant-type:jwt-bearer",
|
117
|
+
assertion: jwt
|
118
|
+
})
|
119
|
+
});
|
120
|
+
if (!response.ok) {
|
121
|
+
throw new Error(`Token request failed: ${response.statusText}`);
|
122
|
+
}
|
123
|
+
const data = await response.json();
|
124
|
+
return data.access_token;
|
125
|
+
} catch (error) {
|
126
|
+
throw error;
|
127
|
+
}
|
128
|
+
}
|
129
|
+
|
130
|
+
// src/anthropic/google-vertex-anthropic-provider.ts
|
131
|
+
var import_provider = require("@ai-sdk/provider");
|
132
|
+
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
133
|
+
var import_internal = require("@ai-sdk/anthropic/internal");
|
134
|
+
function createVertexAnthropic(options = {}) {
|
135
|
+
var _a;
|
136
|
+
const location = (0, import_provider_utils2.loadOptionalSetting)({
|
137
|
+
settingValue: options.location,
|
138
|
+
environmentVariableName: "GOOGLE_VERTEX_LOCATION"
|
139
|
+
});
|
140
|
+
const project = (0, import_provider_utils2.loadOptionalSetting)({
|
141
|
+
settingValue: options.project,
|
142
|
+
environmentVariableName: "GOOGLE_VERTEX_PROJECT"
|
143
|
+
});
|
144
|
+
const baseURL = (_a = (0, import_provider_utils2.withoutTrailingSlash)(options.baseURL)) != null ? _a : `https://${location}-aiplatform.googleapis.com/v1/projects/${project}/locations/${location}/publishers/anthropic/models`;
|
145
|
+
const createChatModel = (modelId, settings = {}) => {
|
146
|
+
var _a2;
|
147
|
+
return new import_internal.AnthropicMessagesLanguageModel(
|
148
|
+
modelId,
|
149
|
+
settings,
|
150
|
+
{
|
151
|
+
provider: "vertex.anthropic.messages",
|
152
|
+
baseURL,
|
153
|
+
headers: (_a2 = options.headers) != null ? _a2 : {},
|
154
|
+
fetch: options.fetch,
|
155
|
+
buildRequestUrl: (baseURL2, isStreaming) => `${baseURL2}/${modelId}:${isStreaming ? "streamRawPredict" : "rawPredict"}`,
|
156
|
+
transformRequestBody: (args) => {
|
157
|
+
const { model, ...rest } = args;
|
158
|
+
return {
|
159
|
+
...rest,
|
160
|
+
anthropic_version: "vertex-2023-10-16"
|
161
|
+
};
|
162
|
+
}
|
163
|
+
}
|
164
|
+
);
|
165
|
+
};
|
166
|
+
const provider = function(modelId, settings) {
|
167
|
+
if (new.target) {
|
168
|
+
throw new Error(
|
169
|
+
"The Anthropic model function cannot be called with the new keyword."
|
170
|
+
);
|
171
|
+
}
|
172
|
+
return createChatModel(modelId, settings);
|
173
|
+
};
|
174
|
+
provider.languageModel = createChatModel;
|
175
|
+
provider.chat = createChatModel;
|
176
|
+
provider.messages = createChatModel;
|
177
|
+
provider.textEmbeddingModel = (modelId) => {
|
178
|
+
throw new import_provider.NoSuchModelError({ modelId, modelType: "textEmbeddingModel" });
|
179
|
+
};
|
180
|
+
provider.tools = import_internal.anthropicTools;
|
181
|
+
return provider;
|
182
|
+
}
|
183
|
+
|
184
|
+
// src/anthropic/edge/google-vertex-anthropic-provider-edge.ts
|
185
|
+
function createVertexAnthropic2(options = {}) {
|
186
|
+
var _a;
|
187
|
+
return createVertexAnthropic({
|
188
|
+
...options,
|
189
|
+
headers: (_a = options.headers) != null ? _a : async () => ({
|
190
|
+
Authorization: `Bearer ${await generateAuthToken(
|
191
|
+
options.googleCredentials
|
192
|
+
)}`
|
193
|
+
})
|
194
|
+
});
|
195
|
+
}
|
196
|
+
var vertexAnthropic = createVertexAnthropic2();
|
197
|
+
// Annotate the CommonJS export names for ESM import in node:
|
198
|
+
0 && (module.exports = {
|
199
|
+
createVertexAnthropic,
|
200
|
+
vertexAnthropic
|
201
|
+
});
|
202
|
+
//# sourceMappingURL=index.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../../src/anthropic/edge/index.ts","../../../src/edge/google-vertex-auth-edge.ts","../../../src/anthropic/google-vertex-anthropic-provider.ts","../../../src/anthropic/edge/google-vertex-anthropic-provider-edge.ts"],"sourcesContent":["export {\n createVertexAnthropic,\n vertexAnthropic,\n} from './google-vertex-anthropic-provider-edge';\nexport type {\n GoogleVertexAnthropicProviderSettings,\n GoogleVertexAnthropicProvider,\n} from './google-vertex-anthropic-provider-edge';\n","import { loadOptionalSetting, loadSetting } from '@ai-sdk/provider-utils';\n\nexport interface GoogleCredentials {\n /**\n * The client email for the Google Cloud service account. Defaults to the\n * value of the `GOOGLE_CLIENT_EMAIL` environment variable.\n */\n clientEmail: string;\n\n /**\n * The private key for the Google Cloud service account. Defaults to the\n * value of the `GOOGLE_PRIVATE_KEY` environment variable.\n */\n privateKey: string;\n\n /**\n * Optional. The private key ID for the Google Cloud service account. Defaults\n * to the value of the `GOOGLE_PRIVATE_KEY_ID` environment variable.\n */\n privateKeyId?: string;\n}\n\nconst loadCredentials = async (): Promise<GoogleCredentials> => {\n try {\n return {\n clientEmail: loadSetting({\n settingValue: undefined,\n settingName: 'clientEmail',\n environmentVariableName: 'GOOGLE_CLIENT_EMAIL',\n description: 'Google client email',\n }),\n privateKey: loadSetting({\n settingValue: undefined,\n settingName: 'privateKey',\n environmentVariableName: 'GOOGLE_PRIVATE_KEY',\n description: 'Google private key',\n }),\n privateKeyId: loadOptionalSetting({\n settingValue: undefined,\n environmentVariableName: 'GOOGLE_PRIVATE_KEY_ID',\n }),\n };\n } catch (error: any) {\n throw new Error(`Failed to load Google credentials: ${error.message}`);\n }\n};\n\n// Convert a string to base64url\nconst base64url = (str: string) => {\n return btoa(str).replace(/\\+/g, '-').replace(/\\//g, '_').replace(/=/g, '');\n};\nconst importPrivateKey = async (pemKey: string) => {\n const pemHeader = '-----BEGIN PRIVATE KEY-----';\n const pemFooter = '-----END PRIVATE KEY-----';\n\n // Remove header, footer, and any whitespace/newlines\n const pemContents = pemKey\n .replace(pemHeader, '')\n .replace(pemFooter, '')\n .replace(/\\s/g, '');\n\n // Decode base64 to binary\n const binaryString = atob(pemContents);\n\n // Convert binary string to Uint8Array\n const binaryData = new Uint8Array(binaryString.length);\n for (let i = 0; i < binaryString.length; i++) {\n binaryData[i] = binaryString.charCodeAt(i);\n }\n\n return await crypto.subtle.importKey(\n 'pkcs8',\n binaryData,\n { name: 'RSASSA-PKCS1-v1_5', hash: 'SHA-256' },\n true,\n ['sign'],\n );\n};\n\nconst buildJwt = async (credentials: GoogleCredentials) => {\n const now = Math.floor(Date.now() / 1000);\n\n // Only include kid in header if privateKeyId is provided\n const header: { alg: string; typ: string; kid?: string } = {\n alg: 'RS256',\n typ: 'JWT',\n };\n\n if (credentials.privateKeyId) {\n header.kid = credentials.privateKeyId;\n }\n\n const payload = {\n iss: credentials.clientEmail,\n scope: 'https://www.googleapis.com/auth/cloud-platform',\n aud: 'https://oauth2.googleapis.com/token',\n exp: now + 3600,\n iat: now,\n };\n\n const privateKey = await importPrivateKey(credentials.privateKey);\n\n const signingInput = `${base64url(JSON.stringify(header))}.${base64url(\n JSON.stringify(payload),\n )}`;\n const encoder = new TextEncoder();\n const data = encoder.encode(signingInput);\n\n const signature = await crypto.subtle.sign(\n 'RSASSA-PKCS1-v1_5',\n privateKey,\n data,\n );\n\n const signatureBase64 = base64url(\n String.fromCharCode(...new Uint8Array(signature)),\n );\n\n return `${base64url(JSON.stringify(header))}.${base64url(\n JSON.stringify(payload),\n )}.${signatureBase64}`;\n};\n\n/**\n * Generate an authentication token for Google Vertex AI in a manner compatible\n * with the Edge runtime.\n */\nexport async function generateAuthToken(credentials?: GoogleCredentials) {\n try {\n const creds = credentials || (await loadCredentials());\n const jwt = await buildJwt(creds);\n\n const response = await fetch('https://oauth2.googleapis.com/token', {\n method: 'POST',\n headers: { 'Content-Type': 'application/x-www-form-urlencoded' },\n body: new URLSearchParams({\n grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',\n assertion: jwt,\n }),\n });\n\n if (!response.ok) {\n throw new Error(`Token request failed: ${response.statusText}`);\n }\n\n const data = await response.json();\n return data.access_token;\n } catch (error) {\n throw error;\n }\n}\n","import {\n LanguageModelV1,\n NoSuchModelError,\n ProviderV1,\n} from '@ai-sdk/provider';\nimport {\n FetchFunction,\n Resolvable,\n loadOptionalSetting,\n withoutTrailingSlash,\n} from '@ai-sdk/provider-utils';\nimport {\n anthropicTools,\n AnthropicMessagesLanguageModel,\n AnthropicMessagesModelId,\n AnthropicMessagesSettings,\n} from '@ai-sdk/anthropic/internal';\nimport {\n GoogleVertexAnthropicMessagesModelId,\n GoogleVertexAnthropicMessagesSettings,\n} from './google-vertex-anthropic-messages-settings';\nexport interface GoogleVertexAnthropicProvider extends ProviderV1 {\n /**\nCreates a model for text generation.\n*/\n (\n modelId: GoogleVertexAnthropicMessagesModelId,\n settings?: AnthropicMessagesSettings,\n ): LanguageModelV1;\n\n /**\nCreates a model for text generation.\n*/\n languageModel(\n modelId: GoogleVertexAnthropicMessagesModelId,\n settings?: AnthropicMessagesSettings,\n ): LanguageModelV1;\n\n /**\nAnthropic-specific computer use tool.\n */\n tools: typeof anthropicTools;\n}\n\nexport interface GoogleVertexAnthropicProviderSettings {\n /**\n * Google Cloud project ID. Defaults to the value of the `GOOGLE_VERTEX_PROJECT` environment variable.\n */\n project?: string;\n\n /**\n * Google Cloud region. Defaults to the value of the `GOOGLE_VERTEX_LOCATION` environment variable.\n */\n location?: string;\n\n /**\nUse a different URL prefix for API calls, e.g. to use proxy servers.\nThe default prefix is `https://api.anthropic.com/v1`.\n */\n baseURL?: string;\n\n /**\nCustom headers to include in the requests.\n */\n headers?: Resolvable<Record<string, string | undefined>>;\n\n /**\nCustom fetch implementation. You can use it as a middleware to intercept requests,\nor to provide a custom fetch implementation for e.g. testing.\n */\n fetch?: FetchFunction;\n}\n\n/**\nCreate a Google Vertex Anthropic provider instance.\n */\nexport function createVertexAnthropic(\n options: GoogleVertexAnthropicProviderSettings = {},\n): GoogleVertexAnthropicProvider {\n const location = loadOptionalSetting({\n settingValue: options.location,\n environmentVariableName: 'GOOGLE_VERTEX_LOCATION',\n });\n const project = loadOptionalSetting({\n settingValue: options.project,\n environmentVariableName: 'GOOGLE_VERTEX_PROJECT',\n });\n const baseURL =\n withoutTrailingSlash(options.baseURL) ??\n `https://${location}-aiplatform.googleapis.com/v1/projects/${project}/locations/${location}/publishers/anthropic/models`;\n\n const createChatModel = (\n modelId: GoogleVertexAnthropicMessagesModelId,\n settings: GoogleVertexAnthropicMessagesSettings = {},\n ) =>\n new AnthropicMessagesLanguageModel(\n modelId as AnthropicMessagesModelId,\n settings,\n {\n provider: 'vertex.anthropic.messages',\n baseURL,\n headers: options.headers ?? {},\n fetch: options.fetch,\n buildRequestUrl: (baseURL, isStreaming) =>\n `${baseURL}/${modelId}:${\n isStreaming ? 'streamRawPredict' : 'rawPredict'\n }`,\n transformRequestBody: args => {\n // Remove model from args and add anthropic version\n const { model, ...rest } = args;\n return {\n ...rest,\n anthropic_version: 'vertex-2023-10-16',\n };\n },\n },\n );\n\n const provider = function (\n modelId: GoogleVertexAnthropicMessagesModelId,\n settings?: GoogleVertexAnthropicMessagesSettings,\n ) {\n if (new.target) {\n throw new Error(\n 'The Anthropic model function cannot be called with the new keyword.',\n );\n }\n\n return createChatModel(modelId, settings);\n };\n\n provider.languageModel = createChatModel;\n provider.chat = createChatModel;\n provider.messages = createChatModel;\n provider.textEmbeddingModel = (modelId: string) => {\n throw new NoSuchModelError({ modelId, modelType: 'textEmbeddingModel' });\n };\n\n provider.tools = anthropicTools;\n\n return provider as GoogleVertexAnthropicProvider;\n}\n","import {\n generateAuthToken,\n GoogleCredentials,\n} from '../../edge/google-vertex-auth-edge';\nimport {\n createVertexAnthropic as createVertexAnthropicOriginal,\n GoogleVertexAnthropicProvider,\n GoogleVertexAnthropicProviderSettings as GoogleVertexAnthropicProviderSettingsOriginal,\n} from '../google-vertex-anthropic-provider';\n\nexport type { GoogleVertexAnthropicProvider };\n\nexport interface GoogleVertexAnthropicProviderSettings\n extends GoogleVertexAnthropicProviderSettingsOriginal {\n /**\n * Optional. The Google credentials for the Google Cloud service account. If\n * not provided, the Google Vertex provider will use environment variables to\n * load the credentials.\n */\n googleCredentials?: GoogleCredentials;\n}\n\nexport function createVertexAnthropic(\n options: GoogleVertexAnthropicProviderSettings = {},\n): GoogleVertexAnthropicProvider {\n return createVertexAnthropicOriginal({\n ...options,\n headers:\n options.headers ??\n (async () => ({\n Authorization: `Bearer ${await generateAuthToken(\n options.googleCredentials,\n )}`,\n })),\n });\n}\n\n/**\n * Default Google Vertex AI Anthropic provider instance.\n */\nexport const vertexAnthropic = createVertexAnthropic();\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA,+BAAAA;AAAA,EAAA;AAAA;AAAA;;;ACAA,4BAAiD;AAsBjD,IAAM,kBAAkB,YAAwC;AAC9D,MAAI;AACF,WAAO;AAAA,MACL,iBAAa,mCAAY;AAAA,QACvB,cAAc;AAAA,QACd,aAAa;AAAA,QACb,yBAAyB;AAAA,QACzB,aAAa;AAAA,MACf,CAAC;AAAA,MACD,gBAAY,mCAAY;AAAA,QACtB,cAAc;AAAA,QACd,aAAa;AAAA,QACb,yBAAyB;AAAA,QACzB,aAAa;AAAA,MACf,CAAC;AAAA,MACD,kBAAc,2CAAoB;AAAA,QAChC,cAAc;AAAA,QACd,yBAAyB;AAAA,MAC3B,CAAC;AAAA,IACH;AAAA,EACF,SAAS,OAAY;AACnB,UAAM,IAAI,MAAM,sCAAsC,MAAM,OAAO,EAAE;AAAA,EACvE;AACF;AAGA,IAAM,YAAY,CAAC,QAAgB;AACjC,SAAO,KAAK,GAAG,EAAE,QAAQ,OAAO,GAAG,EAAE,QAAQ,OAAO,GAAG,EAAE,QAAQ,MAAM,EAAE;AAC3E;AACA,IAAM,mBAAmB,OAAO,WAAmB;AACjD,QAAM,YAAY;AAClB,QAAM,YAAY;AAGlB,QAAM,cAAc,OACjB,QAAQ,WAAW,EAAE,EACrB,QAAQ,WAAW,EAAE,EACrB,QAAQ,OAAO,EAAE;AAGpB,QAAM,eAAe,KAAK,WAAW;AAGrC,QAAM,aAAa,IAAI,WAA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AA,QACF;AAAA,MACF;AAAA,IACF;AAAA;AAEF,QAAM,WAAW,SACf,SACA,UACA;AACA,QAAI,YAAY;AACd,YAAM,IAAI;AAAA,QACR;AAAA,MACF;AAAA,IACF;AAEA,WAAO,gBAAgB,SAAS,QAAQ;AAAA,EAC1C;AAEA,WAAS,gBAAgB;AACzB,WAAS,OAAO;AAChB,WAAS,WAAW;AACpB,WAAS,qBAAqB,CAAC,YAAoB;AACjD,UAAM,IAAI,iCAAiB,EAAE,SAAS,WAAW,qBAAqB,CAAC;AAAA,EACzE;AAEA,WAAS,QAAQ;AAEjB,SAAO;AACT;;;ACvHO,SAASC,uBACd,UAAiD,CAAC,GACnB;AAxBjC;AAyBE,SAAO,sBAA8B;AAAA,IACnC,GAAG;AAAA,IACH,UACE,aAAQ,YAAR,YACC,aAAa;AAAA,MACZ,eAAe,UAAU,MAAM;AAAA,QAC7B,QAAQ;AAAA,MACV,CAAC;AAAA,IACH;AAAA,EACJ,CAAC;AACH;AAKO,IAAM,kBAAkBA,uBAAsB;","names":["createVertexAnthropic","import_provider_utils","_a","baseURL","createVertexAnthropic"]}
|
@@ -0,0 +1,182 @@
|
|
1
|
+
// src/edge/google-vertex-auth-edge.ts
|
2
|
+
import { loadOptionalSetting, loadSetting } from "@ai-sdk/provider-utils";
|
3
|
+
var loadCredentials = async () => {
|
4
|
+
try {
|
5
|
+
return {
|
6
|
+
clientEmail: loadSetting({
|
7
|
+
settingValue: void 0,
|
8
|
+
settingName: "clientEmail",
|
9
|
+
environmentVariableName: "GOOGLE_CLIENT_EMAIL",
|
10
|
+
description: "Google client email"
|
11
|
+
}),
|
12
|
+
privateKey: loadSetting({
|
13
|
+
settingValue: void 0,
|
14
|
+
settingName: "privateKey",
|
15
|
+
environmentVariableName: "GOOGLE_PRIVATE_KEY",
|
16
|
+
description: "Google private key"
|
17
|
+
}),
|
18
|
+
privateKeyId: loadOptionalSetting({
|
19
|
+
settingValue: void 0,
|
20
|
+
environmentVariableName: "GOOGLE_PRIVATE_KEY_ID"
|
21
|
+
})
|
22
|
+
};
|
23
|
+
} catch (error) {
|
24
|
+
throw new Error(`Failed to load Google credentials: ${error.message}`);
|
25
|
+
}
|
26
|
+
};
|
27
|
+
var base64url = (str) => {
|
28
|
+
return btoa(str).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
|
29
|
+
};
|
30
|
+
var importPrivateKey = async (pemKey) => {
|
31
|
+
const pemHeader = "-----BEGIN PRIVATE KEY-----";
|
32
|
+
const pemFooter = "-----END PRIVATE KEY-----";
|
33
|
+
const pemContents = pemKey.replace(pemHeader, "").replace(pemFooter, "").replace(/\s/g, "");
|
34
|
+
const binaryString = atob(pemContents);
|
35
|
+
const binaryData = new Uint8Array(binaryString.length);
|
36
|
+
for (let i = 0; i < binaryString.length; i++) {
|
37
|
+
binaryData[i] = binaryString.charCodeAt(i);
|
38
|
+
}
|
39
|
+
return await crypto.subtle.importKey(
|
40
|
+
"pkcs8",
|
41
|
+
binaryData,
|
42
|
+
{ name: "RSASSA-PKCS1-v1_5", hash: "SHA-256" },
|
43
|
+
true,
|
44
|
+
["sign"]
|
45
|
+
);
|
46
|
+
};
|
47
|
+
var buildJwt = async (credentials) => {
|
48
|
+
const now = Math.floor(Date.now() / 1e3);
|
49
|
+
const header = {
|
50
|
+
alg: "RS256",
|
51
|
+
typ: "JWT"
|
52
|
+
};
|
53
|
+
if (credentials.privateKeyId) {
|
54
|
+
header.kid = credentials.privateKeyId;
|
55
|
+
}
|
56
|
+
const payload = {
|
57
|
+
iss: credentials.clientEmail,
|
58
|
+
scope: "https://www.googleapis.com/auth/cloud-platform",
|
59
|
+
aud: "https://oauth2.googleapis.com/token",
|
60
|
+
exp: now + 3600,
|
61
|
+
iat: now
|
62
|
+
};
|
63
|
+
const privateKey = await importPrivateKey(credentials.privateKey);
|
64
|
+
const signingInput = `${base64url(JSON.stringify(header))}.${base64url(
|
65
|
+
JSON.stringify(payload)
|
66
|
+
)}`;
|
67
|
+
const encoder = new TextEncoder();
|
68
|
+
const data = encoder.encode(signingInput);
|
69
|
+
const signature = await crypto.subtle.sign(
|
70
|
+
"RSASSA-PKCS1-v1_5",
|
71
|
+
privateKey,
|
72
|
+
data
|
73
|
+
);
|
74
|
+
const signatureBase64 = base64url(
|
75
|
+
String.fromCharCode(...new Uint8Array(signature))
|
76
|
+
);
|
77
|
+
return `${base64url(JSON.stringify(header))}.${base64url(
|
78
|
+
JSON.stringify(payload)
|
79
|
+
)}.${signatureBase64}`;
|
80
|
+
};
|
81
|
+
async function generateAuthToken(credentials) {
|
82
|
+
try {
|
83
|
+
const creds = credentials || await loadCredentials();
|
84
|
+
const jwt = await buildJwt(creds);
|
85
|
+
const response = await fetch("https://oauth2.googleapis.com/token", {
|
86
|
+
method: "POST",
|
87
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
88
|
+
body: new URLSearchParams({
|
89
|
+
grant_type: "urn:ietf:params:oauth:grant-type:jwt-bearer",
|
90
|
+
assertion: jwt
|
91
|
+
})
|
92
|
+
});
|
93
|
+
if (!response.ok) {
|
94
|
+
throw new Error(`Token request failed: ${response.statusText}`);
|
95
|
+
}
|
96
|
+
const data = await response.json();
|
97
|
+
return data.access_token;
|
98
|
+
} catch (error) {
|
99
|
+
throw error;
|
100
|
+
}
|
101
|
+
}
|
102
|
+
|
103
|
+
// src/anthropic/google-vertex-anthropic-provider.ts
|
104
|
+
import {
|
105
|
+
NoSuchModelError
|
106
|
+
} from "@ai-sdk/provider";
|
107
|
+
import {
|
108
|
+
loadOptionalSetting as loadOptionalSetting2,
|
109
|
+
withoutTrailingSlash
|
110
|
+
} from "@ai-sdk/provider-utils";
|
111
|
+
import {
|
112
|
+
anthropicTools,
|
113
|
+
AnthropicMessagesLanguageModel
|
114
|
+
} from "@ai-sdk/anthropic/internal";
|
115
|
+
function createVertexAnthropic(options = {}) {
|
116
|
+
var _a;
|
117
|
+
const location = loadOptionalSetting2({
|
118
|
+
settingValue: options.location,
|
119
|
+
environmentVariableName: "GOOGLE_VERTEX_LOCATION"
|
120
|
+
});
|
121
|
+
const project = loadOptionalSetting2({
|
122
|
+
settingValue: options.project,
|
123
|
+
environmentVariableName: "GOOGLE_VERTEX_PROJECT"
|
124
|
+
});
|
125
|
+
const baseURL = (_a = withoutTrailingSlash(options.baseURL)) != null ? _a : `https://${location}-aiplatform.googleapis.com/v1/projects/${project}/locations/${location}/publishers/anthropic/models`;
|
126
|
+
const createChatModel = (modelId, settings = {}) => {
|
127
|
+
var _a2;
|
128
|
+
return new AnthropicMessagesLanguageModel(
|
129
|
+
modelId,
|
130
|
+
settings,
|
131
|
+
{
|
132
|
+
provider: "vertex.anthropic.messages",
|
133
|
+
baseURL,
|
134
|
+
headers: (_a2 = options.headers) != null ? _a2 : {},
|
135
|
+
fetch: options.fetch,
|
136
|
+
buildRequestUrl: (baseURL2, isStreaming) => `${baseURL2}/${modelId}:${isStreaming ? "streamRawPredict" : "rawPredict"}`,
|
137
|
+
transformRequestBody: (args) => {
|
138
|
+
const { model, ...rest } = args;
|
139
|
+
return {
|
140
|
+
...rest,
|
141
|
+
anthropic_version: "vertex-2023-10-16"
|
142
|
+
};
|
143
|
+
}
|
144
|
+
}
|
145
|
+
);
|
146
|
+
};
|
147
|
+
const provider = function(modelId, settings) {
|
148
|
+
if (new.target) {
|
149
|
+
throw new Error(
|
150
|
+
"The Anthropic model function cannot be called with the new keyword."
|
151
|
+
);
|
152
|
+
}
|
153
|
+
return createChatModel(modelId, settings);
|
154
|
+
};
|
155
|
+
provider.languageModel = createChatModel;
|
156
|
+
provider.chat = createChatModel;
|
157
|
+
provider.messages = createChatModel;
|
158
|
+
provider.textEmbeddingModel = (modelId) => {
|
159
|
+
throw new NoSuchModelError({ modelId, modelType: "textEmbeddingModel" });
|
160
|
+
};
|
161
|
+
provider.tools = anthropicTools;
|
162
|
+
return provider;
|
163
|
+
}
|
164
|
+
|
165
|
+
// src/anthropic/edge/google-vertex-anthropic-provider-edge.ts
|
166
|
+
function createVertexAnthropic2(options = {}) {
|
167
|
+
var _a;
|
168
|
+
return createVertexAnthropic({
|
169
|
+
...options,
|
170
|
+
headers: (_a = options.headers) != null ? _a : async () => ({
|
171
|
+
Authorization: `Bearer ${await generateAuthToken(
|
172
|
+
options.googleCredentials
|
173
|
+
)}`
|
174
|
+
})
|
175
|
+
});
|
176
|
+
}
|
177
|
+
var vertexAnthropic = createVertexAnthropic2();
|
178
|
+
export {
|
179
|
+
createVertexAnthropic2 as createVertexAnthropic,
|
180
|
+
vertexAnthropic
|
181
|
+
};
|
182
|
+
//# sourceMappingURL=index.mjs.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../../src/edge/google-vertex-auth-edge.ts","../../../src/anthropic/google-vertex-anthropic-provider.ts","../../../src/anthropic/edge/google-vertex-anthropic-provider-edge.ts"],"sourcesContent":["import { loadOptionalSetting, loadSetting } from '@ai-sdk/provider-utils';\n\nexport interface GoogleCredentials {\n /**\n * The client email for the Google Cloud service account. Defaults to the\n * value of the `GOOGLE_CLIENT_EMAIL` environment variable.\n */\n clientEmail: string;\n\n /**\n * The private key for the Google Cloud service account. Defaults to the\n * value of the `GOOGLE_PRIVATE_KEY` environment variable.\n */\n privateKey: string;\n\n /**\n * Optional. The private key ID for the Google Cloud service account. Defaults\n * to the value of the `GOOGLE_PRIVATE_KEY_ID` environment variable.\n */\n privateKeyId?: string;\n}\n\nconst loadCredentials = async (): Promise<GoogleCredentials> => {\n try {\n return {\n clientEmail: loadSetting({\n settingValue: undefined,\n settingName: 'clientEmail',\n environmentVariableName: 'GOOGLE_CLIENT_EMAIL',\n description: 'Google client email',\n }),\n privateKey: loadSetting({\n settingValue: undefined,\n settingName: 'privateKey',\n environmentVariableName: 'GOOGLE_PRIVATE_KEY',\n description: 'Google private key',\n }),\n privateKeyId: loadOptionalSetting({\n settingValue: undefined,\n environmentVariableName: 'GOOGLE_PRIVATE_KEY_ID',\n }),\n };\n } catch (error: any) {\n throw new Error(`Failed to load Google credentials: ${error.message}`);\n }\n};\n\n// Convert a string to base64url\nconst base64url = (str: string) => {\n return btoa(str).replace(/\\+/g, '-').replace(/\\//g, '_').replace(/=/g, '');\n};\nconst importPrivateKey = async (pemKey: string) => {\n const pemHeader = '-----BEGIN PRIVATE KEY-----';\n const pemFooter = '-----END PRIVATE KEY-----';\n\n // Remove header, footer, and any whitespace/newlines\n const pemContents = pemKey\n .replace(pemHeader, '')\n .replace(pemFooter, '')\n .replace(/\\s/g, '');\n\n // Decode base64 to binary\n const binaryString = atob(pemContents);\n\n // Convert binary string to Uint8Array\n const binaryData = new Uint8Array(binaryString.length);\n for (let i = 0; i < binaryString.length; i++) {\n binaryData[i] = binaryString.charCodeAt(i);\n }\n\n return await crypto.subtle.importKey(\n 'pkcs8',\n binaryData,\n { name: 'RSASSA-PKCS1-v1_5', hash: 'SHA-256' },\n true,\n ['sign'],\n );\n};\n\nconst buildJwt = async (credentials: GoogleCredentials) => {\n const now = Math.floor(Date.now() / 1000);\n\n // Only include kid in header if privateKeyId is provided\n const header: { alg: string; typ: string; kid?: string } = {\n alg: 'RS256',\n typ: 'JWT',\n };\n\n if (credentials.privateKeyId) {\n header.kid = credentials.privateKeyId;\n }\n\n const payload = {\n iss: credentials.clientEmail,\n scope: 'https://www.googleapis.com/auth/cloud-platform',\n aud: 'https://oauth2.googleapis.com/token',\n exp: now + 3600,\n iat: now,\n };\n\n const privateKey = await importPrivateKey(credentials.privateKey);\n\n const signingInput = `${base64url(JSON.stringify(header))}.${base64url(\n JSON.stringify(payload),\n )}`;\n const encoder = new TextEncoder();\n const data = encoder.encode(signingInput);\n\n const signature = await crypto.subtle.sign(\n 'RSASSA-PKCS1-v1_5',\n privateKey,\n data,\n );\n\n const signatureBase64 = base64url(\n String.fromCharCode(...new Uint8Array(signature)),\n );\n\n return `${base64url(JSON.stringify(header))}.${base64url(\n JSON.stringify(payload),\n )}.${signatureBase64}`;\n};\n\n/**\n * Generate an authentication token for Google Vertex AI in a manner compatible\n * with the Edge runtime.\n */\nexport async function generateAuthToken(credentials?: GoogleCredentials) {\n try {\n const creds = credentials || (await loadCredentials());\n const jwt = await buildJwt(creds);\n\n const response = await fetch('https://oauth2.googleapis.com/token', {\n method: 'POST',\n headers: { 'Content-Type': 'application/x-www-form-urlencoded' },\n body: new URLSearchParams({\n grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',\n assertion: jwt,\n }),\n });\n\n if (!response.ok) {\n throw new Error(`Token request failed: ${response.statusText}`);\n }\n\n const data = await response.json();\n return data.access_token;\n } catch (error) {\n throw error;\n }\n}\n","import {\n LanguageModelV1,\n NoSuchModelError,\n ProviderV1,\n} from '@ai-sdk/provider';\nimport {\n FetchFunction,\n Resolvable,\n loadOptionalSetting,\n withoutTrailingSlash,\n} from '@ai-sdk/provider-utils';\nimport {\n anthropicTools,\n AnthropicMessagesLanguageModel,\n AnthropicMessagesModelId,\n AnthropicMessagesSettings,\n} from '@ai-sdk/anthropic/internal';\nimport {\n GoogleVertexAnthropicMessagesModelId,\n GoogleVertexAnthropicMessagesSettings,\n} from './google-vertex-anthropic-messages-settings';\nexport interface GoogleVertexAnthropicProvider extends ProviderV1 {\n /**\nCreates a model for text generation.\n*/\n (\n modelId: GoogleVertexAnthropicMessagesModelId,\n settings?: AnthropicMessagesSettings,\n ): LanguageModelV1;\n\n /**\nCreates a model for text generation.\n*/\n languageModel(\n modelId: GoogleVertexAnthropicMessagesModelId,\n settings?: AnthropicMessagesSettings,\n ): LanguageModelV1;\n\n /**\nAnthropic-specific computer use tool.\n */\n tools: typeof anthropicTools;\n}\n\nexport interface GoogleVertexAnthropicProviderSettings {\n /**\n * Google Cloud project ID. Defaults to the value of the `GOOGLE_VERTEX_PROJECT` environment variable.\n */\n project?: string;\n\n /**\n * Google Cloud region. Defaults to the value of the `GOOGLE_VERTEX_LOCATION` environment variable.\n */\n location?: string;\n\n /**\nUse a different URL prefix for API calls, e.g. to use proxy servers.\nThe default prefix is `https://api.anthropic.com/v1`.\n */\n baseURL?: string;\n\n /**\nCustom headers to include in the requests.\n */\n headers?: Resolvable<Record<string, string | undefined>>;\n\n /**\nCustom fetch implementation. You can use it as a middleware to intercept requests,\nor to provide a custom fetch implementation for e.g. testing.\n */\n fetch?: FetchFunction;\n}\n\n/**\nCreate a Google Vertex Anthropic provider instance.\n */\nexport function createVertexAnthropic(\n options: GoogleVertexAnthropicProviderSettings = {},\n): GoogleVertexAnthropicProvider {\n const location = loadOptionalSetting({\n settingValue: options.location,\n environmentVariableName: 'GOOGLE_VERTEX_LOCATION',\n });\n const project = loadOptionalSetting({\n settingValue: options.project,\n environmentVariableName: 'GOOGLE_VERTEX_PROJECT',\n });\n const baseURL =\n withoutTrailingSlash(options.baseURL) ??\n `https://${location}-aiplatform.googleapis.com/v1/projects/${project}/locations/${location}/publishers/anthropic/models`;\n\n const createChatModel = (\n modelId: GoogleVertexAnthropicMessagesModelId,\n settings: GoogleVertexAnthropicMessagesSettings = {},\n ) =>\n new AnthropicMessagesLanguageModel(\n modelId as AnthropicMessagesModelId,\n settings,\n {\n provider: 'vertex.anthropic.messages',\n baseURL,\n headers: options.headers ?? {},\n fetch: options.fetch,\n buildRequestUrl: (baseURL, isStreaming) =>\n `${baseURL}/${modelId}:${\n isStreaming ? 'streamRawPredict' : 'rawPredict'\n }`,\n transformRequestBody: args => {\n // Remove model from args and add anthropic version\n const { model, ...rest } = args;\n return {\n ...rest,\n anthropic_version: 'vertex-2023-10-16',\n };\n },\n },\n );\n\n const provider = function (\n modelId: GoogleVertexAnthropicMessagesModelId,\n settings?: GoogleVertexAnthropicMessagesSettings,\n ) {\n if (new.target) {\n throw new Error(\n 'The Anthropic model function cannot be called with the new keyword.',\n );\n }\n\n return createChatModel(modelId, settings);\n };\n\n provider.languageModel = createChatModel;\n provider.chat = createChatModel;\n provider.messages = createChatModel;\n provider.textEmbeddingModel = (modelId: string) => {\n throw new NoSuchModelError({ modelId, modelType: 'textEmbeddingModel' });\n };\n\n provider.tools = anthropicTools;\n\n return provider as GoogleVertexAnthropicProvider;\n}\n","import {\n generateAuthToken,\n GoogleCredentials,\n} from '../../edge/google-vertex-auth-edge';\nimport {\n createVertexAnthropic as createVertexAnthropicOriginal,\n GoogleVertexAnthropicProvider,\n GoogleVertexAnthropicProviderSettings as GoogleVertexAnthropicProviderSettingsOriginal,\n} from '../google-vertex-anthropic-provider';\n\nexport type { GoogleVertexAnthropicProvider };\n\nexport interface GoogleVertexAnthropicProviderSettings\n extends GoogleVertexAnthropicProviderSettingsOriginal {\n /**\n * Optional. The Google credentials for the Google Cloud service account. If\n * not provided, the Google Vertex provider will use environment variables to\n * load the credentials.\n */\n googleCredentials?: GoogleCredentials;\n}\n\nexport function createVertexAnthropic(\n options: GoogleVertexAnthropicProviderSettings = {},\n): GoogleVertexAnthropicProvider {\n return createVertexAnthropicOriginal({\n ...options,\n headers:\n options.headers ??\n (async () => ({\n Authorization: `Bearer ${await generateAuthToken(\n options.googleCredentials,\n )}`,\n })),\n });\n}\n\n/**\n * Default Google Vertex AI Anthropic provider instance.\n */\nexport const vertexAnthropic = createVertexAnthropic();\n"],"mappings":";AAAA,SAAS,qBAAqB,mBAAmB;AAsBjD,IAAM,kBAAkB,YAAwC;AAC9D,MAAI;AACF,WAAO;AAAA,MACL,aAAa,YAAY;AAAA,QACvB,cAAc;AAAA,QACd,aAAa;AAAA,QACb,yBAAyB;AAAA,QACzB,aAAa;AAAA,MACf,CAAC;AAAA,MACD,YAAY,YAAY;AAAA,QACtB,cAAc;AAAA,QACd,aAAa;AAAA,QACb,yBAAyB;AAAA,QACzB,aAAa;AAAA,MACf,CAAC;AAAA,MACD,cAAc,oBAAoB;AAAA,QAChC,cAAc;AAAA,QACd,yBAAyB;AAAA,MAC3B,CAAC;AAAA,IACH;AAAA,EACF,SAAS,OAAY;AACnB,UAAM,IAAI,MAAM,sCAAsC,MAAM,OAAO,EAAE;AAAA,EACvE;AACF;AAGA,IAAM,YAAY,CAAC,QAAgB;AACjC,SAAO,KAAK,GAAG,EAAE,QAAQ,OAAO,G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;AAAA,MACA;AAAA,MACA;AAAA,QACE,UAAU;AAAA,QACV;AAAA,QACA,UAASA,MAAA,QAAQ,YAAR,OAAAA,MAAmB,CAAC;AAAA,QAC7B,OAAO,QAAQ;AAAA,QACf,iBAAiB,CAACC,UAAS,gBACzB,GAAGA,QAAO,IAAI,OAAO,IACnB,cAAc,qBAAqB,YACrC;AAAA,QACF,sBAAsB,UAAQ;AAE5B,gBAAM,EAAE,OAAO,GAAG,KAAK,IAAI;AAC3B,iBAAO;AAAA,YACL,GAAG;AAAA,YACH,mBAAmB;AAAA,UACrB;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA;AAEF,QAAM,WAAW,SACf,SACA,UACA;AACA,QAAI,YAAY;AACd,YAAM,IAAI;AAAA,QACR;AAAA,MACF;AAAA,IACF;AAEA,WAAO,gBAAgB,SAAS,QAAQ;AAAA,EAC1C;AAEA,WAAS,gBAAgB;AACzB,WAAS,OAAO;AAChB,WAAS,WAAW;AACpB,WAAS,qBAAqB,CAAC,YAAoB;AACjD,UAAM,IAAI,iBAAiB,EAAE,SAAS,WAAW,qBAAqB,CAAC;AAAA,EACzE;AAEA,WAAS,QAAQ;AAEjB,SAAO;AACT;;;ACvHO,SAASC,uBACd,UAAiD,CAAC,GACnB;AAxBjC;AAyBE,SAAO,sBAA8B;AAAA,IACnC,GAAG;AAAA,IACH,UACE,aAAQ,YAAR,YACC,aAAa;AAAA,MACZ,eAAe,UAAU,MAAM;AAAA,QAC7B,QAAQ;AAAA,MACV,CAAC;AAAA,IACH;AAAA,EACJ,CAAC;AACH;AAKO,IAAM,kBAAkBA,uBAAsB;","names":["loadOptionalSetting","_a","baseURL","createVertexAnthropic"]}
|
package/dist/index.d.mts
CHANGED
@@ -1,30 +1,10 @@
|
|
1
1
|
import { ProviderV1, LanguageModelV1 } from '@ai-sdk/provider';
|
2
|
-
import {
|
2
|
+
import { Resolvable, FetchFunction } from '@ai-sdk/provider-utils';
|
3
|
+
import { InternalGoogleGenerativeAISettings } from '@ai-sdk/google/internal';
|
4
|
+
import { GoogleAuthOptions } from 'google-auth-library';
|
3
5
|
|
4
|
-
type GoogleVertexModelId = 'gemini-1.5-flash' | 'gemini-1.5-flash-001' | 'gemini-1.5-flash-002' | 'gemini-1.5-pro' | 'gemini-1.5-pro-001' | 'gemini-1.5-pro-002' | 'gemini-1.0-pro' | 'gemini-1.0-pro-001' | 'gemini-1.0-pro
|
5
|
-
interface GoogleVertexSettings {
|
6
|
-
/**
|
7
|
-
* Optional. Enable structured output. Default is true.
|
8
|
-
*
|
9
|
-
* This is useful when the JSON Schema contains elements that are
|
10
|
-
* not supported by the OpenAPI schema version that
|
11
|
-
* Google Generative AI uses. You can use this to disable
|
12
|
-
* structured outputs if you need to.
|
13
|
-
*/
|
14
|
-
structuredOutputs?: boolean;
|
15
|
-
/**
|
16
|
-
Optional. A list of unique safety settings for blocking unsafe content.
|
17
|
-
*/
|
18
|
-
safetySettings?: Array<{
|
19
|
-
category: 'HARM_CATEGORY_UNSPECIFIED' | 'HARM_CATEGORY_HATE_SPEECH' | 'HARM_CATEGORY_DANGEROUS_CONTENT' | 'HARM_CATEGORY_HARASSMENT' | 'HARM_CATEGORY_SEXUALLY_EXPLICIT';
|
20
|
-
threshold: 'HARM_BLOCK_THRESHOLD_UNSPECIFIED' | 'BLOCK_LOW_AND_ABOVE' | 'BLOCK_MEDIUM_AND_ABOVE' | 'BLOCK_ONLY_HIGH' | 'BLOCK_NONE';
|
21
|
-
}>;
|
22
|
-
/**
|
23
|
-
Optional. When enabled, the model will use Google search to ground the response.
|
24
|
-
|
25
|
-
@see https://cloud.google.com/vertex-ai/generative-ai/docs/grounding/overview
|
26
|
-
*/
|
27
|
-
useSearchGrounding?: boolean;
|
6
|
+
type GoogleVertexModelId = 'gemini-1.5-flash' | 'gemini-1.5-flash-001' | 'gemini-1.5-flash-002' | 'gemini-1.5-pro' | 'gemini-1.5-pro-001' | 'gemini-1.5-pro-002' | 'gemini-1.0-pro-001' | 'gemini-1.0-pro-vision-001' | 'gemini-1.0-pro' | 'gemini-1.0-pro-001' | 'gemini-1.0-pro-002' | (string & {});
|
7
|
+
interface GoogleVertexSettings extends InternalGoogleGenerativeAISettings {
|
28
8
|
}
|
29
9
|
|
30
10
|
interface GoogleVertexProvider extends ProviderV1 {
|
@@ -34,7 +14,7 @@ interface GoogleVertexProvider extends ProviderV1 {
|
|
34
14
|
(modelId: GoogleVertexModelId, settings?: GoogleVertexSettings): LanguageModelV1;
|
35
15
|
languageModel: (modelId: GoogleVertexModelId, settings?: GoogleVertexSettings) => LanguageModelV1;
|
36
16
|
}
|
37
|
-
interface GoogleVertexProviderSettings {
|
17
|
+
interface GoogleVertexProviderSettings$1 {
|
38
18
|
/**
|
39
19
|
Your Google Vertex location. Defaults to the environment variable `GOOGLE_VERTEX_LOCATION`.
|
40
20
|
*/
|
@@ -43,22 +23,32 @@ interface GoogleVertexProviderSettings {
|
|
43
23
|
Your Google Vertex project. Defaults to the environment variable `GOOGLE_VERTEX_PROJECT`.
|
44
24
|
*/
|
45
25
|
project?: string;
|
26
|
+
/**
|
27
|
+
* Headers to use for requests. Can be:
|
28
|
+
* - A headers object
|
29
|
+
* - A Promise that resolves to a headers object
|
30
|
+
* - A function that returns a headers object
|
31
|
+
* - A function that returns a Promise of a headers object
|
32
|
+
*/
|
33
|
+
headers?: Resolvable<Record<string, string | undefined>>;
|
34
|
+
/**
|
35
|
+
Custom fetch implementation. You can use it as a middleware to intercept requests,
|
36
|
+
or to provide a custom fetch implementation for e.g. testing.
|
37
|
+
*/
|
38
|
+
fetch?: FetchFunction;
|
39
|
+
generateId?: () => string;
|
40
|
+
}
|
41
|
+
|
42
|
+
interface GoogleVertexProviderSettings extends GoogleVertexProviderSettings$1 {
|
46
43
|
/**
|
47
44
|
Optional. The Authentication options provided by google-auth-library.
|
48
45
|
Complete list of authentication options is documented in the
|
49
46
|
GoogleAuthOptions interface:
|
50
47
|
https://github.com/googleapis/google-auth-library-nodejs/blob/main/src/auth/googleauth.ts.
|
51
48
|
*/
|
52
|
-
googleAuthOptions?:
|
53
|
-
generateId?: () => string;
|
54
|
-
createVertexAI?: ({ project, location, }: {
|
55
|
-
project: string;
|
56
|
-
location: string;
|
57
|
-
}) => VertexAI;
|
49
|
+
googleAuthOptions?: GoogleAuthOptions;
|
58
50
|
}
|
59
|
-
|
60
|
-
Create a Google Vertex AI provider instance.
|
61
|
-
*/
|
51
|
+
|
62
52
|
declare function createVertex(options?: GoogleVertexProviderSettings): GoogleVertexProvider;
|
63
53
|
/**
|
64
54
|
Default Google Vertex AI provider instance.
|