$git clone https://github.com/VITA-Group/DP-OPTDP-OPT: Make Large Language Model Your Privacy-Preserving Prompt Engineer ====================================================
| 1 | DP-OPT: Make Large Language Model Your Privacy-Preserving Prompt Engineer |
| 2 | ==================================================== |
| 3 | |
| 4 | [](https://opensource.org/licenses/MIT) |
| 5 | |
| 6 | Official PyTorch Code for Paper: "DP-OPT: Make Large Language Model Your Privacy-Preserving Prompt Engineer" [Junyuan Hong](https://jyhong.gitlab.io/), [Jiachen T. Wang](https://tianhaowang.netlify.app/), [Chenhui Zhang](https://scholar.google.com/citations?user=UYxdrBsAAAAJ&hl=en), [Zhangheng Li](https://scholar.google.com/citations?user=NZCLqZMAAAAJ), [Bo Li](https://aisecure.github.io/), [Zhangyang Wang](https://vita-group.github.io/), *ICLR (Spotlight, top-5%)* 2024. |
| 7 | |
| 8 | [paper](https://arxiv.org/abs/2312.03724) / [code](https://github.com/VITA-Group/DP-OPT) / [blog](https://jyhong.gitlab.io/publication/2023dp_opt/) |
| 9 | |
| 10 | **TL;DR**: We proposed the first end-to-end privacy-preserving automatic prompt engineering method. |
| 11 | |
| 12 | ## Overview |
| 13 | |
| 14 | |
| 15 |  |
| 16 | |
| 17 | Large Language Models (LLMs) have emerged as dominant tools for various tasks, particularly when tailored for a specific target by prompt tuning. Nevertheless, concerns surrounding data privacy present obstacles due to the tuned prompts' dependency on sensitive private information. A practical solution is to host a local LLM and optimize a soft prompt privately using data. Yet, hosting a local model becomes problematic when model ownership is protected. Alternative methods, like sending data to the model’s provider for training, intensify these privacy issues facing an untrusted provider. In this paper, we present a novel solution called Differentially-Private Offsite Prompt Tuning (DP-OPT) to address this challenge. Our approach involves tuning a discrete prompt on the client side and then applying it to the desired cloud models. We demonstrate that prompts suggested by LLMs themselves can be transferred without compromising performance significantly. To ensure that the prompts do not leak private information, we introduce the first private prompt generation mechanism, by a differentially-private (DP) ensemble of in-context learning with private demonstrations. With DP-OPT, generating privacy-preserving prompts by Vicuna-7b can yield competitive performance compared to non-private in-context learning on GPT3.5 or local private prompt tuning. |
| 18 | |
| 19 | ## Get Started |
| 20 | |
| 21 | Prepare conda env. |
| 22 | ```shell |
| 23 | conda create --name dp-opt python=3.8 -y |
| 24 | conda activate dp-opt |
| 25 | pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118 |
| 26 | pip install transformers datasets accelerate sentencepiece scikit-learn wandb autodp |
| 27 | # transformers==4.28.1 |
| 28 | ``` |
| 29 | |
| 30 | Prepare DLN datasets |
| 31 | ```shell |
| 32 | bash setup_data.sh |
| 33 | ``` |
| 34 | |
| 35 | To use openai models, create `openai_config.py` in the root folder. This will be only used for evaluation. |
| 36 | ```python |
| 37 | import openai |
| 38 | |
| 39 | openai.api_key = "<your-key>" |
| 40 | # openai.organization = "<your-org>" |
| 41 | openai.api_base = "https://api.openai.com/v1" |
| 42 | openai_model_types = ['text-davinci-003'] |
| 43 | ``` |
| 44 | > :warning: **Warning:** Setting `echo` and `logprobs` simultaneously is no longer supported for certain OpenAI models. |
| 45 | > However, classification inference with openai models requires both settings. Consider to host your own models, e.g., thru vLLM, instead. |
| 46 | |
| 47 | **Example**: Do prompt engineer on website: |
| 48 | ```shell |
| 49 | pip install gradio |
| 50 | python web_demo.py |
| 51 | # open http://127.0.0.1:7860 |
| 52 | ``` |
| 53 | |
| 54 | | Train | Test | |
| 55 | | :---- | :--- | |
| 56 | |  |  | |
| 57 | |
| 58 | **Example**: Use local model (`lmsys/vicuna-7b-v1.3`) to generate a instruction and test the instruction by OpenAI model (`text-davinci-003`). |
| 59 | * OPT: |
| 60 | ```shell |
| 61 | # generate a instruction |
| 62 | python train_opt.py --ape_mode=iid_ibwd --ensemble_gen=True --gen_temp=1.1 --num_prompt=40 --max_new_tokens=50 \ |
| 63 | --data=sst2 |