/** * ============================================================================= * STREAMING EXAMPLE - Real-time Token Streaming * ============================================================================= * * Demonstrates streaming chat completion with Server-Sent Events. * * INTERVIEW NOTES: * - Streaming provides better UX for long responses * - Uses SSE (Server-Sent Events) under the hood * - Can be aborted mid-stream * - Content updates in real-time as tokens arrive */ import { useState } from 'react'; import { useMistralStream } from '@/hooks'; export default function StreamingExample() { const [input, setInput] = useState('Write a short poem about programming'); const { content, loading, error, stream, abort, reset } = useMistralStream(); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!input.trim()) return; await stream([ { role: 'user', content: input }, ], { temperature: 0.9, // Higher temperature for creative content maxTokens: 500, }); }; return (
Watch the response generate in real-time, token by token.
{error && ({content} {loading && |}
{`const { content, loading, stream, abort } = useMistralStream();
// Start streaming
await stream([{ role: 'user', content: 'Write a poem' }]);
// content updates in real-time
// Call abort() to stop mid-generation`}