Integrate Google Generative AI models (Gemini) into your application using Vercel's AI SDK. Supports text generation, streaming, and multi-modal capabilities with Google's Gemini models.
Integrate Google Generative AI (Gemini) models into your application using the Vercel AI SDK's Google provider.
This skill guides you through integrating Google's Gemini language models into your TypeScript/JavaScript application using the `@ai-sdk/google` package from Vercel's AI SDK. It supports text generation, streaming responses, and leveraging Google's powerful Gemini models.
Install the Google provider package and the AI SDK core:
```bash
npm i @ai-sdk/google ai
```
Set your Google AI API key as an environment variable:
```bash
GOOGLE_GENERATIVE_AI_API_KEY=your_api_key_here
```
Alternatively, pass it directly when creating the provider instance (not recommended for production).
Create a provider instance in your code:
```typescript
import { google } from '@ai-sdk/google';
import { generateText } from 'ai';
// Provider automatically uses GOOGLE_GENERATIVE_AI_API_KEY from env
const model = google('gemini-1.5-pro-latest');
```
Use the `generateText` function for simple text generation:
```typescript
import { google } from '@ai-sdk/google';
import { generateText } from 'ai';
const { text } = await generateText({
model: google('gemini-1.5-pro-latest'),
prompt: 'Write a vegetarian lasagna recipe for 4 people.',
});
console.log(text);
```
For streaming responses in real-time applications:
```typescript
import { google } from '@ai-sdk/google';
import { streamText } from 'ai';
const result = streamText({
model: google('gemini-1.5-flash'),
prompt: 'Explain quantum computing in simple terms.',
});
for await (const chunk of result.textStream) {
process.stdout.write(chunk);
}
```
Common Google Gemini models:
Check [Google AI Studio](https://ai.google.dev/models/gemini) for the latest models.
**Text Generation:**
```typescript
const { text } = await generateText({
model: google('gemini-1.5-pro-latest'),
prompt: 'Your prompt here',
});
```
**Structured Output:**
```typescript
import { generateObject } from 'ai';
import { z } from 'zod';
const { object } = await generateObject({
model: google('gemini-1.5-pro-latest'),
schema: z.object({
recipe: z.string(),
ingredients: z.array(z.string()),
}),
prompt: 'Create a simple pasta recipe',
});
```
**Multi-turn Conversations:**
```typescript
const { text } = await generateText({
model: google('gemini-1.5-flash'),
messages: [
{ role: 'user', content: 'Hello!' },
{ role: 'assistant', content: 'Hi! How can I help?' },
{ role: 'user', content: 'Tell me a joke.' },
],
});
```
**API Key Not Found:**
Ensure `GOOGLE_GENERATIVE_AI_API_KEY` is set in your environment or `.env` file.
**Model Not Found:**
Verify the model name is correct and available in your region.
**Rate Limit Errors:**
Implement retry logic with exponential backoff or reduce request frequency.
After integration, explore:
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/ai-sdk-google-provider-integration/raw