-
Notifications
You must be signed in to change notification settings - Fork 127
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
isolated python code interpreter #619
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -590,64 +590,53 @@ Emit type information compatible with PyLance.` | |
````` | ||
|
||
|
||
### `system.python_interpreter` | ||
### `system.python_code_interpreter` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The header should be updated to
|
||
|
||
Python Dockerized code execution | ||
Python Dockerized code execution for data analysis | ||
|
||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The description of the tool should be updated to "Executes python 3.12 code for Data Analysis tasks in a docker container."
|
||
- tool `python_interpreter`: Executes python 3.12 code in a docker container. The process output is returned. Use 'print' to output data. | ||
- tool `python_code_interpreter`: Executes python 3.12 code for Data Analysis tasks in a docker container. The process output is returned. Do not generate visualizations. The only packages available are numpy, pandas, scipy. There is NO network connectivity. Do not attempt to install other packages or make web requests. | ||
|
||
`````js wrap title="system.python_interpreter" | ||
`````js wrap title="system.python_code_interpreter" | ||
system({ | ||
title: "Python Dockerized code execution", | ||
title: "Python Dockerized code execution for data analysis", | ||
}) | ||
|
||
const image = env.vars.pythonImage ?? "python:3.12" | ||
const packages = ["numpy", "pandas", "scipy"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The declaration of the
|
||
|
||
let container = null | ||
|
||
defTool( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The tool name in the definition should be updated to
|
||
"python_interpreter", | ||
"Executes python 3.12 code in a docker container. The process output is returned. Use 'print' to output data.", | ||
"python_code_interpreter", | ||
"Executes python 3.12 code for Data Analysis tasks in a docker container. The process output is returned. Do not generate visualizations. The only packages available are numpy, pandas, scipy. There is NO network connectivity. Do not attempt to install other packages or make web requests.", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The tool description should be updated to match the new functionality and limitations of the
|
||
{ | ||
type: "object", | ||
properties: { | ||
requirements: { | ||
type: "string", | ||
description: `list of pip packages to install using pip. should be using the pip install format: | ||
<package1> | ||
<package2> | ||
`, | ||
}, | ||
main: { | ||
type: "string", | ||
description: "python 3.12 source code to execute", | ||
}, | ||
}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
|
||
required: ["requirements", "main"], | ||
required: ["main"], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
|
||
}, | ||
async (args) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The handling of the
|
||
const { requirements, main = "" } = args | ||
console.log(`python: running code...`) | ||
container = await host.container({ image, networkEnabled: true }) | ||
if (requirements) { | ||
console.log(`installing: ` + requirements) | ||
await container.writeText( | ||
"requirements.txt", | ||
requirements.replace(/[ ,]\s*/g, "\n") | ||
) | ||
const { main = "" } = args | ||
console.log(`python code interpreter: ` + main) | ||
if (!container) { | ||
console.log(`python: preparing container...`) | ||
container = await host.container({ image, networkEnabled: true }) | ||
const res = await container.exec("pip", [ | ||
"install", | ||
"--root-user-action", | ||
"ignore", | ||
"-r", | ||
"requirements.txt", | ||
...packages, | ||
]) | ||
if (res.failed) throw new Error(`Failed to install requirements`) | ||
await container.disconnect() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The section handling the installation of requirements has been removed and should be updated in the documentation.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The addition of
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The call to
|
||
} | ||
|
||
console.log(`code: ` + main) | ||
await container.writeText("main.py", main) | ||
const res = await container.exec("python", ["main.py"]) | ||
return res | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -299,6 +299,22 @@ export class DockerManager { | |
} | ||
} | ||
|
||
const disconnect = async () => { | ||
const networks = await this._docker.listNetworks() | ||
for (const network of networks.filter( | ||
({ Name }) => Name === "bridge" | ||
)) { | ||
const n = await this._docker.getNetwork(network.Id) | ||
if (n) { | ||
const state = await n.inspect() | ||
if (state?.Containers?.[container.id]) { | ||
logVerbose(`container: disconnect ${network.Name}`) | ||
await n.disconnect({ Container: container.id }) | ||
} | ||
} | ||
} | ||
pelikhan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
const c = <ContainerHost>{ | ||
id: container.id, | ||
disablePurge: !!options.disablePurge, | ||
|
@@ -309,6 +325,7 @@ export class DockerManager { | |
writeText, | ||
readText, | ||
copyTo, | ||
disconnect, | ||
} | ||
pelikhan marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
|
||
this.containers.push(c) | ||
await container.start() | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
system({ | ||
title: "Python Dockerized code execution for data analysis", | ||
}) | ||
|
||
const image = env.vars.pythonImage ?? "python:3.12" | ||
const packages = ["numpy", "pandas", "scipy"] | ||
|
||
let container = null | ||
|
||
defTool( | ||
"python_code_interpreter", | ||
"Executes python 3.12 code for Data Analysis tasks in a docker container. The process output is returned. Do not generate visualizations. The only packages available are numpy, pandas, scipy. There is NO network connectivity. Do not attempt to install other packages or make web requests.", | ||
{ | ||
type: "object", | ||
properties: { | ||
main: { | ||
type: "string", | ||
description: "python 3.12 source code to execute", | ||
}, | ||
}, | ||
required: ["main"], | ||
}, | ||
async (args) => { | ||
const { main = "" } = args | ||
console.log(`python code interpreter: ` + main) | ||
if (!container) { | ||
console.log(`python: preparing container...`) | ||
container = await host.container({ image, networkEnabled: true }) | ||
const res = await container.exec("pip", [ | ||
"install", | ||
"--root-user-action", | ||
"ignore", | ||
...packages, | ||
]) | ||
if (res.failed) throw new Error(`Failed to install requirements`) | ||
await container.disconnect() | ||
} | ||
|
||
await container.writeText("main.py", main) | ||
const res = await container.exec("python", ["main.py"]) | ||
return res | ||
} | ||
) |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1594,6 +1594,11 @@ interface ContainerHost extends ShellHost { | |
* Stops and cleans out the container | ||
*/ | ||
stop(): Promise<void> | ||
|
||
/** | ||
* Force disconnect network | ||
*/ | ||
disconnect(): Promise<void> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
|
||
} | ||
|
||
interface PromptContext extends ChatGenerationContext { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The link for the
python_code_interpreter
tool needs to be updated to reflect the new tool name.