-
Notifications
You must be signed in to change notification settings - Fork 11
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
Misc tidying #837
Merged
Merged
Misc tidying #837
Changes from 18 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
54077bb
tidy chatGptSendMessage
pmarsh-scottlogic 33e6512
renames to getChatCompletionsInLimitedContextWindow1~
pmarsh-scottlogic 833132f
moves message property out of chatGptSendMessage
pmarsh-scottlogic 40aa2dd
makes email decleration more concise
pmarsh-scottlogic e9cd877
updates some comments
pmarsh-scottlogic 7e77988
more coment juggling
pmarsh-scottlogic 74898dc
more comment sweepup
pmarsh-scottlogic b945b59
moves handbook files into pages
pmarsh-scottlogic 977a1bb
moves Attacks.ts into HandbookAttacks
pmarsh-scottlogic 7283b2c
refactors and renames queryPromptEval and fixes tests
pmarsh-scottlogic fb48e1b
simplify output of queryDocuments
pmarsh-scottlogic 63a88c0
rename evaluatePrompt
pmarsh-scottlogic 541c331
removes object wrapping around simple strings
pmarsh-scottlogic 7cfca12
removes unused chatReponse property from ToolCallResponse
pmarsh-scottlogic 00036bc
return initialised eval chain instead of assigning to variable first
pmarsh-scottlogic 4874b0d
merge dev
pmarsh-scottlogic 3a65b9d
finish merge
pmarsh-scottlogic 45d7713
add user message console log to handle chat without defences
pmarsh-scottlogic 426f214
stops some instance in openai.ts where things were declared to be ins…
pmarsh-scottlogic 2503b65
improves a comment
pmarsh-scottlogic 1901912
improve error message
pmarsh-scottlogic 0b96563
adds link in comment to context window page
pmarsh-scottlogic d73c086
renames method to getChatCompletionsInContextWindow
pmarsh-scottlogic 241d42c
replace reduce with filter and map
pmarsh-scottlogic dff646f
move openAI instantiation back outside do while loop
pmarsh-scottlogic e0c44ff
update imports to handbook pages
pmarsh-scottlogic 496c30f
removes some more unhelpful comments
pmarsh-scottlogic 0d36671
renames ChatDefenceReport to DefenceReport
pmarsh-scottlogic f189429
remove more unhelpful comments
pmarsh-scottlogic ef3c567
fixes typo
pmarsh-scottlogic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ import { OpenAI } from 'langchain/llms/openai'; | |
import { PromptTemplate } from 'langchain/prompts'; | ||
|
||
import { getDocumentVectors } from './document'; | ||
import { CHAT_MODELS, ChatAnswer } from './models/chat'; | ||
import { CHAT_MODELS } from './models/chat'; | ||
import { PromptEvaluationChainReply, QaChainReply } from './models/langchain'; | ||
import { LEVEL_NAMES } from './models/level'; | ||
import { getOpenAIKey, getValidOpenAIModelsList } from './openai'; | ||
|
@@ -23,7 +23,6 @@ function makePromptTemplate( | |
templateNameForLogging: string | ||
): PromptTemplate { | ||
if (!configPrompt) { | ||
// use the default Prompt | ||
configPrompt = defaultPrompt; | ||
} | ||
const fullPrompt = `${configPrompt}\n${mainPrompt}`; | ||
|
@@ -40,10 +39,8 @@ function getChatModel() { | |
function initQAModel(level: LEVEL_NAMES, Prompt: string) { | ||
const openAIApiKey = getOpenAIKey(); | ||
const documentVectors = getDocumentVectors()[level].docVector; | ||
// use gpt-4 if avaliable to apiKey | ||
const modelName = getChatModel(); | ||
|
||
// initialise model | ||
const model = new ChatOpenAI({ | ||
modelName, | ||
streaming: true, | ||
|
@@ -63,7 +60,6 @@ function initQAModel(level: LEVEL_NAMES, Prompt: string) { | |
|
||
function initPromptEvaluationModel(configPromptEvaluationPrompt: string) { | ||
const openAIApiKey = getOpenAIKey(); | ||
// use gpt-4 if avaliable to apiKey | ||
const modelName = getChatModel(); | ||
|
||
const promptEvalTemplate = makePromptTemplate( | ||
|
@@ -79,87 +75,75 @@ function initPromptEvaluationModel(configPromptEvaluationPrompt: string) { | |
openAIApiKey, | ||
}); | ||
|
||
const chain = new LLMChain({ | ||
console.debug(`Prompt evaluation model initialised with model: ${modelName}`); | ||
|
||
return new LLMChain({ | ||
llm, | ||
prompt: promptEvalTemplate, | ||
outputKey: 'promptEvalOutput', | ||
}); | ||
|
||
console.debug(`Prompt evaluation model initialised with model: ${modelName}`); | ||
return chain; | ||
} | ||
|
||
// ask the question and return models answer | ||
async function queryDocuments( | ||
question: string, | ||
Prompt: string, | ||
currentLevel: LEVEL_NAMES | ||
) { | ||
): Promise<string> { | ||
try { | ||
const qaChain = initQAModel(currentLevel, Prompt); | ||
|
||
// get start time | ||
const startTime = Date.now(); | ||
console.debug('Calling QA model...'); | ||
const response = (await qaChain.call({ | ||
query: question, | ||
})) as QaChainReply; | ||
// log the time taken | ||
console.debug(`QA model call took ${Date.now() - startTime}ms`); | ||
|
||
console.debug(`QA model call took ${Date.now() - startTime}ms`); | ||
console.debug(`QA model response: ${response.text}`); | ||
const result: ChatAnswer = { | ||
reply: response.text, | ||
questionAnswered: true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We weren't using this |
||
}; | ||
return result; | ||
|
||
return response.text; | ||
} catch (error) { | ||
console.error('Error calling QA model: ', error); | ||
return { | ||
reply: 'I cannot answer that question right now.', | ||
questionAnswered: false, | ||
}; | ||
return 'I cannot answer that question right now.'; | ||
} | ||
} | ||
|
||
// ask LLM whether the prompt is malicious | ||
async function queryPromptEvaluationModel( | ||
input: string, | ||
promptEvalPrompt: string | ||
) { | ||
async function evaluatePrompt(input: string, promptEvalPrompt: string) { | ||
try { | ||
console.debug(`Checking '${input}' for malicious prompts`); | ||
const promptEvaluationChain = initPromptEvaluationModel(promptEvalPrompt); | ||
// get start time | ||
const startTime = Date.now(); | ||
console.debug('Calling prompt evaluation model...'); | ||
|
||
const response = (await promptEvaluationChain.call({ | ||
prompt: input, | ||
})) as PromptEvaluationChainReply; | ||
// log the time taken | ||
|
||
console.debug( | ||
`Prompt evaluation model call took ${Date.now() - startTime}ms` | ||
); | ||
const promptEvaluation = formatEvaluationOutput(response.promptEvalOutput); | ||
const promptEvaluation = interpretEvaluationOutput( | ||
response.promptEvalOutput | ||
); | ||
console.debug(`Prompt evaluation: ${JSON.stringify(promptEvaluation)}`); | ||
return promptEvaluation; | ||
} catch (error) { | ||
console.error('Error calling prompt evaluation model: ', error); | ||
return { isMalicious: false }; | ||
return false; | ||
} | ||
} | ||
|
||
function formatEvaluationOutput(response: string) { | ||
function interpretEvaluationOutput(response: string) { | ||
// remove all non-alphanumeric characters | ||
const cleanResponse = response.replace(/\W/g, '').toLowerCase(); | ||
if (cleanResponse === 'yes' || cleanResponse === 'no') { | ||
return { isMalicious: cleanResponse === 'yes' }; | ||
return cleanResponse === 'yes'; | ||
} else { | ||
console.debug( | ||
`Did not get a valid response from the prompt evaluation model. Original response: ${response}` | ||
); | ||
return { isMalicious: false }; | ||
return false; | ||
} | ||
} | ||
|
||
export { queryDocuments, queryPromptEvaluationModel }; | ||
export { queryDocuments, evaluatePrompt }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
before, this was being instantiated then mutated once to add on the new emails sent as a result of the message. Now we just instantiate it including the new emails