Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sandbox): adding docker sandbox #1517

Merged
merged 19 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
fdcfb87
feat(sandbox): adding docker sandbox
ArslanSaleem Jan 14, 2025
bd18ebf
Merge branch 'release/v3' into feat/sandbox
ArslanSaleem Jan 16, 2025
3c72f5a
feat(sandbox): add serializer and deserializer
ArslanSaleem Jan 20, 2025
f0b5c87
Merge branch 'release/v3' into feat/sandbox
ArslanSaleem Jan 20, 2025
b630742
feat(dockerSandbox): complete implementation of docker sandbox
ArslanSaleem Jan 21, 2025
f880b01
fix(sandbox): spell mistakes
ArslanSaleem Jan 21, 2025
221c1dc
fix(sandbox): fix readme command
ArslanSaleem Jan 21, 2025
a341e07
fix(langchan): poetry lock file update
ArslanSaleem Jan 21, 2025
52d0489
Update pandasai/sandbox/sandbox.py
ArslanSaleem Jan 21, 2025
3ed84fe
fix(docker): make docker not use network
ArslanSaleem Jan 21, 2025
d0dc4c0
Merge branch 'feat/sandbox' of https://github.com/gventuri/pandas-ai …
ArslanSaleem Jan 21, 2025
afb593f
Merge branch 'release/v3' into feat/sandbox
ArslanSaleem Jan 21, 2025
a78039d
fix(ruff): errors in code formatting
ArslanSaleem Jan 21, 2025
5d99577
feat(sandbox): add notebook for docker sandbox
ArslanSaleem Jan 21, 2025
1806976
added notebook and documentation for sandbox
gdcsinaptik Jan 22, 2025
878f325
feat(Sandbox): typo and update documentation
ArslanSaleem Jan 24, 2025
7c18a8c
fix(docker): create docker from command line
ArslanSaleem Jan 24, 2025
bf1bf4f
Update extensions/sandbox/docker/pandasai_docker/docker_sandbox.py
ArslanSaleem Jan 24, 2025
79f71f6
Update extensions/sandbox/docker/pandasai_docker/docker_sandbox.py
ArslanSaleem Jan 24, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions docs/mint.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"logo": {
"light": "/logo/logo.png",
"dark": "/logo/logo.png",
"href": "https://pandas-ai.com"
"href": "https://getpanda.ai"
},
"favicon": "/favicon.svg",
"colors": {
Expand Down Expand Up @@ -72,9 +72,14 @@
"pages": ["v3/ai-dashboards", "v3/share-dataframes", "v3/permission-management"],
"version": "v3"
},
{
"group": "Advanced Usage",
"pages": ["v3/agent"],
"version": "v3"
},
{
"group": "Backwards Compatibility",
"pages": ["v3/agent", "v3/smart-dataframes", "v3/smart-datalakes"],
"pages": ["v3/smart-dataframes", "v3/smart-datalakes"],
"version": "v3"
},
{
Expand Down
66 changes: 61 additions & 5 deletions docs/v3/agent.mdx
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
---
title: 'Agent'
description: 'Add few-shot learning to your PandaAI agent'
title: "Agent"
description: "Add few-shot learning to your PandaAI agent"
---

<Note title="Beta Notice">
Release v3 is currently in beta. This documentation reflects the features and functionality in progress and may change before the final release.
Release v3 is currently in beta. This documentation reflects the features and
functionality in progress and may change before the final release.
</Note>

You can train PandaAI to understand your data better and to improve its performance. Training is as easy as calling the `train` method on the `Agent`.


## Prerequisites

Before you start training PandaAI, you need to set your PandaAI API key.
Before you start training PandaAI, you need to set your PandaAI API key.
You can generate your API key by signing up at [https://app.pandabi.ai](https://app.pandabi.ai).

```python
Expand Down Expand Up @@ -134,6 +134,60 @@ print(response)
# The model will use the information provided in the training to generate a response
```

## Using the Sandbox Environment

To enhance security and protect against malicious code through prompt injection, PandaAI provides a sandbox environment for code execution. The sandbox runs your code in an isolated Docker container, ensuring that potentially harmful operations are contained.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe it would be useful to specify that the docker daemon must be running in the machine before using this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@scaliseraoul Yes definitely we should mention

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ArslanSaleem great, can we add it accordingly?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


### Installation

Before using the sandbox, you need to install Docker on your machine and ensure it is running.

First, install the sandbox package:

```bash
pip install pandasai-docker
```

### Basic Usage

Here's how to use the sandbox with your PandaAI agent:

```python
from pandasai import Agent
from pandasai_docker import DockerSandbox

# Initialize the sandbox
sandbox = DockerSandbox()
sandbox.start()

# Create an agent with the sandbox
df = pai.read_csv("data.csv")
agent = Agent([df], sandbox=sandbox)

# Chat with the agent - code will run in the sandbox
response = agent.chat("Calculate the average sales")

# Don't forget to stop the sandbox when done
sandbox.stop()
```

### Customizing the Sandbox

You can customize the sandbox environment by specifying a custom name and Dockerfile:

```python
sandbox = DockerSandbox(
"custom-sandbox-name",
"/path/to/custom/Dockerfile"
)
```

<Note>
The sandbox works offline and provides an additional layer of security for
code execution. It's particularly useful when working with untrusted data or
when you need to ensure that code execution is isolated from your main system.
</Note>

## Troubleshooting

In some cases, you might get an error like this: `No vector store provided. Please provide a vector store to train the agent`. It means no API key has been generated to use the `BambooVectorStore`.
Expand All @@ -153,9 +207,11 @@ vector_store = BambooVectorStor(api_key="YOUR_PANDABI_API_KEY")
# Instantiate the agent with the custom vector store
agent = Agent(connector, config={...} vectorstore=vector_store)
```

## Custom Head

In some cases, you might want to provide custom data samples to the conversational agent to improve its understanding and responses. For example, you might want to:

- Provide better examples that represent your data patterns
- Avoid sharing sensitive information
- Guide the agent with specific data scenarios
Expand Down
123 changes: 123 additions & 0 deletions examples/docker_sandbox.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Execute code in a sandbox\n",
"\n",
"To enhance security and protect yourself from malicious code through prompt injection, \n",
"we make it possible to run code in a sandbox environment.\n",
"This notebook explains how to do it."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Install the package\n",
"\n",
"First of all you need to install the python package. You can use pip to install it"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%pip install pandasai-docker"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Execute the code in the sandbox with the agent\n",
"\n",
"Please keep in mind the sandbox works offline. \n",
"Once you have installed the package, you can start the sandbox with the following code.\n",
"For the purpose of this example, we are going to use bambooLLM as the LLM chosen for the agent."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pandasai as pai\n",
"from pandasai import Agent\n",
"from pandasai_docker import DockerSandbox\n",
"\n",
"pai.set_api_key(\"YOUR_API_KEY\")\n",
"\n",
"# initialize the sandbox\n",
"sandbox = DockerSandbox()\n",
"sandbox.start()\n",
"\n",
"# read a csv as df\n",
"df = pai.read_csv(\"./data/heart.csv\")\n",
"\n",
"# pass the csv and the sandbox to the agent\n",
"agent = Agent([df], memory_size=10, sandbox=sandbox)\n",
"\n",
"# Chat with the Agent\n",
"response = agent.chat(\"plot top five artists streams\")\n",
"\n",
"# stop the sandbox (docker container)\n",
"sandbox.stop()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Customize the sandbox\n",
"\n",
"You can decide the name and path of your sandbox by passing them as positional arguments in the DockerSandbox()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sandbox = DockerSandbox(\"pandaai-sandbox\", \"/path/to/Dockerfile\")\n",
"\n",
"# read a csv as df\n",
"df = pai.read_csv(\"./data/heart.csv\")\n",
"\n",
"# pass the csv and the sandbox to the agent\n",
"agent = Agent([df], memory_size=10, sandbox=sandbox)\n",
"\n",
"# Chat with the Agent\n",
"response = agent.chat(\"plot top five artists streams\")\n",
"\n",
"sandbox.stop()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.0"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
9 changes: 9 additions & 0 deletions extensions/sandbox/docker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Docker Sandbox Extension for PandasAI

## Installation

You can install this extension using poetry:

```bash
poetry add pandasai-docker
```
12 changes: 12 additions & 0 deletions extensions/sandbox/docker/pandasai_docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM python:3.9

LABEL image_name="pandasai-sandbox"

# Install required Python packages
RUN pip install pandas numpy matplotlib

# Set the working directory inside the container
WORKDIR /app

# Default command keeps the container running (useful for testing or debugging)
CMD ["sleep", "infinity"]
3 changes: 3 additions & 0 deletions extensions/sandbox/docker/pandasai_docker/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .docker_sandbox import DockerSandbox

__all__ = ["DockerSandbox"]
Loading
Loading