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.
This commit is contained in:
Debanjum
2025-03-30 09:47:25 +05:30
parent 5b8c2989d6
commit db68372b81
2 changed files with 9 additions and 6 deletions

View File

@@ -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

View File

@@ -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,