feat: Vertex AI support; add google-vertex and google-vertex-anthropic providers (#2347)

This commit is contained in:
Yuku Kotani
2025-10-04 14:10:38 +09:00
committed by GitHub
parent 395c41b748
commit e7b6ffb314
4 changed files with 91 additions and 6 deletions

View File

@@ -153,6 +153,40 @@ export namespace Provider {
},
}
},
"google-vertex": async () => {
const project = process.env["GOOGLE_CLOUD_PROJECT"] ?? process.env["GCP_PROJECT"] ?? process.env["GCLOUD_PROJECT"]
const location = process.env["GOOGLE_CLOUD_LOCATION"] ?? process.env["VERTEX_LOCATION"] ?? "us-east5"
const autoload = Boolean(project)
if (!autoload) return { autoload: false }
return {
autoload: true,
options: {
project,
location,
},
async getModel(sdk: any, modelID: string) {
const id = String(modelID).trim()
return sdk.languageModel(id)
},
}
},
"google-vertex-anthropic": async () => {
const project = process.env["GOOGLE_CLOUD_PROJECT"] ?? process.env["GCP_PROJECT"] ?? process.env["GCLOUD_PROJECT"]
const location = process.env["GOOGLE_CLOUD_LOCATION"] ?? process.env["VERTEX_LOCATION"] ?? "us-east5"
const autoload = Boolean(project)
if (!autoload) return { autoload: false }
return {
autoload: true,
options: {
project,
location,
},
async getModel(sdk: any, modelID: string) {
const id = String(modelID).trim()
return sdk.languageModel(id)
},
}
},
}
const state = Instance.state(async () => {
@@ -344,7 +378,16 @@ export namespace Provider {
const key = Bun.hash.xxHash32(JSON.stringify({ pkg, options }))
const existing = s.sdk.get(key)
if (existing) return existing
const mod = await import(await BunProc.install(pkg, "latest"))
const installedPath = await BunProc.install(pkg, "latest")
// The `google-vertex-anthropic` provider points to the `@ai-sdk/google-vertex` package.
// Ref: https://github.com/sst/models.dev/blob/0a87de42ab177bebad0620a889e2eb2b4a5dd4ab/providers/google-vertex-anthropic/provider.toml
// However, the actual export is at the subpath `@ai-sdk/google-vertex/anthropic`.
// Ref: https://ai-sdk.dev/providers/ai-sdk-providers/google-vertex#google-vertex-anthropic-provider-usage
// In addition, Bun's dynamic import logic does not support subpath imports,
// so we patch the import path to load directly from `dist`.
const modPath =
provider.id === "google-vertex-anthropic" ? `${installedPath}/dist/anthropic/index.mjs` : installedPath
const mod = await import(modPath)
if (options["timeout"] !== undefined) {
// Only override fetch if user explicitly sets timeout
options["fetch"] = async (input: any, init?: any) => {