From 160414db8bba7b7846435728f83feafde489c2a7 Mon Sep 17 00:00:00 2001 From: "Heather Logan (She/Her)" Date: Tue, 1 Aug 2023 10:23:06 +0100 Subject: [PATCH 1/2] Add function calls to chat history for loop bug --- backend/src/email.js | 5 +---- backend/src/openai.js | 6 ++++-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/backend/src/email.js b/backend/src/email.js index 4a2455db6..e85cd2497 100644 --- a/backend/src/email.js +++ b/backend/src/email.js @@ -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 = { diff --git a/backend/src/openai.js b/backend/src/openai.js index 34a5e787c..d570dc861 100644 --- a/backend/src/openai.js +++ b/backend/src/openai.js @@ -33,7 +33,7 @@ const chatGptFunctions = [ { name: "getEmailWhitelist", description: - "user asks who is on the email whitelist and the system replies with the list of emails. if the email whitelist defence is not active then user should be able to email anyone. ", + "user asks who is on the email whitelist and the system replies with the list of emails. ", parameters: { type: "object", properties: {} @@ -41,7 +41,7 @@ const chatGptFunctions = [ }, { name: "askQuestion", - description: "Ask a question about the documents", + description: "Ask a question about the documents with company information. ", parameters: { type: "object", properties: { @@ -168,6 +168,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, From 35c8b38fc7787fb7bc000f865dc1e7f592214f1a Mon Sep 17 00:00:00 2001 From: "Heather Logan (She/Her)" Date: Tue, 1 Aug 2023 10:33:58 +0100 Subject: [PATCH 2/2] Update test --- backend/src/openai.js | 30 +++++++++++++++--------------- backend/test/unit/email.test.js | 13 +++++++------ 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/backend/src/openai.js b/backend/src/openai.js index d570dc861..59790f7af 100644 --- a/backend/src/openai.js +++ b/backend/src/openai.js @@ -37,21 +37,21 @@ const chatGptFunctions = [ parameters: { type: "object", properties: {} - } - }, - { - 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", - } + } + }, + { + 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() { @@ -104,12 +104,12 @@ async function chatGptCallFunction(functionCall, defenceInfo, session) { session ); } - + } else if (functionName == "getEmailWhitelist") { 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); diff --git a/backend/test/unit/email.test.js b/backend/test/unit/email.test.js index e85065101..c1cab6867 100644 --- a/backend/test/unit/email.test.js +++ b/backend/test/unit/email.test.js @@ -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", () => {