Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discussion questions and comparison #22

Open
PylotLight opened this issue Jan 13, 2025 · 3 comments
Open

Discussion questions and comparison #22

PylotLight opened this issue Jan 13, 2025 · 3 comments

Comments

@PylotLight
Copy link

Apologies for the issue but I didn't see a better discussion space/tab in GH or other location to put these.

  1. The other OS alt https://github.com/shroominic/codeinterpreter-api + https://github.com/shroominic/codebox-api suggests it provides isolation in a sandbox to be able to run code. Can you advise how code-interpreter handles/compares to this, what level of isolation is provided here?
  2. How would you set this up as an API instead of CLI in docker e.g curl -x POST localhost:8080/execute {"code": "can you generate script to do x"}
@haseeb-heaven
Copy link
Owner

Comparison of Isolation in Code Interpreter and Codebox API

First question:

The codebox api provides isolation because it runs via API but my code-interpreter runs locally so there is no isolation in there because it runs locally and don't need any, only if you convert this project to API then you would need isolation environment.

Second question:

Setting Up Code-Interpreter as an API in Docker

To set up Code-Interpreter as an HTTP API in Docker, you can create a simple Flask application that exposes an endpoint for code execution. Here's an example setup:

  1. Dockerfile:

Create a Dockerfile to set up the environment:

# Use an official Python runtime as a parent image
FROM python:3.9-slim

# Set the working directory in the container
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Make port 8080 available to the world outside this container
EXPOSE 8080

# Define environment variable
ENV NAME CodeInterpreterAPI

# Run app.py when the container launches
CMD ["python", "app.py"]
  1. requirements.txt:

Specify the required Python packages:

flask
open-code-interpreter
  1. app.py:

Create a Flask application to handle code execution requests:

from flask import Flask, request, jsonify
from codeinterpreter import CodeInterpreterSession

app = Flask(__name__)

@app.route('/execute', methods=['POST'])
def execute_code():
    data = request.get_json()
    code = data.get("code", "")

    with CodeInterpreterSession() as session:
        response = session.generate_response(code)
        return jsonify({"output": response.content})

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=8080)
  1. Build and Run Docker Container:

Build the Docker image and run the container:

docker build -t codeinterpreter-api .
docker run -p 8080:8080 codeinterpreter-api
  1. Testing the API:

You can test the API using curl:

curl -X POST http://localhost:8080/execute -H "Content-Type: application/json" -d '{"code": "print(\'Hello, World!\')"}'

This setup provides a basic HTTP API for running Python code using the Code-Interpreter within a Docker container. The code execution can be isolated and sandboxed using Docker to ensure security.

@PylotLight
Copy link
Author

Thanks for the response!
I think we were hoping for an even greater level of security, as we can see that the other one uses Jupyter kernel and docker as isolation I believe.

Follow up question, how would you handle an issue like trying to generate a docx file where the default lib docx is not valid for py3, and it should use python-docx instead. I had multiple attempts but it always errored trying to install the wrong lib. How would you overwrite it's attempts to install the wrong lib?

@haseeb-heaven
Copy link
Owner

If you have specific issues then please create issues for that i will try to solve it.

Regarding isolation its only for API, but running in Docker or Jupyter kernel can be features please request them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants