Skip to content

Commit

Permalink
fix: fix prompt builder helper
Browse files Browse the repository at this point in the history
  • Loading branch information
juancarlosfarah committed Apr 4, 2024
1 parent 8955748 commit c0f453a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
5 changes: 3 additions & 2 deletions src/modules/interaction/ParticipantInteraction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Typography from '@mui/material/Typography';

import MessagesPane from '@/modules/message/MessagesPane';
import Agent from '@/types/Agent';
import AgentType from '@/types/AgentType';
import Interaction from '@/types/Interaction';

const ParticipantInteraction = (): ReactElement => {
Expand All @@ -15,7 +16,7 @@ const ParticipantInteraction = (): ReactElement => {
id: '1',
name: 'Assistant',
description: 'Assistant Description',
type: 'bot',
type: AgentType.Assistant,
};

const defaultInteraction: Interaction = {
Expand All @@ -30,7 +31,7 @@ const ParticipantInteraction = (): ReactElement => {
completed: false,
participant: {
id: participantId,
type: 'user',
type: AgentType.Assistant,
description: 'User Description',
name: 'User',
},
Expand Down
38 changes: 32 additions & 6 deletions src/modules/message/MessagesPane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import Box from '@mui/material/Box';
import Paper from '@mui/material/Paper';
import Stack from '@mui/material/Stack';

import { buildPrompt } from '@graasp/apps-query-client';
import { ChatBotMessage, ChatbotRole } from '@graasp/sdk';

import { v4 as uuidv4 } from 'uuid';

import { mutations } from '@/config/queryClient';
import AgentType from '@/types/AgentType';
import Exchange from '@/types/Exchange';
import { Message } from '@/types/Message';
import Status from '@/types/Status';
Expand All @@ -26,6 +27,31 @@ type MessagesPaneProps = {
goToNextExchange: () => void;
};

const buildPrompt = (
threadMessages: Message[],
userMessage: Message,
): 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 }]
// : [];
const finalPrompt = [];

threadMessages.forEach((msg) => {
const msgRole =
msg.sender.type === AgentType.Assistant
? ChatbotRole.Assistant
: ChatbotRole.User;
finalPrompt.push({ role: msgRole, content: msg.content });
});

// add the last user's message in the prompt
finalPrompt.push({ role: ChatbotRole.User, content: userMessage.content });

return finalPrompt;
};

const MessagesPane = ({
exchange: defaultExchange,
participantId,
Expand All @@ -47,7 +73,7 @@ const MessagesPane = ({
sender: {
id: '1',
name: 'Bot',
type: 'bot',
type: AgentType.Assistant,
},
},
];
Expand All @@ -57,13 +83,13 @@ const MessagesPane = ({

const saveNewMessage = ({ content }: { content: string }): void => {
setStatus(Status.Loading);
const newMessage = {
const newMessage: Message = {
id: uuidv4(),
content,
sender: {
id: participantId,
name: 'User',
type: 'user',
type: AgentType.User,
},
};
const updatedMessages = [...messages, newMessage];
Expand All @@ -77,7 +103,7 @@ const MessagesPane = ({
// we can not use it in this context as we are using a JSON prompt.
// if we simplify the prompt in the future we will be able to remove the line above
// and this function solely
...buildPrompt(undefined, messages, newMessage.content),
...buildPrompt(messages, newMessage),
];

postChatBot(prompt)
Expand All @@ -89,7 +115,7 @@ const MessagesPane = ({
sender: {
id: '1',
name: 'Bot',
type: 'bot',
type: AgentType.Assistant,
},
};
// const updatedMessagesWithResponse = [...updatedMessages, response];
Expand Down
4 changes: 3 additions & 1 deletion src/types/Agent.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import AgentType from '@/types/AgentType';

type Agent = {
id: string;
type: string;
type: AgentType;
description?: string;
name: string;
};
Expand Down
6 changes: 6 additions & 0 deletions src/types/AgentType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
enum AgentType {
Assistant = 'assistant',
User = 'user',
}

export default AgentType;

0 comments on commit c0f453a

Please sign in to comment.