Replies: 4 comments
-
Find any solution yet? I was searching a way to clear the messages state with the setMessages function built in the useChat hook but with no look |
Beta Was this translation helpful? Give feedback.
-
@gasatrya @benjaminbascary check out this example: 'use client';
import { useChat } from 'ai/react';
import { useState } from 'react';
export default function Chat() {
const [chatId, setChatId] = useState<number | undefined>(undefined);
const { messages, input, handleInputChange, handleSubmit } = useChat({
id: chatId?.toString(),
});
const handleReset = () => {
setChatId(chatId => (chatId ?? 0) + 1);
};
return (
<div className="flex flex-col w-full max-w-md py-24 mx-auto stretch">
{messages.length > 0
? messages.map(m => (
<div key={m.id} className="whitespace-pre-wrap">
{m.role === 'user' ? 'User: ' : 'AI: '}
{m.content}
</div>
))
: null}
<form onSubmit={handleSubmit}>
<input
className="fixed bottom-0 w-full max-w-md p-2 mb-8 border border-gray-300 rounded shadow-xl"
value={input}
placeholder="Say something..."
onChange={handleInputChange}
/>
<button
type="button"
className="fixed bottom-0 w-20 p-2 mb-8 text-white bg-blue-500 rounded hover:bg-blue-700"
onClick={handleReset}
style={{ transform: 'translateY(100%)' }}
>
Reset
</button>
</form>
</div>
);
} |
Beta Was this translation helpful? Give feedback.
-
How will it be different if the user is allowed to click the button to stop and then click again to re-generate new chat output. Does the above code still work? |
Beta Was this translation helpful? Give feedback.
-
Is the only way to regenerate properly to start a chat with a new id? I’m really struggling with this (though using svelte). Was really expecting a setMessages() + reload call to work, but no 😢 |
Beta Was this translation helpful? Give feedback.
-
I am building a text generator using the
useChat
but I want to 'restart' the chat every time a user hits the submit button. I tried thesetMessages([])
and assign an empty array, but it only removes the response. How to restart it properly, please help? Here's a live url https://outlineai.vercel.app/Beta Was this translation helpful? Give feedback.
All reactions