Skip to content

Commit 42f71e2

Browse files
authored
Merge pull request #29 from navapbc/fg/vertexai-provider
feat: add Vertex AI Anthropic integration and setup documentation
2 parents 43245b6 + c661187 commit 42f71e2

File tree

6 files changed

+297
-2
lines changed

6 files changed

+297
-2
lines changed

mastra-test-app/.env.example

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
# LLM API Key Configuration
12
OPENAI_API_KEY=sk-proj-123
23
ANTHROPIC_API_KEY=sk-ant-123
3-
EXA_API_KEY=abc-123
4+
EXA_API_KEY=abc-123
5+
GOOGLE_GENERATIVE_AI_API_KEY=AI...
6+
7+
# Google Vertex AI Configuration
8+
GOOGLE_VERTEX_LOCATION=us-east5
9+
GOOGLE_VERTEX_PROJECT=project-id
10+
GOOGLE_APPLICATION_CREDENTIALS=path/to/vertex-ai-credentials.json
11+
12+
# Database Configuration
13+
DATABASE_URL="postgresql://..."
14+
15+
# Authentication Configuration
16+
MASTRA_JWT_SECRET=jwt-secret
17+
MASTRA_APP_PASSWORD=your-password
18+
MASTRA_JWT_TOKEN=jwt-token

