Core types and classes for accessing Google AI/ML models and services in an auth-independent way. Supports Gemini models through both Google AI Studio and Google Cloud Vertex AI with function/tool calling capabilities.
A skill for integrating Google AI/ML models and services into LangChain applications using the `@langchain/google-common` package and its authentication implementations.
This skill helps you integrate Google's AI/ML services (particularly Gemini models) into LangChain applications with a unified interface that works across both Google AI Studio and Google Cloud Vertex AI. It provides guidance on installation, configuration, and handling known limitations when working with Google services through LangChain.
First, identify which authentication package the user needs:
Note: `@langchain/google-common` is a shared dependency and should not be installed directly.
Based on the authentication method:
**For Google AI Studio:**
```bash
npm install @langchain/google-genai
```
**For Google Cloud Vertex AI:**
```bash
npm install @langchain/google-vertexai
```
**Google AI Studio Example:**
```typescript
import { ChatGoogleGenerativeAI } from "@langchain/google-genai";
const model = new ChatGoogleGenerativeAI({
modelName: "gemini-pro",
apiKey: process.env.GOOGLE_API_KEY,
temperature: 0.7,
});
const response = await model.invoke("Your prompt here");
```
**Vertex AI Example:**
```typescript
import { ChatVertexAI } from "@langchain/google-vertexai";
const model = new ChatVertexAI({
model: "gemini-pro",
temperature: 0.7,
});
const response = await model.invoke("Your prompt here");
```
When adding tool support, be aware of schema limitations:
```typescript
import { z } from "zod";
import { tool } from "@langchain/core/tools";
// GOOD: Simple, flat schema
const weatherTool = tool(
async ({ location, units }) => {
// Implementation
return `Weather in ${location}: 72°F`;
},
{
name: "get_weather",
description: "Get current weather for a location",
schema: z.object({
location: z.string().describe("City name"),
units: z.enum(["celsius", "fahrenheit"]).optional(),
}),
}
);
const modelWithTools = model.bindTools([weatherTool]);
const result = await modelWithTools.invoke("What's the weather in Paris?");
```
**AVOID these Zod patterns (not supported by Gemini):**
```typescript
// ❌ Discriminated unions
z.discriminatedUnion("type", [
z.object({ type: z.literal("email"), email: z.string() }),
z.object({ type: z.literal("phone"), phone: z.string() }),
])
// ❌ Union types
z.union([z.string(), z.number()])
// ❌ Positive refinement
z.number().positive()
```
**USE these alternatives:**
```typescript
// ✅ Flat object with optional fields
z.object({
contactType: z.enum(["email", "phone"]).optional(),
email: z.string().optional(),
phone: z.string().optional(),
})
// ✅ Separate optional fields
z.object({
stringValue: z.string().optional(),
numberValue: z.number().optional(),
})
// ✅ Manual minimum constraint
z.number().min(0.01).describe("Must be positive")
```
Ensure proper environment configuration:
**For Google AI Studio:**
```env
GOOGLE_API_KEY=your_api_key_here
```
**For Vertex AI:**
```env
GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account-key.json
GOOGLE_CLOUD_PROJECT=your-project-id
GOOGLE_CLOUD_LOCATION=us-central1
```
Implement robust error handling:
```typescript
try {
const response = await model.invoke(prompt);
// Handle response
} catch (error) {
if (error.message.includes("quota")) {
console.error("API quota exceeded");
} else if (error.message.includes("authentication")) {
console.error("Authentication failed - check credentials");
} else {
console.error("Unexpected error:", error);
}
}
```
**Example 1: Simple Chat Application**
```typescript
import { ChatGoogleGenerativeAI } from "@langchain/google-genai";
const chat = new ChatGoogleGenerativeAI({
modelName: "gemini-pro",
apiKey: process.env.GOOGLE_API_KEY,
});
const result = await chat.invoke("Explain quantum computing in simple terms");
console.log(result.content);
```
**Example 2: Multi-Turn Conversation**
```typescript
import { HumanMessage, AIMessage } from "@langchain/core/messages";
const messages = [
new HumanMessage("What is LangChain?"),
new AIMessage("LangChain is a framework for developing applications powered by language models..."),
new HumanMessage("How do I install it?"),
];
const response = await model.invoke(messages);
```
**Example 3: Function Calling with Multiple Tools**
```typescript
const calculator = tool(
async ({ operation, a, b }) => {
const ops = { add: a + b, subtract: a - b, multiply: a * b };
return ops[operation];
},
{
name: "calculator",
description: "Perform basic arithmetic",
schema: z.object({
operation: z.enum(["add", "subtract", "multiply"]),
a: z.number(),
b: z.number(),
}),
}
);
const modelWithCalc = model.bindTools([calculator]);
const result = await modelWithCalc.invoke("What is 25 times 4?");
```
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/langchain-google-common-integration/raw