You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- Specify E2B api key and template to use via env variables
- Try load, use e2b library when E2B api key set
- Fallback to try use terrarium sandbox otherwise
- Enable more python packages in e2b sandbox like rdkit via custom e2b template
- Use Async E2B Sandbox
- Parallelize file IO with sandbox
- Add documentation on how to enable E2B as code sandbox instead of Terrarium
# Default URL of Terrarium, the Python sandbox used by Khoj to run code. Its container is specified above
61
+
# Default URL of Terrarium, the default Python sandbox used by Khoj to run code. Its container is specified above
62
62
- KHOJ_TERRARIUM_URL=http://sandbox:8080
63
+
# Uncomment line below to have Khoj run code in remote E2B code sandbox instead of the self-hosted Terrarium sandbox above. Get your E2B API key from https://e2b.dev/.
64
+
# - E2B_API_KEY=your_e2b_api_key
63
65
# Default URL of SearxNG, the default web search engine used by Khoj. Its container is specified above
64
66
- KHOJ_SEARXNG_URL=http://search:8080
65
67
# Uncomment line below to use with Ollama running on your local machine at localhost:11434.
Copy file name to clipboardExpand all lines: documentation/docs/features/code_execution.md
+17-7
Original file line number
Diff line number
Diff line change
@@ -3,22 +3,23 @@
3
3
4
4
# Code Execution
5
5
6
-
Khoj can generate and run very simple Python code snippets as well. This is useful if you want to generate a plot, run a simple calculation, or do some basic data manipulation. LLMs by default aren't skilled at complex quantitative tasks. Code generation & execution can come in handy for such tasks.
6
+
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.
7
7
8
-
Just use `/code` in your chat command.
8
+
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](https://docs.khoj.dev/features/chat/#commands)in your chat.
9
9
10
-
### Setup (Self-Hosting)
11
-
Run [Cohere's Terrarium](https://github.com/cohere-ai/cohere-terrarium) on your machine to enable code generation and execution.
10
+
## Setup (Self-Hosting)
11
+
### Terrarium Sandbox
12
+
Use [Cohere's Terrarium](https://github.com/cohere-ai/cohere-terrarium) to host the code sandbox locally on your machine for free.
12
13
13
-
Check the [instructions](https://github.com/cohere-ai/cohere-terrarium?tab=readme-ov-file#development) for running from source.
14
-
15
-
For running with Docker, you can use our [docker-compose.yml](https://github.com/khoj-ai/khoj/blob/master/docker-compose.yml), or start it manually like this:
14
+
To run with Docker, use our [docker-compose.yml](https://github.com/khoj-ai/khoj/blob/master/docker-compose.yml) to automatically setup the Terrarium code sandbox, or start it manually like this:
16
15
17
16
```bash
18
17
docker pull ghcr.io/khoj-ai/terrarium:latest
19
18
docker run -d -p 8080:8080 ghcr.io/khoj-ai/terrarium:latest
20
19
```
21
20
21
+
To run from source, check [these instructions](https://github.com/khoj-ai/cohere-terrarium?tab=readme-ov-file#development).
22
+
22
23
#### Verify
23
24
Verify that it's running, by evaluating a simple Python expression:
[E2B](https://e2b.dev/) allows Khoj to run code on a remote but versatile sandbox with support for more python libraries. This is [not free](https://e2b.dev/pricing).
35
+
36
+
To have Khoj use E2B as the code sandbox:
37
+
1. Generate an API key on [their dashboard](https://e2b.dev/dashboard).
38
+
2. Set the `E2B_API_KEY` environment variable to it on the machine running your Khoj server.
39
+
- When using our [docker-compose.yml](https://github.com/khoj-ai/khoj/blob/master/docker-compose.yml), uncomment and set the `E2B_API_KEY` env var in the `docker-compose.yml` file.
40
+
3. Now restart your Khoj server to switch to using the E2B code sandbox.
You are Khoj, an advanced python programmer. You are tasked with constructing a python program to best answer the user query.
977
-
- The python program will run in a pyodide python sandbox with no network access.
977
+
- The python program will run in a sandbox with no network access.
978
978
- You can write programs to run complex calculations, analyze data, create charts, generate documents to meticulously answer the query.
979
-
- The sandbox has access to the standard library, matplotlib, panda, numpy, scipy, bs4 and sympy packages. The requests, torch, catboost, tensorflow and tkinter packages are not available.
980
979
- List known file paths to required user documents in "input_files" and known links to required documents from the web in the "input_links" field.
981
980
- The python program should be self-contained. It can only read data generated by the program itself and from provided input_files, input_links by their basename (i.e filename excluding file path).
982
981
- Do not try display images or plots in the code directly. The code should save the image or plot to a file instead.
@@ -1030,6 +1029,13 @@
1030
1029
""".strip()
1031
1030
)
1032
1031
1032
+
e2b_sandbox_context="""
1033
+
- The sandbox has access to only the standard library, matplotlib, pandas, numpy, scipy, bs4, sympy, einops, biopython, shapely, plotly and rdkit packages. The requests, torch, catboost, tensorflow and tkinter packages are not available.
1034
+
""".strip()
1035
+
1036
+
terrarium_sandbox_context="""
1037
+
The sandbox has access to the standard library, matplotlib, pandas, numpy, scipy, bs4 and sympy packages. The requests, torch, catboost, tensorflow, rdkit and tkinter packages are not available.
@@ -362,20 +368,23 @@ class ConversationCommand(str, Enum):
362
368
ConversationCommand.Code: "Agent can run Python code to parse information, run complex calculations, create documents and charts.",
363
369
}
364
370
371
+
e2b_tool_description="To run Python code in a E2B sandbox with no network access. Helpful to parse complex information, run calculations, create text documents and create charts with quantitative data. Only matplotlib, pandas, numpy, scipy, bs4, sympy, einops, biopython, shapely and rdkit external packages are available."
372
+
terrarium_tool_description="To run Python code in a Terrarium, Pyodide sandbox with no network access. Helpful to parse complex information, run complex calculations, create plaintext documents and create charts with quantitative data. Only matplotlib, panda, numpy, scipy, bs4 and sympy external packages are available."
373
+
365
374
tool_descriptions_for_llm= {
366
375
ConversationCommand.Default: "To use a mix of your internal knowledge and the user's personal knowledge, or if you don't entirely understand the query.",
367
376
ConversationCommand.General: "To use when you can answer the question without any outside information or personal knowledge",
368
377
ConversationCommand.Notes: "To search the user's personal knowledge base. Especially helpful if the question expects context from the user's notes or documents.",
369
378
ConversationCommand.Online: "To search for the latest, up-to-date information from the internet. Note: **Questions about Khoj should always use this data source**",
370
379
ConversationCommand.Webpage: "To use if the user has directly provided the webpage urls or you are certain of the webpage urls to read.",
371
-
ConversationCommand.Code: "To run Python code in a Pyodide sandbox with no network access. Helpful when need to parse complex information, run complex calculations, create plaintext documents, and create charts with quantitative data. Only matplotlib, panda, numpy, scipy, bs4 and sympy external packages are available.",
ConversationCommand.Notes: "To search the user's personal knowledge base. Especially helpful if the question expects context from the user's notes or documents.",
376
385
ConversationCommand.Online: "To search the internet for information. Useful to get a quick, broad overview from the internet. Provide all relevant context to ensure new searches, not in previous iterations, are performed.",
377
386
ConversationCommand.Webpage: "To extract information from webpages. Useful for more detailed research from the internet. Usually used when you know the webpage links to refer to. Share the webpage links and information to extract in your query.",
378
-
ConversationCommand.Code: "To run Python code in a Pyodide sandbox with no network access. Helpful when need to parse complex information, run complex calculations, create plaintext documents, and create charts with quantitative data. Only matplotlib, panda, numpy, scipy, bs4 and sympy external packages are available.",
0 commit comments