/** * ============================================================================= * 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 (

Streaming Completion

Watch the response generate in real-time, token by token.

setInput(e.target.value)} placeholder="Enter a prompt..." className="input" disabled={loading} />
{!loading ? ( ) : ( )}
{error && (
{error}
)} {(content || loading) && (

Response:

{loading && ( Streaming... )}

{content} {loading && |}

)} {/* Code example */}
View code
{`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`}
        
); }