You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/en/docs/getting-started/quickstart.md
+151Lines changed: 151 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -153,3 +153,154 @@ if __name__ == '__main__':
153
153
```bash
154
154
python quickstart_app.py
155
155
```
156
+
---
157
+
158
+
## Method 3: Agent Tool Execution
159
+
160
+
When your Agent supports OpenAI Tools (function calling), you can expose sandbox tools as callable functions, allowing the model to trigger tools and execute them in the sandbox.
161
+
162
+
### Use Cases
163
+
- Need the LLM to autonomously decide when to run Python code or Shell commands
164
+
- Want to inject safely controlled code execution capabilities into the Agent
165
+
166
+
### Usage Steps
167
+
1) Create a manager and sandbox, and enable tools
168
+
2) Retrieve the sandbox's tool schema (OpenAI-compatible format)
169
+
3) Call the model (tools=...), collect tool_calls
170
+
4) Execute corresponding tools in the sandbox and append tool messages
171
+
5) Let the model generate the final answer again
172
+
173
+
### Code Example
174
+
````python
175
+
import asyncio
176
+
import json
177
+
import os
178
+
from typing import Any, Dict, List, Optional
179
+
180
+
from openai import OpenAI
181
+
from ms_enclave.sandbox.manager import SandboxManagerFactory
182
+
from ms_enclave.sandbox.model import DockerSandboxConfig, SandboxType
183
+
184
+
asyncdefrun_agent_with_sandbox() -> None:
185
+
"""
186
+
Create a sandbox, bind tools to an agent (qwen-plus via DashScope), and execute tool calls.
187
+
Prints final model output and minimal tool execution results.
# If no tool calls were made, just print the model output
261
+
print("Model output:"+"="*20)
262
+
print(msg.content or"")
263
+
264
+
# Minimal summary of executed tools
265
+
if tool_summaries:
266
+
print("Executed tools:"+"="*20)
267
+
for s in tool_summaries:
268
+
print(f"- {s}")
269
+
270
+
271
+
defmain() -> None:
272
+
"""Entry point."""
273
+
asyncio.run(run_agent_with_sandbox())
274
+
275
+
if__name__=="__main__":
276
+
main()
277
+
278
+
````
279
+
280
+
> Tip: Any model/service compatible with OpenAI Tools can use this pattern; you need to pass the sandbox tool schema to tools and execute each tool_call sequentially.
281
+
282
+
Output Example:
283
+
```text
284
+
[INFO:ms_enclave] Local sandbox manager started
285
+
[INFO:ms_enclave] Created and started sandbox a3odo8es of type docker
286
+
[INFO:ms_enclave] [📦 a3odo8es] hi from sandbox
287
+
[INFO:ms_enclave] [📦 a3odo8es] hello.txt
288
+
Model output:====================
289
+
- Python printed: `hi from sandbox`
290
+
- Computed `123456 * 654321 = 80779853376`
291
+
- The `/sandbox/data` directory contains one file: `hello.txt`
292
+
293
+
Summary: The sandbox successfully executed the print and multiplication tasks, and the data directory listing revealed a single file named `hello.txt`.
0 commit comments