/** * ============================================================================= * 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 (
Have a back-and-forth conversation. The AI remembers context from previous messages.
{/* Message history */}{message.content}
Thinking...
{`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();`}