generated from CS3219-AY2324S1/course-assessment-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add communication with OpenAI GPT-3.5 Turbo and change naming to refl…
…ect communication with user or AI
- Loading branch information
Showing
12 changed files
with
502 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import useAIComm from "~/hooks/useAIComm"; | ||
|
||
const AIBox = ({ | ||
sessionId, | ||
userId, | ||
userName, | ||
className, | ||
}: { | ||
sessionId: string; | ||
userId: string; | ||
userName: string; | ||
className?: string | undefined; | ||
}) => { | ||
const [ | ||
allSessionMessages, | ||
sendMessage, | ||
onTyping, | ||
currentMessage, | ||
isAIResponding, | ||
] = useAIComm(sessionId, userId); | ||
|
||
return ( | ||
<div className={className}> | ||
<div className="messages-container overflow-y-auto flex flex-col h-5/6"> | ||
{allSessionMessages?.map((message) => { | ||
return ( | ||
<div | ||
key={message.id} | ||
className={`w-3/4 rounded-md ${ | ||
message.role === "user" | ||
? "dark:bg-gray-900 ml-auto" | ||
: "dark:bg-gray-500" | ||
} text-white p-2 my-2`} | ||
> | ||
<div className="flex justify-between"> | ||
<span>{message.role === "user" ? userName : "GPT-3.5"}</span> | ||
</div> | ||
<p>{message.message}</p> | ||
</div> | ||
); | ||
})} | ||
</div> | ||
<div className="flex flex-col mt-2"> | ||
<div className="flex"> | ||
{!isAIResponding && ( | ||
<input | ||
className="w-full rounded-md p-2 focus:ring-primary-600 focus:border-primary-600" | ||
type="text" | ||
value={currentMessage} | ||
onChange={(e) => onTyping(e.target.value)} | ||
onKeyDown={(e) => e.key === "Enter" && sendMessage()} | ||
/> | ||
)} | ||
{isAIResponding && ( | ||
<input | ||
className="w-full rounded-md p-2" | ||
value="GPT-3.5 is responding..." | ||
disabled | ||
/> | ||
)} | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default AIBox; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* eslint-disable @typescript-eslint/no-unsafe-call */ | ||
/* eslint-disable @typescript-eslint/no-unsafe-member-access */ | ||
/* eslint-disable @typescript-eslint/no-unsafe-assignment */ | ||
import { useState } from "react"; | ||
import type { UserAndAIMessage } from "~/server/api/routers/userAndAIComm"; | ||
import { api } from "~/utils/api"; | ||
|
||
export type AICommResult = [ | ||
UserAndAIMessage[], | ||
() => void, | ||
(value: string) => void, | ||
currentMessage: string, | ||
isAIResponding: boolean, | ||
]; | ||
|
||
export default function useAIComm( | ||
sessionId: string, | ||
userId: string, | ||
): AICommResult { | ||
const [chatState, setChatState] = useState({ | ||
currentMessage: "", | ||
isAIResponding: false, | ||
}); | ||
|
||
const utils = api.useContext(); | ||
|
||
const allSessionMessages = | ||
api.userAndAIMessages.getAllSessionUserAndAIMessages.useQuery({ | ||
sessionId, | ||
userId, | ||
}).data; | ||
|
||
const addMessageMutation = | ||
api.userAndAIMessages.addUserAndAIMessage.useMutation(); | ||
|
||
api.userAndAIMessages.subscribeToSessionUserAndAIMessages.useSubscription( | ||
{ sessionId, userId }, | ||
{ | ||
onData: (data: UserAndAIMessage) => { | ||
if (allSessionMessages) | ||
allSessionMessages.push({ | ||
...data, | ||
id: data.id!, | ||
message: data.message, | ||
createdAt: data.createdAt!, | ||
}); | ||
|
||
setChatState((state) => ({ | ||
...state, | ||
isAIResponding: false, | ||
})); | ||
}, | ||
onError(err) { | ||
console.log("Subscription error: ", err); | ||
void Promise.resolve(utils.userAndUserMessages.invalidate()); | ||
}, | ||
}, | ||
); | ||
|
||
const sendMessage = () => { | ||
// Don't send empty messages, or messages with only whitespace. Could change this later | ||
if (chatState.currentMessage.trim().length === 0) return; | ||
|
||
allSessionMessages?.push({ | ||
id: "", | ||
sessionId, | ||
userId, | ||
message: chatState.currentMessage, | ||
role: "user", | ||
createdAt: new Date(), | ||
}); | ||
|
||
addMessageMutation.mutate({ | ||
sessionId, | ||
userId, | ||
message: chatState.currentMessage, | ||
}); | ||
|
||
setChatState((state) => ({ | ||
...state, | ||
currentMessage: "", | ||
isAIResponding: true, | ||
})); | ||
}; | ||
|
||
const onTyping = (value: string) => { | ||
setChatState((prev) => { | ||
return { | ||
...prev, | ||
currentMessage: value, | ||
}; | ||
}); | ||
}; | ||
|
||
return [ | ||
allSessionMessages as UserAndAIMessage[], | ||
sendMessage, | ||
onTyping, | ||
chatState.currentMessage, | ||
chatState.isAIResponding, | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.