mastra-test-app/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,6 @@ mastra.db
2727
mastra.db-*
2828
mastra-memory.db
2929
mastra-memory.db-*
30+
31+
# Google Vertex AI
32+
vertex-ai-credentials.json
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
# Vertex AI Anthropic Setup Guide
2+
3+
This guide shows how to set up Google Vertex AI with Anthropic's Claude models for your Node.js application.
4+
5+
## Prerequisites
6+
7+
- Google Cloud Platform account with billing enabled
8+
- A GCP project
9+
- Node.js application with the AI SDK
10+
11+
## Step 1: Install Dependencies
12+
13+
Add the required packages to your project:
14+
15+
```bash
16+
pnpm add @ai-sdk/google-vertex google-auth-library
17+
```
18+
19+
Your `package.json` should include these versions (or newer):
20+
21+
```json
22+
{
23+
"dependencies": {
24+
"@ai-sdk/google-vertex": "^2.2.27",
25+
"google-auth-library": "^10.2.1"
26+
}
27+
}
28+
```
29+
30+
## Step 2: Configure Google Cloud
31+
32+
### Install and configure gcloud CLI:
33+
34+
```bash
35+
# Install gcloud CLI (if not already installed)
36+
# Follow instructions at: https://cloud.google.com/sdk/docs/install
37+
38+
# Set your project
39+
gcloud config set project YOUR-PROJECT-ID
40+
41+
# Enable Vertex AI API
42+
gcloud services enable aiplatform.googleapis.com
43+
44+
# Authenticate
45+
gcloud auth login
46+
gcloud auth application-default login
47+
```
48+
49+
## Step 3: Enable Anthropic Models
50+
51+
1. Go to the [Vertex AI Model Garden](https://console.cloud.google.com/vertex-ai/model-garden)
52+
2. Navigate to Anthropic models or use this direct link for Claude Sonnet 4: https://console.cloud.google.com/vertex-ai/publishers/anthropic/model-garden/claude-sonnet-4
53+
3. Click "Enable" for the models you want to use
54+
4. Complete the access request form
55+
5. Wait 1-2 business days for approval
56+
57+
## Step 4: Set Environment Variables
58+
59+
Add these variables to your `.env` file:
60+
61+
```bash
62+
# Google Vertex AI Configuration
63+
GOOGLE_VERTEX_LOCATION=us-east5
64+
GOOGLE_VERTEX_PROJECT=your-project-id
65+
GOOGLE_APPLICATION_CREDENTIALS=${PWD}/vertex-ai-credentials.json
66+
```
67+
68+
Note: Use `us-east5` as the location since it has the best model availability.
69+
70+
## Step 5: Create Service Account Credentials
71+
72+
### Option A: Using Google Cloud Console
73+
74+
1. Go to [Google Cloud Console IAM](https://console.cloud.google.com/iam-admin/serviceaccounts)
75+
2. Create a new service account or use an existing one
76+
3. Add the `Vertex AI User` role
77+
4. Generate a JSON key file
78+
5. Save it as `vertex-ai-credentials.json` in your project root
79+
80+
### Option B: Using gcloud CLI
81+
82+
Create a service account and generate credentials using the gcloud CLI:
83+
84+
```bash
85+
# Create a service account
86+
gcloud iam service-accounts create vertex-ai \
87+
--display-name="Vertex AI Service Account"
88+
89+
# Add the Vertex AI User role
90+
gcloud projects add-iam-policy-binding YOUR-PROJECT-ID \
91+
--member="serviceAccount:[email protected]" \
92+
--role="roles/aiplatform.user"
93+
94+
# Generate and download the credentials JSON file
95+
gcloud iam service-accounts keys create vertex-ai-credentials.json \
96+
97+
```
98+
99+
Replace `YOUR-PROJECT-ID` with your actual Google Cloud project ID.
100+
101+
## Step 6: Use in Your Code
102+
103+
Import and use the Vertex AI Anthropic provider:
104+
105+
```typescript
106+
import { vertexAnthropic } from '@ai-sdk/google-vertex/anthropic';
107+
108+
// Use in your agent or application
109+
const model = vertexAnthropic('claude-sonnet-4@20250514');
110+
```
111+
112+
## Step 7: Test the Setup
113+
114+
Test your configuration with a curl request:
115+
116+
```bash
117+
curl -X POST \
118+
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
119+
-H "Content-Type: application/json; charset=utf-8" \
120+
-d '{
121+
"anthropic_version": "vertex-2023-10-16",
122+
"messages": [
123+
{
124+
"role": "user",
125+
"content": "Hello"
126+
}
127+
],
128+
"max_tokens": 100
129+
}' \
130+
"https://aiplatform.googleapis.com/v1/projects/YOUR-PROJECT-ID/locations/global/publishers/anthropic/models/claude-sonnet-4@20250514:streamRawPredict"
131+
```
132+
133+
Replace `YOUR-PROJECT-ID` with your actual Google Cloud project ID.
134+
135+
## Troubleshooting
136+
137+
**Model not found errors**: Verify you have access to the model in the specified region and that it's enabled in Model Garden.
138+
139+
**Authentication errors**: Check that your service account has the correct permissions and that the credentials file path is correct.
140+
141+
**Quota errors**: Request quota increases in the [Cloud Console](https://console.cloud.google.com/iam-admin/quotas).
142+
143+
## Helpful Resources
144+
145+
- [Vertex AI Documentation](https://cloud.google.com/vertex-ai/docs)
146+
- [Anthropic Claude Code Vertex AI Guide](https://docs.anthropic.com/en/docs/claude-code/google-vertex-ai)
147+
- [AI SDK Google Vertex Documentation](https://sdk.vercel.ai/providers/ai-sdk-providers/google-vertex)

mastra-test-app/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"@ai-sdk/anthropic": "^1.2.12",
2929
"@ai-sdk/google": "^1.2.22",
3030
"@ai-sdk/openai": "^1.3.23",
31+
"@ai-sdk/google-vertex": "^2.2.27",
3132
"@inquirer/prompts": "^7.7.1",
3233
"@mastra/core": "^0.13.2",
3334
"@mastra/libsql": "^0.13.2",
@@ -40,6 +41,7 @@
4041
"csv-parse": "^6.1.0",
4142
"dotenv": "^17.2.1",
4243
"exa-mcp-server": "^2.0.3",
44+
"google-auth-library": "^10.2.1",
4345
"jsonwebtoken": "^9.0.2",
4446
"pg": "^8.16.3",
4547
"pino-pretty": "^13.0.0",

0 commit comments

Comments
 (0)