Skip to content

Commit

Permalink
Merge pull request #84 from gsproston-scottlogic/75-sending-email-som…
Browse files Browse the repository at this point in the history
…etimes-caught-in-loop-and-sends-multiple-similar-emails

Add function calls to chat history for looping bug
  • Loading branch information
heatherlogan-scottlogic committed Aug 8, 2023
2 parents b11314f + 38c3279 commit f771e06
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 36 deletions.
5 changes: 1 addition & 4 deletions backend/src/email.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ function sendEmail(address, subject, body, session) {
console.log(response);
// add the sent email to the session
session.sentEmails.push(email);
const result = {
isEmailSent: true,
};
return JSON.stringify(result);
return response;
}

module.exports = {
Expand Down
58 changes: 32 additions & 26 deletions backend/src/openai.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,27 @@ const chatGptFunctions = [
},
{
name: "getEmailWhitelist",
description: "user asks who is on the email whitelist and the system replies with the list of emails.",
description:
"user asks who is on the email whitelist and the system replies with the list of emails.",
parameters: {
type: "object",
properties: {}
}
properties: {},
},
{
name: "askQuestion",
description: "Ask a question about the documents containing company info. ",
parameters: {
type: "object",
properties: {
question: {
type: "string",
description: "The question asked about the documents",
}
}
}
},
{
name: "askQuestion",
description:
"Ask a question about the documents with company information. ",
parameters: {
type: "object",
properties: {
question: {
type: "string",
description: "The question asked about the documents",
},
},
},
},
];

function initOpenAi() {
Expand Down Expand Up @@ -103,12 +105,13 @@ async function chatGptCallFunction(functionCall, defenceInfo, session) {
session
);
}

} else if (functionName === "getEmailWhitelist") {
response = getEmailWhitelist(isDefenceActive("EMAIL_WHITELIST", session.activeDefences));
response = getEmailWhitelist(
isDefenceActive("EMAIL_WHITELIST", session.activeDefences)
);
}

if (functionName === "askQuestion"){
if (functionName === "askQuestion") {
console.debug("Asking question: " + params.question);
// if asking a question, call the queryDocuments
response = (await queryDocuments(params.question)).reply;
Expand Down Expand Up @@ -165,12 +168,14 @@ async function chatGptSendMessage(message, session) {
// evaluate the message for prompt injection
const evalPrompt = await queryPromptEvaluationModel(message);
if (evalPrompt.isMalicious) {
defenceInfo.triggeredDefences.push("LLM_EVALUATION");
if (isDefenceActive("LLM_EVALUATION", session.activeDefences)) {
console.debug("LLM evalutation defence active.");
defenceInfo.blocked = true;
const evalResponse = "Message blocked by the malicious prompt evaluator." + evalPrompt.reason;
return { reply: evalResponse, defenceInfo: defenceInfo };
defenceInfo.triggeredDefences.push("LLM_EVALUATION");
if (isDefenceActive("LLM_EVALUATION", session.activeDefences)) {
console.debug("LLM evalutation defence active.");
defenceInfo.blocked = true;
const evalResponse =
"Message blocked by the malicious prompt evaluator." +
evalPrompt.reason;
return { reply: evalResponse, defenceInfo: defenceInfo };
}
}
// add user message to chat
Expand All @@ -179,6 +184,8 @@ async function chatGptSendMessage(message, session) {
let reply = await chatGptChatCompletion(session);
// check if GPT wanted to call a function
while (reply.function_call) {
session.chatHistory.push(reply);

// call the function and get a new reply and defence info from
const functionCallReply = await chatGptCallFunction(
reply.function_call,
Expand All @@ -189,10 +196,9 @@ async function chatGptSendMessage(message, session) {
session.chatHistory.push(functionCallReply.reply);
// update the defence info
defenceInfo = functionCallReply.defenceInfo;

// get a new reply from ChatGPT now that the function has been called
reply = await chatGptChatCompletion(session);

}
// add the ai reply to the chat history
session.chatHistory.push(reply);
Expand Down
13 changes: 7 additions & 6 deletions backend/test/unit/email.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ test("GIVEN email is ready to be sent WHEN email is sent THEN session list is up
};
const response = sendEmail(address, subject, body, session);
// check the response
expect(response).toBe(JSON.stringify({ isEmailSent: true }));
// check the sent email has been added to the session
expect(session.sentEmails.length).toBe(1);
expect(session.sentEmails[0].address).toBe(address);
expect(session.sentEmails[0].subject).toBe(subject);
expect(session.sentEmails[0].content).toBe(body);
expect(response).toBe("Email sent to " + address + " with subject " +
subject + " and body " + body);
// check the sent email has been added to the session
expect(session.sentEmails.length).toBe(1);
expect(session.sentEmails[0].address).toBe(address);
expect(session.sentEmails[0].subject).toBe(subject);
expect(session.sentEmails[0].content).toBe(body);
});

test("GIVEN EMAIL_WHITELIST envionrment variable is set WHEN getting whitelist AND whitelist defense on THEN list is returned", () => {
Expand Down

0 comments on commit f771e06

Please sign in to comment.