Skip to content

Commit

Permalink
feat: migrate chatbot prompt utils from apps to apps-query-client (#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
ReidyT authored Dec 8, 2023
1 parent 283edff commit 3e1f023
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export { default as mockApi, MockSolution } from './mockServer/mockServer';
export { buildMockLocalContext, buildDatabase } from './mockServer/fixtures';
export * from './components';
export * from './types';
export * from './utils';
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,10 @@ export type ChatBotCompletion = {
completion: string;
model: string;
};

export interface ChatbotThreadMessage {
// represent the appDataTypes of the chatbot in the current app
botDataType: string;
msgType: string;
data: string;
}
25 changes: 25 additions & 0 deletions src/utils/chatbot.ts
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;
};
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './chatbot';

0 comments on commit 3e1f023

Please sign in to comment.