new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

AI SDK · Cookbook · all subjects

multi-modal/routing

2 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Route handler for multi-modal chat

Create a route handler at app/api/chat/route.ts that imports streamText, convertToModelMessages, createUIMessageStreamResponse, toUIMessageStream, and UIMessage type from 'ai'. Set maxDuration export to 30 seconds. The POST handler extracts messages from request JSON, converts UI messages to model messages using convertToModelMessages, calls streamText with model 'openai/gpt-4o' and converted messages, passes the result.stream to toUIMessageStream, and returns the response using createUIMessageStreamResponse.

Route handler code example

```tsx import { streamText, convertToModelMessages, createUIMessageStreamResponse, toUIMessageStream, type UIMessage, } from 'ai'; export const maxDuration = 30; export async function POST(req: Request) { const { messages }: { messages: UIMessage[] } = await req.json(); const result = streamText({ model: 'openai/gpt-4o', messages: await convertToModelMessages(messages), }); return createUIMessageStreamResponse({ stream: toUIMessageStream({ stream: result.stream }), }); } ``` This code creates a POST endpoint at /api/chat that accepts UI messages, converts them to model format, streams responses from GPT-4o, and returns a streamed response object.

Give your agent this brain