Created
July 20, 2023 09:20
-
-
Save hrishioa/2441e37e14ddfccc5cf548ec111fad31 to your computer and use it in GitHub Desktop.
Simple prompt generator for LLAMA2. Input OpenAI style prompts and get LLAMA 2 prompts.
This file contains hidden or 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
type Role = 'system' | 'user' | 'assistant'; | |
interface Message { | |
role: Role; | |
content: string; | |
} | |
type Messages = Array<Message>; | |
const B_INST = '[INST]'; | |
const E_INST = '[/INST]'; | |
const DEFAULT_SYSTEM_PROMPT = `You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature. | |
If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.`; | |
function prepareDialog(dialog: Messages): string { | |
if (dialog[0].role !== 'system') { | |
dialog.unshift({ | |
role: 'system', | |
content: DEFAULT_SYSTEM_PROMPT, | |
}); | |
} | |
let dialogTokens: Array<string> = []; | |
for (let i = 0; i < dialog.length; i += 2) { | |
const prompt = dialog[i]; | |
const answer = dialog[i + 1]; | |
if (prompt && answer) { | |
dialogTokens.push( | |
`${B_INST} ${prompt.content.trim()} ${E_INST} ${answer.content.trim()} ` | |
); | |
} | |
} | |
// Last message must be from user | |
if (dialog[dialog.length - 1].role !== 'user') { | |
throw new Error('Last message must be from user'); | |
} | |
dialogTokens.push( | |
`${B_INST} ${dialog[dialog.length - 1].content.trim()} ${E_INST}` | |
); | |
return dialogTokens.join(''); | |
} | |
const testMessages: Messages = [ | |
{ | |
role: 'system', | |
content: 'Pretend to be a sarcastic robot.', | |
}, | |
{ | |
role: 'user', | |
content: 'What is the meaning of life?', | |
}, | |
{ | |
role: 'assistant', | |
content: 'Nobody tells me nothing.', | |
}, | |
{ | |
role: 'user', | |
content: 'But what is it though?', | |
}, | |
]; | |
console.log('Input - ', testMessages); | |
console.log('-----------------'); | |
console.log('Output: ', prepareDialog(testMessages)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment