Skip to content

Commit e6d2d60

Browse files
authored
Merge pull request #323 from cloudflare/palash/vertex-basic-support
Add Google Vertex support, fix Readme
2 parents 8929a25 + 7c15672 commit e6d2d60

File tree

7 files changed

+61
-273
lines changed

7 files changed

+61
-273
lines changed

.changeset/lovely-knives-read.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"ai-gateway-provider": minor
3+
---
4+
5+
Fix Google Vertex support

packages/ai-gateway-provider/README.md

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ This library provides an AI Gateway Provider for the [Vercel AI SDK](https://sdk
88

99
* **Runtime Agnostic:** Works in all JavaScript runtimes supported by the Vercel AI SDK including Node.js, Edge Runtime, and more.
1010
* **Automatic Provider Fallback:** ✨ Define an array of models and the provider will **automatically fallback** to the next available provider if one fails, ensuring high availability and resilience for your AI applications.
11-
* **Multi-Provider Support:** Easily switch between and combine models from various providers like OpenAI, Anthropic, DeepSeek, Google AI Studio, Grok, Mistral, Perplexity AI, Replicate and Groq.
12-
* **AI Gateway Integration:** Utilizes Cloudflare's AI Gateway for advanced features like request management, caching, and rate limiting.
13-
* **Simplified Model Management:** Abstracts away the complexities of interacting with different AI provider APIs.
14-
* **Easy Configuration:** Simple and intuitive setup process.
1511

1612
## Installation
1713

@@ -24,30 +20,59 @@ npm install ai-gateway-provider
2420
### Basic Example with API Key
2521

2622
```typescript
27-
import {createOpenAI} from '@ai-sdk/openai';
28-
import {createAiGateway} from 'ai-gateway-provider';
29-
import {generateText} from "ai";
30-
import {createAnthropic} from "@ai-sdk/anthropic";
23+
import { createAiGateway } from 'ai-gateway-provider';
24+
import { createOpenAI } from 'ai-gateway-provider/providers/openai';
25+
import { generateText } from "ai";
3126

3227
const aigateway = createAiGateway({
33-
accountId: "my-cloudflare-account-id",
34-
gateway: 'my-gateway-name',
35-
apiKey: 'optionally my cloudflare api key',
36-
options: { // Optional per-request override
37-
skipCache: true
38-
}
28+
accountId: "{CLOUDFLARE_ACCOUNT_ID}",
29+
gateway: '{GATEWAY_NAME}',
30+
apiKey: '{CF_AIG_TOKEN}', // If your AI Gateway has authentication enabled
3931
});
40-
const openai = createOpenAI({apiKey: 'openai api key'});
41-
const anthropic = createAnthropic({apiKey: 'anthropic api key'});
4232

33+
const openai = createOpenAI({ apiKey: '{OPENAI_API_KEY}' });
34+
35+
const { text } = await generateText({
36+
model: aigateway(openai.chat("gpt-5.1")),
37+
prompt: 'Write a vegetarian lasagna recipe for 4 people.',
38+
});
39+
```
40+
41+
### Basic Examples with BYOK / Unified Billing
42+
43+
```typescript
44+
import { createAiGateway } from 'ai-gateway-provider';
45+
import { createOpenAI } from 'ai-gateway-provider/providers/openai';
46+
import { generateText } from "ai";
47+
48+
const aigateway = createAiGateway({
49+
accountId: "{CLOUDFLARE_ACCOUNT_ID}",
50+
gateway: '{GATEWAY_NAME}',
51+
apiKey: '{CF_AIG_TOKEN}',
52+
});
53+
54+
const openai = createOpenAI();
55+
56+
const { text } = await generateText({
57+
model: aigateway(openai.chat("gpt-5.1")),
58+
prompt: 'Write a vegetarian lasagna recipe for 4 people.',
59+
});
60+
```
61+
62+
## Automatic Fallback Example
63+
64+
```typescript
65+
// Define multiple provider options with fallback priority
4366
const model = aigateway([
4467
anthropic('claude-3-5-haiku-20241022'), // Primary choice
45-
openai("gpt-4o-mini"), // Fallback if first fails
68+
openai.chat("gpt-4o-mini"), // First fallback
69+
mistral("mistral-large-latest"), // Second fallback
4670
]);
4771

72+
// The system will automatically try the next model if previous ones fail
4873
const {text} = await generateText({
49-
model: model,
50-
prompt: 'Write a vegetarian lasagna recipe for 4 people.',
74+
model,
75+
prompt: 'Suggest three names for my tech startup.',
5176
});
5277
```
5378

@@ -56,7 +81,7 @@ const {text} = await generateText({
5681
Binding Benefits:
5782
- Faster Requests: Saves milliseconds by avoiding open internet routing.
5883
- Enhanced Security: Uses a special pre-authenticated pipeline.
59-
- No API Key Required: Authentication is handled by the binding.
84+
- No Cloudflare API Token Required: Authentication is handled by the binding.
6085

6186
```typescript
6287
const aigateway = createAiGateway({
@@ -70,10 +95,10 @@ const anthropic = createAnthropic({apiKey: 'anthropic api key'});
7095

7196
const model = aigateway([
7297
anthropic('claude-3-5-haiku-20241022'), // Primary choice
73-
openai("gpt-4o-mini"), // Fallback if first fails
98+
openai.chat("gpt-4o-mini"), // Fallback if first fails
7499
]);
75100

76-
const {text} = await generateText({
101+
const { text } = await generateText({
77102
model: model,
78103
prompt: 'Write a vegetarian lasagna recipe for 4 people.',
79104
});
@@ -131,22 +156,6 @@ const aigateway = createAiGateway({
131156
* `retryDelayMs`: Delay between retries
132157
* `backoff`: Retry backoff strategy ('constant', 'linear', 'exponential')
133158

134-
## Automatic Fallback Example
135-
136-
```typescript
137-
// Define multiple provider options with fallback priority
138-
const model = aigateway([
139-
anthropic('claude-3-5-haiku-20241022'), // Primary choice
140-
openai("gpt-4o-mini"), // First fallback
141-
mistral("mistral-large-latest"), // Second fallback
142-
]);
143-
144-
// The system will automatically try the next model if previous ones fail
145-
const {text} = await generateText({
146-
model: model,
147-
prompt: 'Suggest three names for my tech startup.',
148-
});
149-
```
150159

151160
## Supported Providers
152161

packages/ai-gateway-provider/package.json

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@
2828
"import": "./dist/providers/amazon-bedrock.mjs",
2929
"require": "./dist/providers/amazon-bedrock.js"
3030
},
31-
"./providers/google-vertex": {
32-
"types": "./dist/providers/google-vertex.d.ts",
33-
"import": "./dist/providers/google-vertex.mjs",
34-
"require": "./dist/providers/google-vertex.js"
35-
},
3631
"./providers/openai": {
3732
"types": "./dist/providers/openai.d.ts",
3833
"import": "./dist/providers/openai.mjs",
@@ -117,7 +112,7 @@
117112
],
118113
"author": "Gabriel Massadas <https://github.com/g4brym>",
119114
"license": "MIT",
120-
"homepage": "https://github.com/cloudflare/ai",
115+
"homepage": "https://developers.cloudflare.com/ai-gateway/integrations/vercel-ai-sdk/",
121116
"repository": {
122117
"type": "git",
123118
"url": "git+ssh://[email protected]/cloudflare/ai.git"
@@ -141,7 +136,6 @@
141136
"@ai-sdk/elevenlabs": "^1.0.20",
142137
"@ai-sdk/fireworks": "^1.0.29",
143138
"@ai-sdk/google": "^2.0.44",
144-
"@ai-sdk/google-vertex": "^3.0.86",
145139
"@ai-sdk/groq": "^2.0.32",
146140
"@ai-sdk/mistral": "^2.0.25",
147141
"@ai-sdk/openai": "^2.0.77",

packages/ai-gateway-provider/src/providers.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ export const providers = [
2222
transformEndpoint: (url: string) =>
2323
url.replace(/^https:\/\/generativelanguage\.googleapis\.com\//, ""),
2424
},
25+
{
26+
name: "google-vertex-ai",
27+
regex: /aiplatform\.googleapis\.com/,
28+
transformEndpoint: (url: string) =>
29+
url.replace(/https:\/\/(.*)[-]?aiplatform\.googleapis\.com\//, ""),
30+
},
2531
{
2632
name: "grok",
2733
regex: /^https:\/\/api\.x\.ai\//,

packages/ai-gateway-provider/src/providers/google-vertex.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

packages/ai-gateway-provider/src/providers/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ export { createDeepSeek } from "./deepseek";
88
export { createElevenLabs } from "./elevenlabs";
99
export { createFireworks } from "./fireworks";
1010
export { createGoogleGenerativeAI } from "./google";
11-
export { createVertex } from "./google-vertex";
11+
// Temporarily removed, does not work in its current form
12+
// export { createVertex } from "./google-vertex";
1213
export { createGroq } from "./groq";
1314
export { createMistral } from "./mistral";
1415
export { createOpenAI } from "./openai";

0 commit comments

Comments
 (0)