-
Notifications
You must be signed in to change notification settings - Fork 623
Expand file tree
/
Copy pathcode_execution_approval.py
More file actions
73 lines (62 loc) · 2.02 KB
/
Copy pathcode_execution_approval.py
File metadata and controls
73 lines (62 loc) · 2.02 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""
Python SDK code-execution approval callback.
Run with:
pip install -e mistralrs-pyo3 --features code-execution
python examples/python/code_execution_approval.py
"""
from mistralrs import (
AgentPermission,
AgentToolApproval,
AgentToolApprovalDecision,
AgentToolKind,
ChatCompletionRequest,
CodeExecutionConfig,
Runner,
Which,
)
def approve(call: AgentToolApproval):
print("\nAgent action approval required")
print(f"approval_id: {call.approval_id}")
print(f"session_id: {call.session_id}")
print(f"tool: {call.tool.label} ({call.tool.kind})")
if call.tool.kind == AgentToolKind.CodeExecution:
print("\nCode:")
print(call.code or "<no code>")
else:
print("\nArguments:")
print(call.arguments_json)
while True:
decision = (
input("\nRun this Python code? [y]es / [n]o / [a]lways: ").strip().lower()
)
if decision in {"y", "yes"}:
return AgentToolApprovalDecision.approve()
if decision in {"a", "always"}:
return AgentToolApprovalDecision.approve(remember_for_session=True)
if decision in {"", "n", "no"}:
return AgentToolApprovalDecision.deny("The user denied this action.")
print("Please enter y, n, or a.")
def main():
runner = Runner(
which=Which.Plain(model_id="Qwen/Qwen3-4B"),
code_execution_config=CodeExecutionConfig(),
)
response = runner.send_chat_completion_request(
ChatCompletionRequest(
model="default",
messages=[
{
"role": "user",
"content": "Use Python to calculate the first 20 Fibonacci numbers.",
}
],
enable_code_execution=True,
agent_permission=AgentPermission.Ask,
agent_approval_callback=approve,
max_tool_rounds=4,
)
)
for choice in response.choices:
print(choice.message.content)
if __name__ == "__main__":
main()