Skip to content

Commit

Permalink
Merge pull request #51 from 2405-team3/feat/chatbot-loading
Browse files Browse the repository at this point in the history
feat: chatbot loading message when querying
  • Loading branch information
tlane25 authored Aug 15, 2024
2 parents 4245289 + e9a7414 commit 078242e
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions ui/src/components/Chatbot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,25 @@ export function Chatbot({ id }: ChatbotProps) {
{ role: "assistant", content: "Hello, how can I help you?" },
]);
const [input, setInput] = useState("");
const [_, setIsLoading] = useState(false);


const mutation = useMutation({
mutationFn: (message: string) => queryService.sendMessage(id, message),
onSuccess: (data) => {
const assistantMessage: Message = {
role: "assistant",
content: data.response,
source_nodes: data.source_nodes,
};
setMessages((prev) => [...prev, assistantMessage]);
setIsLoading(false);
setMessages((prev) => {
const updatedMessages = [...prev];
updatedMessages[updatedMessages.length - 1] = {
role: "assistant",
content: data.response,
source_nodes: data.source_nodes,
};
return updatedMessages;
});
},
onError: (error) => {
setIsLoading(false);
console.error("Error sending message:", error);
},
});
Expand All @@ -46,6 +53,12 @@ export function Chatbot({ id }: ChatbotProps) {
setMessages((prev) => [...prev, newMessage]);
setInput("");

setIsLoading(true);
setMessages((prev) => [
...prev,
{ role: "assistant", content: "The assistant is typing..." },
]);

mutation.mutate(input);
};

Expand Down

0 comments on commit 078242e

Please sign in to comment.