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 all 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
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