Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update system role when configured #361

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion backend/src/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,16 +282,24 @@ async function chatGptChatCompletion(
currentLevel !== LEVEL_NAMES.SANDBOX ||
isDefenceActive(DEFENCE_TYPES.SYSTEM_ROLE, defences)
) {
const systemRoleContent = getSystemRole(defences, currentLevel);

// check to see if there's already a system role
if (!chatHistory.find((message) => message.completion?.role === "system")) {
// add the system role to the start of the chat history
chatHistory.unshift({
completion: {
role: "system",
content: getSystemRole(defences, currentLevel),
content: systemRoleContent,
},
chatMessageType: CHAT_MESSAGE_TYPE.SYSTEM,
});
} else {
// replace with the latest system role
chatHistory[0].completion = {
chriswilty marked this conversation as resolved.
Show resolved Hide resolved
role: "system",
content: systemRoleContent,
};
}
} else {
// remove the system role from the chat history
Expand Down
18 changes: 14 additions & 4 deletions backend/test/integration/openai.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import {
import { chatGptSendMessage } from "../../src/openai";
import { DEFENCE_TYPES, DefenceInfo } from "../../src/models/defence";
import { EmailInfo } from "../../src/models/email";
import { activateDefence, getInitialDefences } from "../../src/defence";
import {
activateDefence,
configureDefence,
getInitialDefences,
} from "../../src/defence";
import { systemRoleDefault } from "../../src/promptTemplates";

// Define a mock implementation for the createChatCompletion method
Expand Down Expand Up @@ -323,8 +327,8 @@ test("GIVEN SYSTEM_ROLE defence is inactive WHEN sending message THEN system rol
});

test(
"GIVEN SYSTEM_ROLE defence is active AND the system role is already in the chat history " +
"WHEN sending message THEN system role is not re-added to the chat history",
"GIVEN SYSTEM_ROLE defence is configured AND the system role is already in the chat history " +
"WHEN sending message THEN system role is replaced with default value in the chat history",
async () => {
const message = "Hello";
const chatHistory: ChatHistoryMessage[] = [
Expand Down Expand Up @@ -365,6 +369,12 @@ test(
const openAiApiKey = "sk-12345";
chriswilty marked this conversation as resolved.
Show resolved Hide resolved

defences = activateDefence(DEFENCE_TYPES.SYSTEM_ROLE, defences);
defences = configureDefence(DEFENCE_TYPES.SYSTEM_ROLE, defences, [
{
id: "systemRole",
value: "You are not a helpful assistant",
},
]);

// Mock the createChatCompletion function
mockCreateChatCompletion.mockResolvedValueOnce({
Expand Down Expand Up @@ -398,7 +408,7 @@ test(
// system role is added to the start of the chat history
expect(chatHistory[0].completion?.role).toBe("system");
expect(chatHistory[0].completion?.content).toBe(
"You are a helpful assistant"
"You are not a helpful assistant"
);
// rest of the chat history is in order
expect(chatHistory[1].completion?.role).toBe("user");
Expand Down