-
Notifications
You must be signed in to change notification settings - Fork 620
Expand file tree
/
Copy pathcode_execution.py
More file actions
59 lines (49 loc) · 1.61 KB
/
code_execution.py
File metadata and controls
59 lines (49 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""
Python code execution with the OS-level sandbox enabled.
Equivalent of `mistralrs/examples/advanced/code_execution/main.rs` for the
Python SDK. The model is given the `execute_python` tool and runs Python
inside a per-session subprocess that is hardened with rlimits + seccomp +
namespaces + Landlock on Linux (Seatbelt + rlimits on macOS).
Run with:
pip install -e mistralrs-pyo3 --features code-execution
python examples/python/code_execution.py
"""
from mistralrs import (
ChatCompletionRequest,
CodeExecutionConfig,
NetworkMode,
Runner,
SandboxPolicy,
Which,
)
def main():
# Construct an OS-level sandbox policy. Pass `sandbox_policy=None`
# (or omit it) to disable the sandbox - the spawned interpreter will
# then have full filesystem and network access.
sandbox = SandboxPolicy(
max_memory_mb=1024,
max_cpu_secs=120,
max_procs=32,
network=NetworkMode.NoNetwork,
)
runner = Runner(
which=Which.Plain(model_id="Qwen/Qwen3-4B"),
code_execution_config=CodeExecutionConfig(sandbox_policy=sandbox),
)
response = runner.send_chat_completion_request(
ChatCompletionRequest(
model="default",
messages=[
{
"role": "user",
"content": "Use Python to calculate the first 20 prime numbers and their sum.",
}
],
enable_code_execution=True,
max_tool_rounds=4,
)
)
for choice in response.choices:
print(choice.message.content)
if __name__ == "__main__":
main()