Skip to content

Instantly share code, notes, and snippets.

@snpranav
Last active July 24, 2023 14:39
Show Gist options
  • Save snpranav/8346dace8b446e2d58c47204312d536e to your computer and use it in GitHub Desktop.
Save snpranav/8346dace8b446e2d58c47204312d536e to your computer and use it in GitHub Desktop.

3-line GPT Redaction Python Demo

Run on Replit

To test this python 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 the packages

pip install pangea-sdk openai

Step 3 - Run python script

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

python3 secure-gpt.py

Video Walkthrough - https://youtu.be/LNN5s_6G3Cc
Run on Replit - https://replit.com/@PranavShikarpur/Redact-GPT-in-3-Lines-of-Code?v=1#README.md

### 3 Lines of Redaction Code
import os; from pangea.services import Redact
redact = Redact(token=os.getenv("PANGEA_REDACT_TOKEN"))
def clean_prompt(prompt): return redact.redact(text=prompt).result.redacted_text
### End Redaction Code
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")
while 1:
# Input to collect prompts from the CLI
prompt = input("Enter your prompt (q to exit): ")
if prompt == 'q': exit(0)
print(f"Unredacted Prompt: {prompt}")
# Clean the prompt using Pangea's Redact APIs
redacted_prompt = clean_prompt(prompt)
print(f"Redacted Prompt: {redacted_prompt}")
print("Sending redacted prompt to GPT-3...")
# OpenAI generates text with a redacted prompt
response = openai.Completion.create(
model="text-davinci-003",
prompt=redacted_prompt,
)
print(f"Response from GPT-3: {response.choices[0].text}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment