EtiennePerot / safe-code-execution

Code execution utilities for Open WebUI & Ollama
Apache License 2.0
194 stars 12 forks source link

Download files created during code execution #4

Closed EtiennePerot closed 2 months ago

EtiennePerot commented 2 months ago

Right now, if code writes files inside the sandbox, they are simply lost forever. It would be very cool to be able to run code that generates files and then have these files be downloadable from the chat UI.

This idea came from a comment by @sultanjulyan in this issue.

sultanjulyan commented 2 months ago

That's right, I want to create a function that can generate a file and then download it directly from the web UI. Thank you for your attention, I will wait for further developments.

EtiennePerot commented 2 months ago

From some preliminary research, it seems tools and functions can append messages to Open WebUI, but they are limited to either text (markdown) an images. So something like a spreadsheet file (.xlsx) would not be able to be displayed, unless perhaps the tool also had code to convert the spreadsheet to a Markdown table.

Alternatively, it may be possible to show links to download files that aren't markdown/images (as you suggest). Need to look if Open WebUI has some passthrough endpoints that would allow this, or if they could be linked using data URIs (for small files like spreadsheets this might work too).

sultanjulyan commented 2 months ago

Are there any alternative libraries that allow for that besides Web UI?

EtiennePerot commented 2 months ago

Not really. At the end of the day, the output generated by the tool need to be displayed on Open WebUI (that's simply the nature of this tool). We have no control over what functionality Open WebUI will display, but it may be possible to send them PRs to expand this.

Anyway, first feature to implement here is to detect the presence of files at the end of a code execution run and to copy them somewhere outside of the sandbox before the sandbox disappears.

lu4p commented 2 months ago
  1. Files need to be somehow put in /app/backend/data/cache/tools/run_code e.g. /app/backend/data/cache/tools/run_code/hello.txt
  2. This file is then accessible in the webui as http://webui_url/cache/tools/run_code/hello.txt
  3. The tool can provide a message with a markdown link, something like:
    {
    "type": "message",
    "data": {"content": "[Download File](http://webui_url/cache/tools/run_code/hello.txt)"},  
    }

ChatGPT generates code like this, where it outputs the generated filepath to stdout:

import uuid

# Generate a list of 1000 UUIDs
uuid_list = [str(uuid.uuid4()) for _ in range(1000)]

# Save to a text file
file_path = '/mnt/data/uuids.txt'
with open(file_path, 'w') as f:
    for uid in uuid_list:
        f.write(uid + '\n')

file_path
EtiennePerot commented 2 months ago

Excellent. With all these in place, I think this is all feasible now.

To prevent abuse, the tool probably needs a few more valves:

EtiennePerot commented 2 months ago

Another related idea: Letting the sandboxed code have a persistent per-chat "session" directory, in which files created in one session are available in future executions of the tool within the same chat. I'm not sure the current crop of models are really smart enough to think about persistence in this way, but good to think about in advance.

EtiennePerot commented 2 months ago

This is now implemented as of 44b33e38cd9ed43306697a4b22047ea9293ff112. Enjoy!

Code execution function

sultanjulyan commented 1 month ago

I have tried it and succeeded, thank you all.