Skip to content

Commit

Permalink
Cleaning up auth/headers and env vars.
Browse files Browse the repository at this point in the history
  • Loading branch information
shaper committed Nov 30, 2024
1 parent d2ebc4c commit e9ae5cf
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 271 deletions.
4 changes: 4 additions & 0 deletions examples/ai-core/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ AZURE_API_KEY=""
AZURE_RESOURCE_NAME=""
COHERE_API_KEY=""
FIREWORKS_API_KEY=""
GOOGLE_APPLICATION_CREDENTIALS=""
GOOGLE_CLIENT_EMAIL=""
GOOGLE_GENERATIVE_AI_API_KEY=""
GOOGLE_PRIVATE_KEY=""
GOOGLE_PRIVATE_KEY_ID=""
GOOGLE_VERTEX_LOCATION=""
GOOGLE_VERTEX_PROJECT=""
GROQ_API_KEY=""
Expand Down
14 changes: 11 additions & 3 deletions examples/ai-core/src/e2e/google-vertex.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@ import {
embedMany,
} from 'ai';
import fs from 'fs';
import { generateAuthTokenEdgeCompatible as generateAuthToken } from '@ai-sdk/google-vertex';
import { generateAuthTokenEdgeCompatible as generateAuthToken } from '../lib/google-vertex-auth-edge';

const LONG_TEST_MILLIS = 10000;

// Model variants to test against
const MODEL_VARIANTS = {
chat: ['gemini-1.5-flash', 'gemini-1.5-pro-001', 'gemini-1.0-pro-001'],
chat: [
'gemini-1.5-flash',
// Pro models have low quota limits and can only be used if you have a
// Google Cloud account with appropriate billing enabled.
// 'gemini-1.5-pro-001',
// 'gemini-1.0-pro-001',
],
embedding: ['textembedding-gecko', 'textembedding-gecko-multilingual'],
} as const;

Expand All @@ -26,7 +32,9 @@ describe('Google Vertex E2E Tests', () => {
const provider = createVertex({
project: process.env.GOOGLE_VERTEX_PROJECT,
location: process.env.GOOGLE_VERTEX_LOCATION,
generateAuthToken,
experimental_getHeadersAsync: async () => ({
Authorization: `Bearer ${await generateAuthToken()}`,
}),
});

describe.each(MODEL_VARIANTS.chat)('Chat Model: %s', modelId => {
Expand Down
8 changes: 7 additions & 1 deletion examples/ai-core/src/e2e/google.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ import fs from 'fs';
const LONG_TEST_MILLIS = 10000;

const MODEL_VARIANTS = {
chat: ['gemini-1.5-flash-latest', 'gemini-1.5-pro-latest', 'gemini-1.0-pro'],
chat: [
'gemini-1.5-flash-latest',
// Pro models have low quota limits and can only be used if you have a
// Google Cloud account with appropriate billing enabled.
// 'gemini-1.5-pro-latest',
// 'gemini-1.0-pro',
],
embedding: ['text-embedding-004'],
} as const;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,15 @@ export interface GoogleCredentials {
privateKeyId: string;
}

// Helper to load credentials from either file or env vars
const loadCredentials = async (): Promise<GoogleCredentials> => {
try {
// First try to load from credentials file
const credsPath = process.env.GOOGLE_APPLICATION_CREDENTIALS;
if (credsPath) {
const credentials = require(credsPath);
return {
clientEmail: credentials.client_email,
privateKey: credentials.private_key,
privateKeyId: credentials.private_key_id,
};
}

// Fall back to environment variables
const clientEmail = process.env.GOOGLE_CLIENT_EMAIL;
const privateKey = process.env.GOOGLE_PRIVATE_KEY;
const privateKeyId = process.env.GOOGLE_PRIVATE_KEY_ID;
if (!clientEmail || !privateKey || !privateKeyId) {
throw new Error(
'Google credentials not found. Please provide either:\n' +
'1. A credentials file path in GOOGLE_APPLICATION_CREDENTIALS, or\n' +
'2. GOOGLE_CLIENT_EMAIL, GOOGLE_PRIVATE_KEY, and GOOGLE_PRIVATE_KEY_ID environment variables',
'Google credentials not found. Please provide\n' +
'GOOGLE_CLIENT_EMAIL, GOOGLE_PRIVATE_KEY, and GOOGLE_PRIVATE_KEY_ID environment variables',
);
}

Expand Down
9 changes: 2 additions & 7 deletions packages/google-vertex/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"dependencies": {
"@ai-sdk/google": "1.0.4",
"@ai-sdk/provider": "1.0.1",
"@ai-sdk/provider-utils": "2.0.2"
"@ai-sdk/provider-utils": "2.0.2",
"google-auth-library": "^9.15.0"
},
"devDependencies": {
"@types/node": "^18",
Expand All @@ -42,14 +43,8 @@
"zod": "3.23.8"
},
"peerDependencies": {
"google-auth-library": "^9.15.0",
"zod": "^3.0.0"
},
"peerDependenciesMeta": {
"google-auth-library": {
"optional": true
}
},
"engines": {
"node": ">=18"
},
Expand Down
196 changes: 0 additions & 196 deletions packages/google-vertex/src/google-vertex-auth-edge.test.ts

This file was deleted.

Loading

0 comments on commit e9ae5cf

Please sign in to comment.