/** * ============================================================================= * CONVERSATION EXAMPLE - Multi-turn Chat with History * ============================================================================= * * Demonstrates multi-turn conversations with maintained context. * * INTERVIEW NOTES: * - Maintains conversation history across turns * - System prompt sets the assistant's behavior * - Context allows follow-up questions * - Can clear history to start fresh */ import { useState } from 'react'; import { useMistralConversation } from '@/hooks'; export default function ConversationExample() { const [input, setInput] = useState(''); const { messages, sendMessage, loading, error, clearHistory, removeLastExchange, } = useMistralConversation( 'You are a knowledgeable coding tutor. Explain concepts clearly with examples. Be encouraging and patient.' ); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!input.trim() || loading) return; const userMessage = input; setInput(''); await sendMessage(userMessage); }; // Filter out system message for display const displayMessages = messages.filter((m) => m.role !== 'system'); return (

Multi-turn Conversation

Have a back-and-forth conversation. The AI remembers context from previous messages.

{/* Message history */}
{displayMessages.length === 0 ? (
Start a conversation by sending a message
) : (
{displayMessages.map((message, index) => (
{message.role === 'user' ? 'You' : 'Assistant'}

{message.content}

))} {loading && (
Assistant

Thinking...

)}
)}
{error && (
{error}
)} {/* Input form */}
setInput(e.target.value)} placeholder="Type your message..." className="input flex-1" disabled={loading} />
{/* Actions */}
{/* Code example */}
View code
{`const {
  messages,
  sendMessage,
  loading,
  clearHistory
} = useMistralConversation('You are a helpful tutor');

// Send messages - context is maintained
await sendMessage('What are React hooks?');
await sendMessage('Can you show an example?');
// The AI remembers the previous question

// Clear to start fresh
clearHistory();`}
        
); }