Implementation:Ucbepic Docetl ChatRoute
| Knowledge Sources | |
|---|---|
| Domains | Frontend, React_UI |
| Last Updated | 2026-02-08 00:00 GMT |
Overview
Concrete tool for the Next.js API route that provides AI chat functionality for the DocETL playground assistant.
Description
This API route handles chat requests from the AIChatPanel and PromptImprovementDialog components. It supports both Azure OpenAI and direct OpenAI backends, with automatic message truncation to stay within token limits (120K tokens / 500K characters). The route extracts the longest message for middle-section truncation, supports personal API keys via request headers, and tracks usage via Supabase. It conditionally sets temperature to 1 for GPT-5 models.
Usage
Called by the frontend chat components via useChat from the Vercel AI SDK. The route streams responses back to the client.
Code Reference
Source Location
- Repository: Ucbepic_Docetl
- File: website/src/app/api/chat/route.ts
- Lines: 1-191
Signature
export async function POST(req: Request): Promise<Response>
// Internal helper:
function truncateMessages(messages: ChatMessage[]): ChatMessage[]
Import
// This is an API route. Called via:
fetch("/api/chat", { method: "POST", body: JSON.stringify({ messages }) })
I/O Contract
Inputs (Props)
| Name | Type | Required | Description |
|---|---|---|---|
| messages | ChatMessage[] | Yes | Array of chat messages with role and content |
| x-openai-key | string (header) | No | Personal OpenAI API key |
| x-use-openai | string (header) | No | Set to "true" to use OpenAI instead of Azure |
| x-namespace | string (header) | No | User namespace for tracking |
| x-source | string (header) | No | Source identifier (e.g., "ai_chat") |
Outputs
| Name | Type | Description |
|---|---|---|
| stream | ReadableStream | Streamed AI chat response |
Usage Examples
const { messages, handleSubmit } = useChat({
api: "/api/chat",
headers: {
"x-use-openai": "true",
"x-openai-key": myApiKey,
"x-namespace": "my-namespace",
"x-source": "ai_chat",
},
});