|
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(); |