Skip to main content
Loading navigation
Back to prompt library

Initial Project Setup and AI SDK Integration

Linked challenge: AI-Powered Regulatory Drafting Assistant

Prompt source

implementation16 lines7 sectionsNo variables1 code block
Set up a new Next.js project and integrate the Vercel AI SDK. Initialize a client for Gemini 2.5 Pro. Create a basic chat component that can send prompts to the model and display streaming responses in the UI. Ensure your project structure supports future integration of structured output validation and voice features.

```typescript
// app/api/chat/route.ts
import { GoogleGenerativeAI } from '@google/generative-ai';
import { GoogleGenerativeAIStream, Message, StreamingTextResponse } from 'ai';

const genai = new GoogleGenerativeAI(process.env.GOOGLE_API_KEY || '');

export const runtime = 'edge';

export async function POST(req: Request) {
  const { messages } = await req.json();

  const model = genai.getGenerativeModel({ model: 'gemini-pro' });
  const stream = await model.generateContentStream({
    contents: messages.map((m: Message) => ({ role: m.role, parts: [{ text: m.content }] })),
  });

  return new StreamingTextResponse(GoogleGenerativeAIStream(stream));
}
```