Prompting
Learn how to use LLMs for text generation, completion and prompting.
The starter kit ships the AI chat described in the Chatbot guide. The examples on this page are custom additions you can build with the same AI SDK 7 and OpenAI packages already installed in the repository.
Basic Text Generation
Generate text using the generateText function:
import { openai } from '@ai-sdk/openai';
import { generateText } from 'ai';
export async function generateSummary(content: string) {
const { text } = await generateText({
model: openai('gpt-4o-mini'),
prompt: `Summarize the following content in 3 sentences:\n\n${content}`
});
return text;
}Server Actions
Use AI in Server Actions:
'use server';
import { openai } from '@ai-sdk/openai';
import { generateText } from 'ai';
export async function generateBlogPost(topic: string) {
const { text } = await generateText({
model: openai('gpt-4o-mini'),
prompt: `Write a blog post about: ${topic}`,
maxOutputTokens: 2000
});
return text;
}Structured Outputs
Generate structured JSON outputs:
import { openai } from '@ai-sdk/openai';
import { generateText, Output } from 'ai';
import { z } from 'zod';
const ProductSchema = z.object({
name: z.string(),
description: z.string(),
price: z.number(),
features: z.array(z.string())
});
export async function generateProduct(productType: string) {
const { output } = await generateText({
model: openai('gpt-4o-mini'),
output: Output.object({ schema: ProductSchema }),
prompt: `Generate a product specification for: ${productType}`
});
return output;
}Prompt Templates
Create reusable prompt templates:
export const prompts = {
summarize: (content: string) =>
`Summarize the following content in 3 sentences:\n\n${content}`,
translate: (text: string, targetLanguage: string) =>
`Translate the following text to ${targetLanguage}:\n\n${text}`,
extractKeywords: (content: string) =>
`Extract 5 key keywords from the following content:\n\n${content}`,
generateTitle: (content: string) =>
`Generate a compelling title for the following content:\n\n${content}`
};Usage:
import { openai } from '@ai-sdk/openai';
import { generateText } from 'ai';
import { prompts } from './prompts';
export async function summarizeContent(content: string) {
const { text } = await generateText({
model: openai('gpt-4o-mini'),
prompt: prompts.summarize(content)
});
return text;
}System Prompts
Use system prompts to guide model behavior:
import { openai } from '@ai-sdk/openai';
import { generateText } from 'ai';
export async function generateResponse(userInput: string) {
const { text } = await generateText({
model: openai('gpt-4o-mini'),
system:
'You are a helpful assistant that provides concise, accurate answers.',
prompt: userInput
});
return text;
}Temperature and Sampling
Control randomness and creativity:
import { openai } from '@ai-sdk/openai';
import { generateText } from 'ai';
// Creative writing (higher temperature)
export async function generateCreativeStory(prompt: string) {
const { text } = await generateText({
model: openai('gpt-4o-mini'),
prompt,
temperature: 0.9, // More creative
maxOutputTokens: 1000
});
return text;
}
// Factual content (lower temperature)
export async function generateFactualContent(prompt: string) {
const { text } = await generateText({
model: openai('gpt-4o-mini'),
prompt,
temperature: 0.2, // More deterministic
maxOutputTokens: 500
});
return text;
}Streaming Text Generation
Stream text generation for better UX:
import { openai } from '@ai-sdk/openai';
import { streamText } from 'ai';
export async function POST(req: Request) {
const { prompt } = await req.json();
const result = streamText({
model: openai('gpt-4o-mini'),
prompt
});
return result.toTextStreamResponse();
}Client-side usage:
'use client';
import { useCompletion } from '@ai-sdk/react';
import { MessageResponse } from '@/components/ai/message';
export function StreamingGenerator() {
const { completion, input, handleInputChange, handleSubmit, isLoading } =
useCompletion({
api: '/api/ai/generate',
streamProtocol: 'text'
});
return (
<div>
<MessageResponse>{completion}</MessageResponse>
<form onSubmit={handleSubmit}>
<input
value={input}
onChange={handleInputChange}
placeholder="Enter a prompt..."
/>
<button
type="submit"
disabled={isLoading}
>
Generate
</button>
</form>
</div>
);
}Custom Provider Packages
The starter kit installs the OpenAI provider only. To add another provider, follow its current instructions in the AI SDK provider directory, install the provider package and choose a model that the provider currently supports. Provider packages and model IDs are intentionally not hard-coded here because they are not part of the shipped repository.
Error Handling
Handle API errors gracefully:
import { openai } from '@ai-sdk/openai';
import { generateText } from 'ai';
export async function generateTextSafely(prompt: string) {
try {
const { text } = await generateText({
model: openai('gpt-4o-mini'),
prompt
});
return { success: true, text };
} catch (error) {
console.error('AI generation error:', error);
return {
success: false,
error: error instanceof Error ? error.message : 'Unknown error'
};
}
}Best Practices
- Use appropriate models - Choose models based on task complexity
- Set temperature wisely - Lower for factual, higher for creative
- Limit token usage - Set
maxOutputTokensto control costs - Use system prompts - Guide model behavior with system messages
- Handle errors - Always wrap AI calls in try-catch
- Cache results - Cache expensive generations when possible
- Monitor usage - Track token usage and costs