generated from graasp/graasp-repo
-
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.
feat: migrate chatbot prompt utils from apps to apps-query-client (#219)
- Loading branch information
Showing
4 changed files
with
34 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { ChatBotMessage, ChatbotRole } from '@graasp/sdk'; | ||
|
||
import { ChatbotThreadMessage } from 'src/types'; | ||
|
||
export const buildPrompt = ( | ||
initialPrompt: string | undefined, | ||
threadMessages: ChatbotThreadMessage[], | ||
userMessage: string, | ||
): Array<ChatBotMessage> => { | ||
// define the message to send to OpenAI with the initial prompt first if needed (role system). | ||
// Each call to OpenAI must contain the whole history of the messages. | ||
const finalPrompt: Array<ChatBotMessage> = initialPrompt | ||
? [{ role: ChatbotRole.System, content: initialPrompt }] | ||
: []; | ||
|
||
threadMessages.forEach((msg) => { | ||
const msgRole = msg.msgType === msg.botDataType ? ChatbotRole.Assistant : ChatbotRole.User; | ||
finalPrompt.push({ role: msgRole, content: msg.data }); | ||
}); | ||
|
||
// add the last user's message in the prompt | ||
finalPrompt.push({ role: ChatbotRole.User, content: userMessage }); | ||
|
||
return finalPrompt; | ||
}; |
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 @@ | ||
export * from './chatbot'; |