Spaces:
Running
Running
File size: 2,033 Bytes
abb7588 66e93d1 abb7588 f80b091 abb7588 f80b091 abb7588 f80b091 abb7588 f80b091 abb7588 f80b091 abb7588 f80b091 abb7588 f80b091 abb7588 f80b091 abb7588 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
import { useChat, type Message } from 'ai/react';
import { toast } from 'react-hot-toast';
import { useEffect, useState } from 'react';
import { MediaDetails } from '../fetch';
import { MessageWithSelectedDataset } from '../types';
const useChatWithMedia = (mediaList: MediaDetails[]) => {
const {
messages,
append,
reload,
stop,
isLoading,
input,
setInput,
setMessages,
} = useChat({
sendExtraMessageFields: true,
onResponse(response) {
if (response.status !== 200) {
toast.error(response.statusText);
}
},
initialMessages: [
{
id: 'system',
content: `For the full conversation, user have provided the following images: ${mediaList.map(media => media.name)}. Please help reply to user regarding these images`,
dataset: mediaList,
role: 'system',
},
] as MessageWithSelectedDataset[],
});
const [loadingDots, setLoadingDots] = useState('');
useEffect(() => {
let loadingInterval: NodeJS.Timeout;
if (isLoading) {
loadingInterval = setInterval(() => {
setLoadingDots(prevMessage => {
switch (prevMessage) {
case '':
return '.';
case '.':
return '..';
case '..':
return '...';
case '...':
return '';
default:
return '';
}
});
}, 500);
}
return () => {
clearInterval(loadingInterval);
};
}, [isLoading]);
const assistantLoadingMessage = {
id: 'loading',
content: loadingDots,
role: 'assistant',
};
const messageWithLoading =
isLoading &&
messages.length &&
messages[messages.length - 1].role !== 'assistant'
? [...messages, assistantLoadingMessage]
: messages;
return {
messages: messageWithLoading as MessageWithSelectedDataset[],
append,
reload,
stop,
isLoading,
input,
setInput,
};
};
export default useChatWithMedia;
|