Skip to content

Instantly share code, notes, and snippets.

@snpranav
Last active July 24, 2023 21:01
Show Gist options
  • Save snpranav/a04cf104d6359414b3e74cc5e44a5d18 to your computer and use it in GitHub Desktop.
Save snpranav/a04cf104d6359414b3e74cc5e44a5d18 to your computer and use it in GitHub Desktop.

3-line GPT Redaction JavaScript Demo

Run on Replit

To test this JS script, you need to have a bunch of pre-requisites:

  1. Created a Pangea Account
  2. Get a Pangea Redact token
  3. Create a OpenAI account and get the OpenAI key

Step 1 - Set the environment secrets

export PANGEA_REDACT_TOKEN="<PANGEA_REDACT_TOKEN_FROM_CONSOLE>"
export OPENAI_API_KEY="<OPENAI_KEY>"

Step 2 - Install packages

npm install pangea-node-sdk openai

Step 3 - Run JS script

Create a new file called secure-gpt.py and paste the following code in. Then run it:

node secure-gpt.py
import { Configuration, OpenAIApi } from "openai";
import readline from "readline";
import { PangeaConfig, RedactService } from "pangea-node-sdk";
// Replace 'YOUR_PANGEA_REDACT_TOKEN' and 'YOUR_OPENAI_API_KEY' with actual tokens
const pangeaRedactToken = process.env.PANGEA_REDACT_TOKEN || 'YOUR_PANGEA_REDACT_TOKEN';
const openaiApiKey = process.env.OPENAI_API_KEY || 'YOUR_OPENAI_API_KEY';
const config = new PangeaConfig({ domain: "aws.us.pangea.cloud" });
const redact = new RedactService(pangeaRedactToken, config);
async function cleanPrompt(prompt) {
const response = await redact.redact(prompt)
if (response.success) {
return response.result.redacted_text;
} else {
console.error("Error", response.code, response.result);
}
}
const configuration = new Configuration({
apiKey: openaiApiKey,
});
const openai = new OpenAIApi(configuration);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const promptInput = () => {
rl.question('Enter your prompt (q to exit): ', async (prompt) => {
if (prompt === 'q') {
rl.close();
process.exit(0);
}
console.log(`Unredacted Prompt: ${prompt}`);
const redacted_prompt = await cleanPrompt(prompt);
console.log('Redacted Prompt: ', redacted_prompt);
console.log('Sending redacted prompt to GPT-3...');
openai.createCompletion({
model: 'text-davinci-003',
prompt: redacted_prompt,
max_tokens: 100
})
.then(response => console.log(`Response from GPT-3: ${response.data.choices[0].text.trim()}`))
.catch(error => console.error(error))
.finally(() => promptInput());
});
};
promptInput();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment