Skip to main content

Code Execution

Khoj can generate and run simple Python code as well. This is useful if you want to have Khoj do some data analysis, generate plots and reports. LLMs by default aren't skilled at complex quantitative tasks. Code generation & execution can come in handy for such tasks.

Khoj automatically infers when to use the code tool. You can also tell it explicitly to use the code tool or use the /code slash command in your chat.

Setup (Self-Hosting)

Terrarium Sandbox

Use Cohere's Terrarium to host the code sandbox locally on your machine for free.

To run with Docker, use our docker-compose.yml to automatically setup the Terrarium code sandbox, or start it manually like this:

docker pull ghcr.io/khoj-ai/terrarium:latest
docker run -d -p 8080:8080 ghcr.io/khoj-ai/terrarium:latest

To run from source, check these instructions.

Verify

Verify that it's running, by evaluating a simple Python expression:

curl -X POST -H "Content-Type: application/json" \
--url http://localhost:8080 \
--data-raw '{"code": "1 + 1"}' \
--no-buffer

E2B Sandbox

E2B allows Khoj to run code on a remote but versatile sandbox with support for more python libraries. This is not free.

To have Khoj use E2B as the code sandbox:

  1. Generate an API key on their dashboard.
  2. Set the E2B_API_KEY environment variable to it on the machine running your Khoj server.
    • When using our docker-compose.yml, uncomment and set the E2B_API_KEY env var in the docker-compose.yml file.
  3. Now restart your Khoj server to switch to using the E2B code sandbox.