Authenticate and access Google AI/ML models and services using API keys or service account credentials through the @langchain/google-gauth npm package.
Integrate Google AI/ML models and services into your LangChain application using the `@langchain/google-gauth` package. This skill handles authentication via API keys or Google Cloud service account credentials.
This skill helps you set up and configure authentication for Google services in LangChain applications. It installs the required package, configures credential-based authorization, and provides guidance on the credential resolution hierarchy.
**Note:** If you're building browser-based or serverless applications without file system access, use `@langchain/google-webauth` instead.
Install `@langchain/google-gauth` using the project's package manager:
```bash
pnpm install @langchain/google-gauth
```
If the project uses npm or yarn, adjust accordingly.
Identify which authentication method best suits the deployment environment:
1. **API Key (simplest)** - For services that support API key authentication
2. **Service Account File** - For production applications requiring service account credentials
3. **Default Credentials** - For applications running on Google Cloud Platform
Implement one of the following authentication patterns:
**Option A: API Key (passed to constructor)**
```typescript
import { SomeGoogleService } from '@langchain/google-gauth';
const service = new SomeGoogleService({
apiKey: 'YOUR_API_KEY',
// other configuration options
});
```
**Option B: Service Account Credentials (passed to constructor)**
```typescript
import { SomeGoogleService } from '@langchain/google-gauth';
const service = new SomeGoogleService({
authInfo: {
client_email: '[email protected]',
private_key: '-----BEGIN PRIVATE KEY-----\n...',
},
// other configuration options
});
```
**Option C: API Key (environment variable)**
```bash
export API_KEY=your_api_key_here
```
**Option D: Service Account File (environment variable)**
```bash
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account-key.json
```
**Option E: Default Credentials (GCP environment)**
No configuration needed. The package automatically uses default credentials when running on:
The package resolves credentials in this priority order:
1. `apiKey` constructor parameter
2. `authInfo` constructor parameter
3. `API_KEY` environment variable
4. `GOOGLE_APPLICATION_CREDENTIALS` file path
5. Default GCP credentials
Ensure only one method is configured to avoid confusion.
Import the specific Google service class you need:
```typescript
import { GoogleVertexAI } from '@langchain/google-gauth';
import { ChatGoogleVertexAI } from '@langchain/google-gauth';
// Example: Using Vertex AI
const model = new ChatGoogleVertexAI({
apiKey: process.env.API_KEY,
model: 'gemini-pro',
temperature: 0.7,
});
const response = await model.invoke('What is LangChain?');
console.log(response);
```
```typescript
// .env file
API_KEY=your_google_api_key
// app.ts
import { ChatGoogleVertexAI } from '@langchain/google-gauth';
import { HumanMessage } from '@langchain/core/messages';
const chat = new ChatGoogleVertexAI({
apiKey: process.env.API_KEY,
model: 'gemini-pro',
});
const messages = [
new HumanMessage('Explain quantum computing in simple terms.'),
];
const result = await chat.invoke(messages);
console.log(result.content);
```
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/google-auth-for-langchain-services/raw