@ai-sdk/google-vertex
Version:
The **[Google Vertex provider](https://ai-sdk.dev/providers/ai-sdk-providers/google-vertex)** for the [AI SDK](https://ai-sdk.dev/docs) contains language model support for the [Google Vertex AI](https://cloud.google.com/vertex-ai) APIs.
259 lines (254 loc) • 8.06 kB
JavaScript
// src/xai/edge/google-vertex-xai-provider-edge.ts
import { resolve } from "@ai-sdk/provider-utils";
// src/edge/google-vertex-auth-edge.ts
import {
loadOptionalSetting,
loadSetting,
withUserAgentSuffix,
getRuntimeEnvironmentUserAgent
} from "@ai-sdk/provider-utils";
// src/version.ts
var VERSION = true ? "4.0.137" : "0.0.0-test";
// src/edge/google-vertex-auth-edge.ts
var loadCredentials = async () => {
try {
return {
clientEmail: loadSetting({
settingValue: void 0,
settingName: "clientEmail",
environmentVariableName: "GOOGLE_CLIENT_EMAIL",
description: "Google client email"
}),
privateKey: loadSetting({
settingValue: void 0,
settingName: "privateKey",
environmentVariableName: "GOOGLE_PRIVATE_KEY",
description: "Google private key"
}),
privateKeyId: loadOptionalSetting({
settingValue: void 0,
environmentVariableName: "GOOGLE_PRIVATE_KEY_ID"
})
};
} catch (error) {
throw new Error(`Failed to load Google credentials: ${error.message}`);
}
};
var base64url = (str) => {
return btoa(str).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
};
var importPrivateKey = async (pemKey) => {
const pemHeader = "-----BEGIN PRIVATE KEY-----";
const pemFooter = "-----END PRIVATE KEY-----";
const pemContents = pemKey.replace(pemHeader, "").replace(pemFooter, "").replace(/\s/g, "");
const binaryString = atob(pemContents);
const binaryData = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) {
binaryData[i] = binaryString.charCodeAt(i);
}
return await crypto.subtle.importKey(
"pkcs8",
binaryData,
{ name: "RSASSA-PKCS1-v1_5", hash: "SHA-256" },
true,
["sign"]
);
};
var buildJwt = async (credentials) => {
const now = Math.floor(Date.now() / 1e3);
const header = {
alg: "RS256",
typ: "JWT"
};
if (credentials.privateKeyId) {
header.kid = credentials.privateKeyId;
}
const payload = {
iss: credentials.clientEmail,
scope: "https://www.googleapis.com/auth/cloud-platform",
aud: "https://oauth2.googleapis.com/token",
exp: now + 3600,
iat: now
};
const privateKey = await importPrivateKey(credentials.privateKey);
const signingInput = `${base64url(JSON.stringify(header))}.${base64url(
JSON.stringify(payload)
)}`;
const encoder = new TextEncoder();
const data = encoder.encode(signingInput);
const signature = await crypto.subtle.sign(
"RSASSA-PKCS1-v1_5",
privateKey,
data
);
const signatureBase64 = base64url(
String.fromCharCode(...new Uint8Array(signature))
);
return `${base64url(JSON.stringify(header))}.${base64url(
JSON.stringify(payload)
)}.${signatureBase64}`;
};
async function generateAuthToken(credentials) {
try {
const creds = credentials || await loadCredentials();
const jwt = await buildJwt(creds);
const response = await fetch("https://oauth2.googleapis.com/token", {
method: "POST",
headers: withUserAgentSuffix(
{ "Content-Type": "application/x-www-form-urlencoded" },
`ai-sdk/google-vertex/${VERSION}`,
getRuntimeEnvironmentUserAgent()
),
body: new URLSearchParams({
grant_type: "urn:ietf:params:oauth:grant-type:jwt-bearer",
assertion: jwt
})
});
if (!response.ok) {
throw new Error(`Token request failed: ${response.statusText}`);
}
const data = await response.json();
return data.access_token;
} catch (error) {
throw error;
}
}
// src/xai/google-vertex-xai-provider.ts
import {
NoSuchModelError
} from "@ai-sdk/provider";
import {
createOpenAICompatible
} from "@ai-sdk/openai-compatible";
import {
loadOptionalSetting as loadOptionalSetting2,
loadSetting as loadSetting2,
withoutTrailingSlash
} from "@ai-sdk/provider-utils";
function convertGoogleVertexXaiUsage(usage) {
var _a, _b, _c, _d, _e, _f;
if (usage == null) {
return {
inputTokens: {
total: void 0,
noCache: void 0,
cacheRead: void 0,
cacheWrite: void 0
},
outputTokens: {
total: void 0,
text: void 0,
reasoning: void 0
},
raw: void 0
};
}
const promptTokens = (_a = usage.prompt_tokens) != null ? _a : 0;
const completionTokens = (_b = usage.completion_tokens) != null ? _b : 0;
const cacheReadTokens = (_d = (_c = usage.prompt_tokens_details) == null ? void 0 : _c.cached_tokens) != null ? _d : 0;
const reasoningTokens = (_f = (_e = usage.completion_tokens_details) == null ? void 0 : _e.reasoning_tokens) != null ? _f : 0;
return {
inputTokens: {
total: promptTokens,
noCache: promptTokens - cacheReadTokens,
cacheRead: cacheReadTokens,
cacheWrite: void 0
},
outputTokens: {
total: completionTokens + reasoningTokens,
text: completionTokens,
reasoning: reasoningTokens
},
raw: usage
};
}
function transformGoogleVertexXaiRequestBody(args) {
const { reasoning_effort: _reasoningEffort, ...rest } = args;
return rest;
}
function createGoogleVertexXai(options = {}) {
const loadLocation = () => loadOptionalSetting2({
settingValue: options.location,
environmentVariableName: "GOOGLE_VERTEX_LOCATION"
});
const loadProject = () => loadSetting2({
settingValue: options.project,
settingName: "project",
environmentVariableName: "GOOGLE_VERTEX_PROJECT",
description: "Google Vertex project"
});
const constructBaseURL = () => {
var _a;
const projectId = loadProject();
const location = (_a = loadLocation()) != null ? _a : "global";
return `https://aiplatform.googleapis.com/v1/projects/${projectId}/locations/${location}/endpoints/openapi`;
};
const loadBaseURL = () => {
var _a;
return withoutTrailingSlash((_a = options.baseURL) != null ? _a : "") || constructBaseURL();
};
let cachedProvider;
const getProvider = () => cachedProvider != null ? cachedProvider : cachedProvider = createOpenAICompatible({
name: "googleVertex.xai",
baseURL: loadBaseURL(),
fetch: options.fetch,
includeUsage: true,
supportsStructuredOutputs: true,
supportedUrls: () => ({
"image/*": [/^https?:\/\/.*$/]
}),
transformRequestBody: transformGoogleVertexXaiRequestBody,
convertUsage: convertGoogleVertexXaiUsage
});
const createChatModel = (modelId) => getProvider().languageModel(modelId);
const provider = function(modelId) {
if (new.target) {
throw new Error(
"The Google Vertex xAI model function cannot be called with the new keyword."
);
}
return createChatModel(modelId);
};
provider.specificationVersion = "v3";
provider.languageModel = createChatModel;
provider.chatModel = (modelId) => getProvider().chatModel(modelId);
provider.embeddingModel = (modelId) => {
throw new NoSuchModelError({ modelId, modelType: "embeddingModel" });
};
provider.textEmbeddingModel = provider.embeddingModel;
provider.imageModel = (modelId) => {
throw new NoSuchModelError({ modelId, modelType: "imageModel" });
};
return provider;
}
// src/xai/edge/google-vertex-xai-provider-edge.ts
function createGoogleVertexXai2(options = {}) {
const customFetch = async (url, init) => {
var _a;
const token = await generateAuthToken(options.googleCredentials);
const resolvedHeaders = await resolve(options.headers);
const authHeaders = {
...resolvedHeaders,
Authorization: `Bearer ${token}`
};
const fetchInit = {
...init,
headers: {
...init == null ? void 0 : init.headers,
...authHeaders
}
};
return ((_a = options.fetch) != null ? _a : fetch)(url, fetchInit);
};
return createGoogleVertexXai({
...options,
fetch: customFetch,
headers: void 0
});
}
var googleVertexXai = createGoogleVertexXai2();
export {
createGoogleVertexXai2 as createGoogleVertexXai,
googleVertexXai
};
//# sourceMappingURL=index.mjs.map