Skip to main content

View Source Code

Check out the full source code for this example on GitHub.
This tutorial demonstrates how to build a Code Interpreter Agent that can safely execute Python code generated by an LLM. By leveraging Tensorlake’s isolated sandboxing, you can run arbitrary code without compromising your local environment or production servers.

Overview

The Code Interpreter Agent follows this workflow:
  1. User Request: The user asks a question that requires code execution (e.g., “Calculate the Fibonacci sequence up to 100”).
  2. Code Generation: An OpenAI agent interprets the request and generates the necessary Python code.
  3. Secure Execution: The code is sent to a Tensorlake function running in a secure, isolated container.
  4. Result Retrieval: The execution output (stdout, stderr) is captured and returned to the agent.
  5. Final Answer: The agent formulates a final response based on the code output.

Prerequisites

  • Python 3.11+
  • Tensorlake Account and CLI installed.
  • OpenAI API Key

Implementation (app.py)

Here is the complete implementation for the Code Interpreter Agent.

Running Locally

To test the interpreter locally, add this code block to the end of app.py:
Then run the script:

Deploying to Tensorlake

Deploy your secure code interpreter to the cloud with a single command:
This deployment creates a dedicated, isolated environment for every execution request, ensuring complete safety and scalability for your code interpretation tasks.