Vercel's AI SDK enables seamless integration with Groq, providing developers with powerful tools to leverage language models hosted on Groq for a variety of applications. By combining Vercel's cutting-edge platform with Groq's advanced inference capabilities, developers can create scalable, high-speed applications with ease.
npx create-next-app@latest my-groq-app --typescript --tailwind --src-dir
cd my-groq-app
npm install @ai-sdk/groq ai
npm install react-markdown
.env.local
file in your project root and configure your Groq API Key:GROQ_API_KEY="your-api-key"
mkdir -p src/app/api/chat
route.ts
in app/api/chat
:import { groq } from '@ai-sdk/groq';
import { generateText } from 'ai';
// Allow streaming responses up to 30 seconds
export const maxDuration = 30;
export async function POST(req: Request) {
const { messages } = await req.json();
const result = streamText({
model: groq('llama-3.3-70b-versatile'),
messages,
});
return result.toDataStreamResponse();
}
Challenge: Now that you have your basic chat interface working, try enhancing it to create a specialized code explanation assistant!
app/page.tsx
file:'use client';
import { useChat } from 'ai/react';
export default function Chat() {
const { messages, input, handleInputChange, handleSubmit } = useChat();
return (
<div className="min-h-screen bg-white">
<div className="mx-auto w-full max-w-2xl py-8 px-4">
<div className="space-y-4 mb-4">
{messages.map(m => (
<div
key={m.id}
className={`flex ${m.role === 'user' ? 'justify-end' : 'justify-start'}`}
>
<div
className={`
max-w-[80%] rounded-lg px-4 py-2
${m.role === 'user'
? 'bg-blue-100 text-black'
: 'bg-gray-100 text-black'}
`}
>
<div className="text-xs text-gray-500 mb-1">
{m.role === 'user' ? 'You' : 'Llama 3.3 70B powered by Groq'}
</div>
<div className="text-sm whitespace-pre-wrap">
{m.content}
</div>
</div>
</div>
))}
</div>
<form onSubmit={handleSubmit} className="flex gap-4">
<input
value={input}
onChange={handleInputChange}
placeholder="Type your message..."
className="flex-1 rounded-lg border border-gray-300 px-4 py-2 text-black focus:outline-none focus:ring-2 focus:ring-[#f55036]"
/>
<button
type="submit"
className="rounded-lg bg-[#f55036] px-4 py-2 text-white hover:bg-[#d94530] focus:outline-none focus:ring-2 focus:ring-[#f55036]"
>
Send
</button>
</form>
</div>
</div>
);
}
npm run dev
vercel
and then running the vercel
command:The CLI will guide you through a few simple prompts:
Once you've gone through the prompts, your app will be deployed instantly and you'll receive a production URL! 🚀
npm install -g vercel
vercel
For more details on integrating Groq with the Vercel AI SDK, see the following: