From db68372b81bcafc7c316321b6e8f711a735d3091 Mon Sep 17 00:00:00 2001 From: Debanjum Date: Sun, 30 Mar 2025 09:47:25 +0530 Subject: [PATCH] Update code sandbox prompts to allow network access when using E2B Tell Khoj code writing chat actor that it has access to the network and can use the python requests library in the E2B code sandbox. --- src/khoj/processor/conversation/prompts.py | 11 ++++++----- src/khoj/processor/tools/run_code.py | 4 +++- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/khoj/processor/conversation/prompts.py b/src/khoj/processor/conversation/prompts.py index 20c3ce0d..e59ab304 100644 --- a/src/khoj/processor/conversation/prompts.py +++ b/src/khoj/processor/conversation/prompts.py @@ -974,12 +974,13 @@ Khoj: python_code_generation_prompt = PromptTemplate.from_template( """ You are Khoj, an advanced python programmer. You are tasked with constructing a python program to best answer the user query. -- The python program will run in a sandbox with no network access. +- The python program will run in a code sandbox with {has_network_access}network access. - You can write programs to run complex calculations, analyze data, create charts, generate documents to meticulously answer the query. -- The python program should be self-contained. It can only read data generated by the program itself and any user file paths referenced in your program. - Do not try display images or plots in the code directly. The code should save the image or plot to a file instead. - Write any document, charts etc. to be shared with the user to file. These files can be seen by the user. -- Use as much context from the previous questions and answers as required to generate your code. +- Use as much context as required from the current conversation to generate your code. +- The python program you write should be self-contained. It does not have access to the current conversation. + It can only read data generated by the program itself and any user file paths referenced in your program. {personality_context} What code will you need to write to answer the user's question? @@ -1105,11 +1106,11 @@ Code Execution Results: ) e2b_sandbox_context = """ -- 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. +- The sandbox has access to only the standard library and the requests, matplotlib, pandas, numpy, scipy, bs4, sympy, einops, biopython, shapely, plotly and rdkit packages. The torch, catboost, tensorflow and tkinter packages are not available. """.strip() terrarium_sandbox_context = """ -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. +- The sandbox has access to only the standard library and the matplotlib, pandas, numpy, scipy, bs5 and sympy packages. The requests, torch, catboost, tensorflow, rdkit and tkinter packages are not available. """.strip() # Automations diff --git a/src/khoj/processor/tools/run_code.py b/src/khoj/processor/tools/run_code.py index 419e81ed..43a47204 100644 --- a/src/khoj/processor/tools/run_code.py +++ b/src/khoj/processor/tools/run_code.py @@ -139,12 +139,14 @@ async def generate_python_code( prompts.e2b_sandbox_context if is_e2b_code_sandbox_enabled() else prompts.terrarium_sandbox_context ) personality_context = f"{sandbox_context}\n{personality_context}" + network_access_context = "**NO** " if not is_e2b_code_sandbox_enabled() else "" code_generation_prompt = prompts.python_code_generation_prompt.format( - current_date=utc_date, query=q, chat_history=chat_history, context=context, + has_network_access=network_access_context, + current_date=utc_date, location=location, username=username, personality_context=personality_context,