这是张量信任网络游戏和数据清洁管道的源代码。有关该项目的更多详细信息,请参见纸质网站。您也可以使用数据,也可以玩游戏!
如果您在学术出版物中以我们的代码或数据为基础,请引用以下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。