|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Execute code in a sandbox\n", |
| 8 | + "\n", |
| 9 | + "To enhance security and protect yourself from malicious code through prompt injection, \n", |
| 10 | + "we make it possible to run code in a sandbox environment.\n", |
| 11 | + "This notebook explains how to do it." |
| 12 | + ] |
| 13 | + }, |
| 14 | + { |
| 15 | + "cell_type": "markdown", |
| 16 | + "metadata": {}, |
| 17 | + "source": [ |
| 18 | + "## Install the package\n", |
| 19 | + "\n", |
| 20 | + "First of all you need to install the python package. You can use pip to install it" |
| 21 | + ] |
| 22 | + }, |
| 23 | + { |
| 24 | + "cell_type": "code", |
| 25 | + "execution_count": null, |
| 26 | + "metadata": {}, |
| 27 | + "outputs": [], |
| 28 | + "source": [ |
| 29 | + "%pip install pandasai-docker" |
| 30 | + ] |
| 31 | + }, |
| 32 | + { |
| 33 | + "cell_type": "markdown", |
| 34 | + "metadata": {}, |
| 35 | + "source": [ |
| 36 | + "## Execute the code in the sandbox with the agent\n", |
| 37 | + "\n", |
| 38 | + "Please keep in mind the sandbox works offline. \n", |
| 39 | + "Once you have installed the package, you can start the sandbox with the following code.\n", |
| 40 | + "For the purpose of this example, we are going to use bambooLLM as the LLM chosen for the agent." |
| 41 | + ] |
| 42 | + }, |
| 43 | + { |
| 44 | + "cell_type": "code", |
| 45 | + "execution_count": null, |
| 46 | + "metadata": {}, |
| 47 | + "outputs": [], |
| 48 | + "source": [ |
| 49 | + "import pandasai as pai\n", |
| 50 | + "from pandasai import Agent\n", |
| 51 | + "from pandasai_docker import DockerSandbox\n", |
| 52 | + "\n", |
| 53 | + "pai.set_api_key(\"YOUR_API_KEY\")\n", |
| 54 | + "\n", |
| 55 | + "# initialize the sandbox\n", |
| 56 | + "sandbox = DockerSandbox()\n", |
| 57 | + "sandbox.start()\n", |
| 58 | + "\n", |
| 59 | + "# read a csv as df\n", |
| 60 | + "df = pai.read_csv(\"./data/heart.csv\")\n", |
| 61 | + "\n", |
| 62 | + "# pass the csv and the sandbox to the agent\n", |
| 63 | + "agent = Agent([df], memory_size=10, sandbox=sandbox)\n", |
| 64 | + "\n", |
| 65 | + "# Chat with the Agent\n", |
| 66 | + "response = agent.chat(\"plot top five artists streams\")\n", |
| 67 | + "\n", |
| 68 | + "# stop the sandbox (docker container)\n", |
| 69 | + "sandbox.stop()" |
| 70 | + ] |
| 71 | + }, |
| 72 | + { |
| 73 | + "cell_type": "markdown", |
| 74 | + "metadata": {}, |
| 75 | + "source": [ |
| 76 | + "## Customize the sandbox\n", |
| 77 | + "\n", |
| 78 | + "You can decide the name and path of your sandbox by passing them as positional arguments in the DockerSandbox()" |
| 79 | + ] |
| 80 | + }, |
| 81 | + { |
| 82 | + "cell_type": "code", |
| 83 | + "execution_count": null, |
| 84 | + "metadata": {}, |
| 85 | + "outputs": [], |
| 86 | + "source": [ |
| 87 | + "sandbox = DockerSandbox(\"pandaai-sandbox\", \"/path/to/Dockerfile\")\n", |
| 88 | + "\n", |
| 89 | + "# read a csv as df\n", |
| 90 | + "df = pai.read_csv(\"./data/heart.csv\")\n", |
| 91 | + "\n", |
| 92 | + "# pass the csv and the sandbox to the agent\n", |
| 93 | + "agent = Agent([df], memory_size=10, sandbox=sandbox)\n", |
| 94 | + "\n", |
| 95 | + "# Chat with the Agent\n", |
| 96 | + "response = agent.chat(\"plot top five artists streams\")\n", |
| 97 | + "\n", |
| 98 | + "sandbox.stop()" |
| 99 | + ] |
| 100 | + } |
| 101 | + ], |
| 102 | + "metadata": { |
| 103 | + "kernelspec": { |
| 104 | + "display_name": "Python 3", |
| 105 | + "language": "python", |
| 106 | + "name": "python3" |
| 107 | + }, |
| 108 | + "language_info": { |
| 109 | + "codemirror_mode": { |
| 110 | + "name": "ipython", |
| 111 | + "version": 3 |
| 112 | + }, |
| 113 | + "file_extension": ".py", |
| 114 | + "mimetype": "text/x-python", |
| 115 | + "name": "python", |
| 116 | + "nbconvert_exporter": "python", |
| 117 | + "pygments_lexer": "ipython3", |
| 118 | + "version": "3.8.0" |
| 119 | + } |
| 120 | + }, |
| 121 | + "nbformat": 4, |
| 122 | + "nbformat_minor": 4 |
| 123 | +} |
0 commit comments