Skip to content

Commit 9523d42

Browse files
committed
added prompt specific
1 parent a0c8c43 commit 9523d42

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

components/chatbot/ChatBot.tsx

+41-8
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import { wait } from "@/utils/utils";
77

88
import { v4 as uuidv4 } from "uuid";
99
import { useLDClient } from "launchdarkly-react-client-sdk";
10-
import { BeatLoader } from "react-spinners";
10+
import { PulseLoader } from "react-spinners";
11+
import { useToast } from "@/components/ui/use-toast";
1112

1213
//https://sdk.vercel.ai/providers/legacy-providers/aws-bedrock
1314
export default function Chatbot() {
@@ -17,6 +18,7 @@ export default function Chatbot() {
1718
const [messages, setMessages] = useState(startArray);
1819
const [isLoading, setIsLoading] = useState(false);
1920
const client = useLDClient();
21+
const { toast } = useToast();
2022

2123
const handleInputChange = (e: any) => {
2224
setInput(e.target.value);
@@ -42,8 +44,15 @@ export default function Chatbot() {
4244

4345
const response = await fetch("/api/chat", {
4446
method: "POST",
45-
body: JSON.stringify(`${userInput}. Limit response to 100 characters.`),
47+
body: JSON.stringify(`As an AI bot for a travel airline,
48+
your purpose is to answer questions related to flights and traveling.
49+
Act as customer representative.
50+
Limit response to 100 characters.
51+
Only answer queries related to traveling and airlines.
52+
Remove quotation in response.
53+
Here is the user prompt: ${userInput}.`),
4654
});
55+
4756
const data = await response.json();
4857

4958
let aiAnswer;
@@ -56,12 +65,20 @@ export default function Chatbot() {
5665
aiAnswer = data?.completion; //claude
5766
}
5867

59-
const assistantMessage = {
68+
let assistantMessage = {
6069
role: "assistant",
6170
content: aiAnswer,
6271
id: uuidv4().slice(0, 4),
6372
};
64-
setMessages([...messages, userMessage, assistantMessage]);
73+
//TODO: remove loader if you get don't aiAnswer
74+
75+
if(aiAnswer === undefined){
76+
assistantMessage.content = "I'm sorry. Please try again."
77+
setMessages([...messages, userMessage, assistantMessage]);
78+
} else {
79+
setMessages([...messages, userMessage, assistantMessage]);
80+
}
81+
6582

6683
setIsLoading(false);
6784
}
@@ -70,6 +87,13 @@ export default function Chatbot() {
7087
console.log(messages);
7188
}, [messages]);
7289

90+
const surveyResponseNotification = () => {
91+
toast({
92+
title: `Thank you for your response!`,
93+
wrapperStyle: "bg-green-600 text-white font-sohne text-base border-none",
94+
});
95+
};
96+
7397
return (
7498
<>
7599
<div className="fixed bottom-4 right-4 z-50">
@@ -104,7 +128,10 @@ export default function Chatbot() {
104128
size="icon"
105129
title="How was our service today?"
106130
className="rounded-full bg-[#55efc4] text-gray-900 hover:bg-[#00b894] dark:bg-[#55efc4] dark:text-gray-900 dark:hover:bg-[#00b894]"
107-
onClick={() => client?.track("ai-chatbot-good-service", client.getContext())}
131+
onClick={() => {
132+
surveyResponseNotification();
133+
client?.track("ai-chatbot-good-service", client.getContext());
134+
}}
108135
>
109136
<SmileIcon className="h-6 w-6" />
110137
<span className="sr-only">Good</span>
@@ -114,7 +141,10 @@ export default function Chatbot() {
114141
size="icon"
115142
title="How was our service today?"
116143
className="rounded-full bg-[#ffeaa7] text-gray-900 hover:bg-[#fdcb6e] dark:bg-[#ffeaa7] dark:text-gray-900 dark:hover:bg-[#fdcb6e]"
117-
onClick={() => client?.track("ai-chatbot-neutral-service", client.getContext())}
144+
onClick={() => {
145+
surveyResponseNotification();
146+
client?.track("ai-chatbot-neutral-service", client.getContext());
147+
}}
118148
>
119149
<MehIcon className="h-6 w-6" />
120150
<span className="sr-only">Neutral</span>
@@ -124,7 +154,10 @@ export default function Chatbot() {
124154
size="icon"
125155
title="How was our service today?"
126156
className="rounded-full bg-[#ff7675] text-gray-50 hover:bg-[#d63031] dark:bg-[#ff7675] dark:text-gray-50 dark:hover:bg-[#d63031]"
127-
onClick={() => client?.track("ai-chatbot-bad-service", client.getContext())}
157+
onClick={() => {
158+
surveyResponseNotification();
159+
client?.track("ai-chatbot-bad-service", client.getContext());
160+
}}
128161
>
129162
<FrownIcon className="h-6 w-6" />
130163
<span className="sr-only">Bad</span>
@@ -163,7 +196,7 @@ export default function Chatbot() {
163196
key={m?.id}
164197
className="flex w-max max-w-[75%] flex-col gap-2 rounded-lg px-3 py-2 text-sm bg-gray-100 dark:bg-gray-800"
165198
>
166-
<BeatLoader className="" />
199+
<PulseLoader className="" />
167200
</div>
168201
);
169202
}

utils/contexts/login.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { createContext, useState } from "react";
44
import { v4 as uuidv4 } from "uuid";
55
import CryptoJS from 'crypto-js';
66
import { isAndroid, isIOS, isBrowser, isMobile, isMacOs, isWindows } from 'react-device-detect';
7-
7+
import { setCookie } from "cookies-next";
88

99
const LoginContext = createContext();
1010

0 commit comments

Comments
 (0)