這是張量信任網絡遊戲和數據清潔管道的源代碼。有關該項目的更多詳細信息,請參見紙質網站。您也可以使用數據,也可以玩遊戲!
如果您在學術出版物中以我們的代碼或數據為基礎,請引用以下Bibtex:
@misc { toyer2023tensor ,
title = { {Tensor Trust}: Interpretable Prompt Injection Attacks from an Online Game } ,
author = { Toyer, Sam and Watkins, Olivia and Mendes, Ethan Adrian and Svegliato, Justin and Bailey, Luke and Wang, Tiffany and Ong, Isaac and Elmaaroufi, Karim and Abbeel, Pieter and Darrell, Trevor and Ritter, Alan and Russell, Stuart } ,
year = { 2023 } ,
journal = { arXiv preprint arXiv:2311.01011 } ,
url = { https://arxiv.org/pdf/2311.01011.pdf }
}要安裝和運行,如果尚未安裝OpenAI API密鑰,請先設置OpenAI API密鑰:
https://platform.openai.com/account/api-keys 。set OPENAI_API_KEY=<your-key> ,然後在UNIX運行export OPENAI_API_KEY=<your-key> 。現在運行以下內容:
# Install Redis on Ubuntu. For other OSes see:
# https://redis.io/docs/getting-started/installation/
sudo apt install redis
# If this command fails, try running `redis-server` directly
sudo systemctl enable redis-server
&& sudo systemctl restart redis-server
# Install node.js on Ubuntu. For other OSes see:
# https://nodejs.org/en/download
# If this command doesn't work, try installing using nvm. See
# https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-20-04#option-3-installing-node-using-the-node-version-manager
sudo snap install node --classic
# setup:
conda create -n promptgame python=3.10
conda activate promptgame
pip install -e ' .[dev] '
./manage.py tailwind install # install JS modules for Tailwind
./manage.py migrate # set up database
# For testing, we need two commands.
# Run this first command in one terminal to update the stylesheet in response to Tailwind changes:
./manage.py tailwind start
# Now run this second command in another terminal to a Django server
./manage.py runserver # run demo server (will auto-restart when you edit files)現在,您可以通過http:// localhost訪問網站的開發副本:8000/。
Django用Models處理數據庫管理,我們在src/promptgame/gameui/models.py中定義了模型。每當您編輯Model時,都需要在Django管理的基礎數據庫中反映更改。為此,運行:
./manage.py makemigrations
./manage.py migrate用git術語, makemigrations就像創建一個提交錄製您對數據庫的更改一樣。實際上,在src/promptgame/migrations目錄中的文件中跟踪此遷移。運行migrate就像推動此提交一樣,因此實際上更新了數據庫。要了解有關此過程的更多信息(包括如何執行更複雜的行為,例如將數據庫恢復到以前的遷移狀態),請單擊此處。
請注意,如果您在更改模型後從main中拉出,您還必須運行./manage.py migrate以應用對方生成的新遷移。
要創建一個管理員帳戶,請運行:
./manage.py createsuperuser請按照提示創建用戶名和密碼。
登錄到Localhost的管理頁面:8000/private/dj-login/。在產品網站上,這將在tensortrust.ai/private/dj-login/上進行。
輸入您上面創建的用戶名和密碼。如果您在產品網站上,則必須通過打開終端並運行gcloud secrets versions access --secret=promptgame_prod_application_settings latest 。
Tailwind是CSS框架,它使直接將CSS直接嵌入HTML標籤變得更加容易,而不是將HTML源和CSS源放在不同的地方。它通過將樣式信息填充到一組預定義的類中,例如HTML和尾風的組合,該類別定義了一個圓形的紫色按鈕:
< div class =" ml-8 rounded-md bg-indigo-600 px-3 py-2 text-[0.8125rem]
font-semibold leading-5 text-white hover:bg-indigo-500 " >
This is a button!
</ div >您可能會從這個示例中註意到,可能的尾風類非常大。例如, text-[0.8125rem]使文本0.8125 rem高,但是如果用戶要求0.31 REM或
當然,瀏覽器只能處理有限數量的定義,樣式的類,因此尾風需要某種方法來弄清楚它實際上必須生成哪些類以及可以跳過的類。它使用CSS編譯器來執行此操作。為了開發目的,可以通過將此標籤插入文檔的頭部來動態運行:
< script src =" https://cdn.tailwindcss.com " > </ script >這有效,但有一個較慢的缺點,有時會導致未風格的內容顯示。我也有些擔心,如果我們在生產中使用了它,我們會被禁止使用CDN,但我不知道實際的可能性有多大。
由於這兩個原因,我們相反使用Tailwind的服務器端編譯器(通過Django-Tailwind)。服務器端編譯器用JavaScript編寫,這就是我們需要節點的原因,也是為什麼我們需要運行./manage.py tailwind install以在首次安裝在新計算機上時下載所有Tailwind的依賴項。編譯器將您的源代碼(HTML,Python,JavaScript)掃描是否看起來像尾風類名稱,然後生成所有內容,然後將它們放入此樣式表中:
src/promptgame/theme/static/css/dist/styles.css
樣式表被檢查為版本控件,因此,當您運行./manage.py tailwind start時,實時編譯器所做的更改也將顯示在git diffs中。這有點醜陋,但最終很好,因為生產的styles.css文件只有幾千行。
要使用查看django絲綢UI訪問http://127.0.0.1:8000/silk/。
該項目配置為在GCP上部署。事實證明,它非常複雜,因為我們需要:
內部文檔中所有設置的詳細信息(如果您是需要訪問的Chai關聯公司,請參閱內部TT頻道)。
要部署該網站的新版本,您只需要知道該文檔中內容的一小部分即可。一旦您對prompt-ad-game GCP項目獲得了適當的權限,您就可以剪切新的分期部署:
gcloud auth login && gcloud config set project prompt-ad-gamestaging_image_tag= " $( git rev-parse --short=7 HEAD ) $( git diff --quiet || echo " -drt " ) "
&& gcloud builds submit -t " gcr.io/prompt-ad-game/promptgame-staging: $staging_image_tag "
&& yes | gcloud container images add-tag
gcr.io/prompt-ad-game/promptgame-staging:{ " $staging_image_tag " ,latest}Dockerfile在Repo的根源上構建圖像。該圖像將被命名為gcr.io/prompt-ad-game/promptgame-staging ,其中包括:latest標籤,以及由當前GIT修訂版的最後7位數字組成的標籤。:latest圖像): gcloud run jobs execute promptgame-staging-collect-and-migrate
--region us-central1 --wait./deploy/replace_cloud_run_service.py staging如果所有命令都成功,則該應用程序應在我們的登台站點上運行!您可以將其用作在低風險設置中使用它的機會 - 如果我們的登台網站弄亂了,只要我們在生產之前修復錯誤,那就很好。
一旦您驗證了該應用程序的分期工作,就可以將其推入生產:
0f043fc ,但是您可以使用此命令找到適合您的圖像的正確標籤: gcloud container images list-tags
gcr.io/prompt-ad-game/promptgame-staging # can replace -staging:latest with -staging:<your tag>
yes | gcloud container images add-tag
gcr.io/prompt-ad-game/promptgame-staging:latest
gcr.io/prompt-ad-game/promptgame-prod:latestgcloud run jobs execute promptgame-prod-collect-and-migrate
--region us-central1 --wait./deploy/replace_cloud_run_service.py prod完成所有這些步驟後,您在登台站點上成功運行的代碼也應在登台站點上可用!
我這裡還有很多其他詳細信息,例如如何添加分期和產品之間有所不同的新設置,或者如何從頭開始重新創建登台環境。上面鏈接的(很長的)Google文檔應該回答其中的一些問題,但是如果您想要指針,也可以在Slack上ping sam。