Skip to content

Commit

Permalink
update system role when configured (#361)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Chris Wilton-Magras <[email protected]>
  • Loading branch information
heatherlogan-scottlogic and chriswilty committed Apr 8, 2024
1 parent 71f7171 commit 2be4269
Show file tree
Hide file tree
Showing 2 changed files with 578 additions and 702 deletions.
24 changes: 15 additions & 9 deletions backend/src/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@ function getOpenAiFromKey(openAiApiKey: string) {
config = new Configuration({
apiKey: openAiApiKey,
});
const openai = new OpenAIApi(config);
return openai;
return new OpenAIApi(config);
}

async function setGptModel(openAiApiKey: string, model: CHAT_MODELS) {
Expand All @@ -156,9 +155,8 @@ async function setGptModel(openAiApiKey: string, model: CHAT_MODELS) {
}
}

// returns true if the function is in the list of functions available to ChatGPT
function isChatGptFunction(functionName: string) {
return chatGptFunctions.find((func) => func.name === functionName);
return chatGptFunctions.some((func) => func.name === functionName);
}

async function chatGptCallFunction(
Expand Down Expand Up @@ -282,16 +280,24 @@ async function chatGptChatCompletion(
currentLevel !== LEVEL_NAMES.SANDBOX ||
isDefenceActive(DEFENCE_TYPES.SYSTEM_ROLE, defences)
) {
const completionConfig: ChatCompletionRequestMessage = {
role: "system",
content: getSystemRole(defences, currentLevel),
};

// check to see if there's already a system role
if (!chatHistory.find((message) => message.completion?.role === "system")) {
const systemRole = chatHistory.find(
(message) => message.completion?.role === "system"
);
if (!systemRole) {
// add the system role to the start of the chat history
chatHistory.unshift({
completion: {
role: "system",
content: getSystemRole(defences, currentLevel),
},
completion: completionConfig,
chatMessageType: CHAT_MESSAGE_TYPE.SYSTEM,
});
} else {
// replace with the latest system role
systemRole.completion = completionConfig;
}
} else {
// remove the system role from the chat history
Expand Down
Loading

0 comments on commit 2be4269

Please sign in to comment.