diff options
| author | Xe Iaso <me@xeiaso.net> | 2024-08-01 19:25:46 -0400 |
|---|---|---|
| committer | Xe Iaso <me@xeiaso.net> | 2024-08-01 19:25:46 -0400 |
| commit | bdcd9eb26211bb5b10b1c41a2ffc609933d46033 (patch) | |
| tree | 3a69390ea9c377518e8e4ab44893918c4bebd969 /llm/codeinterpreter/python | |
| parent | 7041695d49386f6e2cf660179c55f7f8a93b5ed3 (diff) | |
| download | x-bdcd9eb26211bb5b10b1c41a2ffc609933d46033.tar.xz x-bdcd9eb26211bb5b10b1c41a2ffc609933d46033.zip | |
cmd/mimi: try having mimi run Python
Signed-off-by: Xe Iaso <me@xeiaso.net>
Diffstat (limited to 'llm/codeinterpreter/python')
| -rw-r--r-- | llm/codeinterpreter/python/output/.gitignore | 2 | ||||
| -rw-r--r-- | llm/codeinterpreter/python/python.go | 77 | ||||
| -rw-r--r-- | llm/codeinterpreter/python/python.wasm | bin | 0 -> 26267204 bytes | |||
| -rw-r--r-- | llm/codeinterpreter/python/python_test.go | 22 |
4 files changed, 101 insertions, 0 deletions
diff --git a/llm/codeinterpreter/python/output/.gitignore b/llm/codeinterpreter/python/output/.gitignore new file mode 100644 index 0000000..c96a04f --- /dev/null +++ b/llm/codeinterpreter/python/output/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore
\ No newline at end of file diff --git a/llm/codeinterpreter/python/python.go b/llm/codeinterpreter/python/python.go new file mode 100644 index 0000000..06457b1 --- /dev/null +++ b/llm/codeinterpreter/python/python.go @@ -0,0 +1,77 @@ +package python + +import ( + "bytes" + "context" + _ "embed" + "fmt" + "os" + + "github.com/tetratelabs/wazero" + "github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1" +) + +var ( + //go:embed python.wasm + Binary []byte + + r wazero.Runtime + code wazero.CompiledModule +) + +func init() { + ctx := context.Background() + r = wazero.NewRuntime(ctx) + + wasi_snapshot_preview1.MustInstantiate(ctx, r) + + var err error + code, err = r.CompileModule(ctx, Binary) + if err != nil { + panic(err) + } +} + +type Result struct { + Stdout string + Stderr string +} + +func Run(ctx context.Context, tmpDir, userCode string) (*Result, error) { + fout := &bytes.Buffer{} + ferr := &bytes.Buffer{} + fin := &bytes.Buffer{} + + os.WriteFile(tmpDir+"/main.py", []byte(userCode), 0644) + + fsConfig := wazero.NewFSConfig(). + WithFSMount(os.DirFS(tmpDir), "/") + + config := wazero.NewModuleConfig(). + // stdio + WithStdout(fout). + WithStderr(ferr). + WithStdin(fin). + // argv + WithArgs("python", "/main.py"). + WithName("python"). + // fs / system + WithFSConfig(fsConfig). + WithSysNanosleep(). + WithSysNanotime(). + WithSysWalltime() + + mod, err := r.InstantiateModule(ctx, code, config) + if err != nil { + fmt.Println(fout.String()) + fmt.Println(ferr.String()) + return nil, err + } + + defer mod.Close(ctx) + + return &Result{ + Stdout: fout.String(), + Stderr: ferr.String(), + }, nil +} diff --git a/llm/codeinterpreter/python/python.wasm b/llm/codeinterpreter/python/python.wasm Binary files differnew file mode 100644 index 0000000..159a20b --- /dev/null +++ b/llm/codeinterpreter/python/python.wasm diff --git a/llm/codeinterpreter/python/python_test.go b/llm/codeinterpreter/python/python_test.go new file mode 100644 index 0000000..7b22f65 --- /dev/null +++ b/llm/codeinterpreter/python/python_test.go @@ -0,0 +1,22 @@ +package python + +import ( + "context" + "testing" +) + +func TestRun(t *testing.T) { + var code = `import sys + +print(f"Python {sys.version} running in {sys.platform}/wazero.")` + + dir := t.TempDir() + + res, err := Run(context.Background(), dir, code) + if err != nil { + t.Fatal(err) + } + + t.Logf("stdout: %s", res.Stdout) + t.Logf("stderr: %s", res.Stderr) +} |
