PatternsSDK API

Basic Chat Interface

Loading pattern preview

Problems solved

Technical challenges this implementation handles out of the box.
  • Stream chat responses with message history in the UI
  • Switch between a user-selected model and Perplexity Sonar for web search
  • Enable source and reasoning parts in the UI message stream

Use cases

Products and workflows this pattern is designed to support.
  • Basic AI chat products
  • Model-picker chat experiences
  • Search-backed assistant demos
  • AI Elements conversation shells

Setup

Requirements, wiring steps, and what this pattern adds to your project.

Getting started

Pick how you want to pull this pattern in. Then wire env vars and routes the same way.

  1. 1

    Install from the preview toolbar

    Shadcn Icon

    Copy the install command above and run it in your project — Pro patterns include a short-lived token.

  2. 2

    Add environment variables to .env.local

    OPENAI_API_KEY

  3. 3

    Point client fetches at your API routes

    Replace preview `/view/.../api/...` paths with `/api/...` in the installed files.

  4. 4

    Customize the agent and tool files

    Adapt prompts, tools, and stop conditions for your product — Copy for AI in the toolbar helps seed that work.

  5. 5

    Run your dev server and open the pattern route

    Install dependencies if needed, then start the app and verify responses.

Environment variables

Capabilities

AI SDK APIs

streamTextconvertToModelMessages

Providers

OpenAI

Files added

4 files
  • app/page.tsx
  • app/layout.tsx
  • components/chat-form.tsx
  • app/api/chat/route.ts

Dependencies

6 total

npm packages

4
AISDKlucide-reactNext.jsReact

Shadcn IconRegistry components

2
emptytooltip

Critical files

  • chat/route.ts

    API route showing the pattern for model switching (user-selected model vs perplexity/sonar for web search) and enabling sendSources/sendReasoning in toUIMessageStreamResponse.

Requirements

Complexity
Beginner
Setup time
~10 minutes
Node
24+
Memory
512MB