Skip to content

Commit

Permalink
faster resposne time
Browse files Browse the repository at this point in the history
  • Loading branch information
felixlaii committed Mar 17, 2024
1 parent 1a59bdf commit f7cbb5f
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,7 @@ const Home: React.FC = () => {
MessageProps[]
>([]);

// Handler for sending messages, and conversation history while calling OpenAI API
const handleSendMessage = async (message: string) => {
setConversationHistory([
...conversationHistory,
{ role: "user", content: message },
]);

try {
const response = await fetch("/api/hello", {
method: "POST",
Expand All @@ -22,31 +16,33 @@ const Home: React.FC = () => {
},
body: JSON.stringify({
text: message,
conversationHistory: conversationHistory, // use current state here
conversationHistory, // Use current state here
}),
});

if (response.ok) {
const data = await response.json();
setConversationHistory((prevConversationHistory) => [
...prevConversationHistory,
{ role: "assistant", content: data.assistantMessage }, // Update user message here
{ role: "user", content: message }, // Update user message here
{ role: "assistant", content: data.assistantMessage },
]);
} else {
console.error("Error calling API:", response.statusText);
// Handle error gracefully (e.g., display a message to the user)
}
} catch (error) {
console.error("Error calling API:", error);
// Handle error gracefully (e.g., display a message to the user)
}
};

return (
<div className="flex flex-column mt-14">
<div className="w-full justify-center">
<div>
<h1 className="text-center font-mono text-xl text-gray-600 font-bold tracking-widest mt-6">
ChatGPT Clone
</h1>
</div>
<div className="flex justify-center mt-14">
<div className="w-full max-w-md">
<h1 className="text-center font-mono text-xl text-gray-600 font-bold tracking-widest mt-6">
ChatGPT Clone
</h1>
<ChatBox
conversationHistory={conversationHistory}
onSendMessage={handleSendMessage}
Expand Down

0 comments on commit f7cbb5f

Please sign in to comment.