Generate Text with Chat Prompt

Chat Prompt Conversation AI SDK

Use chat-style prompts with generateText to build conversational AI with system instructions and message history.

Overview

While simple prompts work for basic tasks, chat prompts unlock powerful features: system instructions to define AI behavior, conversation history for context-aware responses, and multi-turn interactions where the AI remembers previous exchanges.

Demo

Try a multi-turn conversation. Say your name, then ask "What's my name?" - the AI remembers!

Send a message to start the conversation

Server Endpoint

The server accepts a messages array containing the full conversation history. Using ModelMessage[] type ensures proper structure.

src/routes/api/chat/+server.ts

Client Component

The client maintains local message state and sends the complete history with each request. This lets the AI maintain context across turns.

+page.svelte

Key Concepts

ModelMessage Type

Each message has a role (user/assistant/system) and content. This structure enables multi-turn conversations.

System Message

Use the system parameter to set persistent instructions. This shapes the AI's personality and response style.

Conversation History

Send all previous messages with each request. The AI uses this context to give relevant, coherent responses.