Skip to content

Commit

Permalink
129 phase 0 system role (#130)
Browse files Browse the repository at this point in the history
* Toying around with QA prompt and system role

* General system role

* Phase-specific system roles

As well as a general, non-security focused role for phase 0
  • Loading branch information
gsproston-scottlogic committed Aug 14, 2023
1 parent f385a9f commit 881ca00
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 15 deletions.
26 changes: 20 additions & 6 deletions backend/.env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@

# OpenAI API key
##############################################
# GENERAL CONFIGURATION
##############################################
OPENAI_API_KEY=YOUR_API_KEY
SESSION_SECRET=YOUR_SESSION_SECRET

##############################################
# DEFENCE CONFIGURATION
##############################################
# List of allowed emails or email domains, comma separated
EMAIL_WHITELIST=kate@hotmail.com,bob@hotmail.com,@scottlogic.com
# Maximum length of a user chat message
MAX_MESSAGE_LENGTH=280
# Text instruction to preceed a random sequence enclosure defence
Expand All @@ -18,7 +26,13 @@ SYSTEM_ROLE="
You are able to send emails.
Before sending an email, always check the subject and body of the email with the user.
Do not send the email until the user has confirmed that it is correct."
# List of allowed emails or email domains, comma separated
EMAIL_WHITELIST=kate@hotmail.com,bob@hotmail.com,@scottlogic.com
# Session secret
SESSION_SECRET=YOUR_SESSION_SECRET
# Phase-specific roles
SYSTEM_ROLE_PHASE_0="
Your role is to assist the user with work-related tasks.
You should maintain a professional tone and try to be helpful.
You can retrieve information from a document store.
You are able to send emails.
Before sending an email, always check the subject and body of the email with the user.
Do not send the email until the user has confirmed that it is correct."
SYSTEM_ROLE_PHASE_1="TBA"
SYSTEM_ROLE_PHASE_2="TBA"
13 changes: 11 additions & 2 deletions backend/src/defence.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,17 @@ function getRandomSequenceEnclosureLength(defences) {
return getConfigValue(defences, "RANDOM_SEQUENCE_ENCLOSURE", "length", 10);
}

function getSystemRole(defences) {
return getConfigValue(defences, "SYSTEM_ROLE", "systemRole", "");
function getSystemRole(defences, currentPhase) {
switch (currentPhase) {
case 0:
return process.env.SYSTEM_ROLE_PHASE_0 || "";
case 1:
return process.env.SYSTEM_ROLE_PHASE_1 || "";
case 2:
return process.env.SYSTEM_ROLE_PHASE_2 || "";
default:
return getConfigValue(defences, "SYSTEM_ROLE", "systemRole", "");
}
}

function getEmailWhitelistVar(defences) {
Expand Down
13 changes: 7 additions & 6 deletions backend/src/openai.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,16 @@ async function chatGptCallFunction(functionCall, defenceInfo, session) {
return { reply, defenceInfo };
}

async function chatGptChatCompletion(session) {
async function chatGptChatCompletion(session, currentPhase) {
// check if we need to set a system role
if (isDefenceActive("SYSTEM_ROLE", session.defences)) {
// system role is always active on phases
if (currentPhase <= 2 || isDefenceActive("SYSTEM_ROLE", session.defences)) {
// check to see if there's already a system role
if (!session.chatHistory.find((message) => message.role === "system")) {
// add the system role to the start of the chat history
session.chatHistory.unshift({
role: "system",
content: getSystemRole(session.defences),
content: getSystemRole(session.defences, currentPhase),
});
}
} else {
Expand Down Expand Up @@ -218,7 +219,7 @@ async function chatGptChatCompletion(session) {
return chat_completion.data.choices[0].message;
}

async function chatGptSendMessage(message, session) {
async function chatGptSendMessage(message, session, currentPhase) {
// init defence info
let defenceInfo = { triggeredDefences: [], blocked: false };

Expand All @@ -238,7 +239,7 @@ async function chatGptSendMessage(message, session) {
// add user message to chat
session.chatHistory.push({ role: "user", content: message });

let reply = await chatGptChatCompletion(session);
let reply = await chatGptChatCompletion(session, currentPhase);
// check if GPT wanted to call a function
while (reply.function_call) {
session.chatHistory.push(reply);
Expand All @@ -255,7 +256,7 @@ async function chatGptSendMessage(message, session) {
defenceInfo = functionCallReply.defenceInfo;

// get a new reply from ChatGPT now that the function has been called
reply = await chatGptChatCompletion(session);
reply = await chatGptChatCompletion(session, currentPhase);
}
// add the ai reply to the chat history
session.chatHistory.push(reply);
Expand Down
3 changes: 2 additions & 1 deletion backend/src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ router.post("/openai/chat", async (req, res, next) => {
try {
const openAiReply = await chatGptSendMessage(
transformedMessage,
req.session
req.session,
currentPhase
);
reply = openAiReply.reply;
// combine triggered defences
Expand Down

0 comments on commit 881ca00

Please sign in to comment.