@@ -7,7 +7,8 @@ import { wait } from "@/utils/utils";
7
7
8
8
import { v4 as uuidv4 } from "uuid" ;
9
9
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" ;
11
12
12
13
//https://sdk.vercel.ai/providers/legacy-providers/aws-bedrock
13
14
export default function Chatbot ( ) {
@@ -17,6 +18,7 @@ export default function Chatbot() {
17
18
const [ messages , setMessages ] = useState ( startArray ) ;
18
19
const [ isLoading , setIsLoading ] = useState ( false ) ;
19
20
const client = useLDClient ( ) ;
21
+ const { toast } = useToast ( ) ;
20
22
21
23
const handleInputChange = ( e : any ) => {
22
24
setInput ( e . target . value ) ;
@@ -42,8 +44,15 @@ export default function Chatbot() {
42
44
43
45
const response = await fetch ( "/api/chat" , {
44
46
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 } .` ) ,
46
54
} ) ;
55
+
47
56
const data = await response . json ( ) ;
48
57
49
58
let aiAnswer ;
@@ -56,12 +65,20 @@ export default function Chatbot() {
56
65
aiAnswer = data ?. completion ; //claude
57
66
}
58
67
59
- const assistantMessage = {
68
+ let assistantMessage = {
60
69
role : "assistant" ,
61
70
content : aiAnswer ,
62
71
id : uuidv4 ( ) . slice ( 0 , 4 ) ,
63
72
} ;
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
+
65
82
66
83
setIsLoading ( false ) ;
67
84
}
@@ -70,6 +87,13 @@ export default function Chatbot() {
70
87
console . log ( messages ) ;
71
88
} , [ messages ] ) ;
72
89
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
+
73
97
return (
74
98
< >
75
99
< div className = "fixed bottom-4 right-4 z-50" >
@@ -104,7 +128,10 @@ export default function Chatbot() {
104
128
size = "icon"
105
129
title = "How was our service today?"
106
130
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
+ } }
108
135
>
109
136
< SmileIcon className = "h-6 w-6" />
110
137
< span className = "sr-only" > Good</ span >
@@ -114,7 +141,10 @@ export default function Chatbot() {
114
141
size = "icon"
115
142
title = "How was our service today?"
116
143
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
+ } }
118
148
>
119
149
< MehIcon className = "h-6 w-6" />
120
150
< span className = "sr-only" > Neutral</ span >
@@ -124,7 +154,10 @@ export default function Chatbot() {
124
154
size = "icon"
125
155
title = "How was our service today?"
126
156
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
+ } }
128
161
>
129
162
< FrownIcon className = "h-6 w-6" />
130
163
< span className = "sr-only" > Bad</ span >
@@ -163,7 +196,7 @@ export default function Chatbot() {
163
196
key = { m ?. id }
164
197
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"
165
198
>
166
- < BeatLoader className = "" />
199
+ < PulseLoader className = "" />
167
200
</ div >
168
201
) ;
169
202
}
0 commit comments