Documentation
¶
Overview ¶
Package python 提供 AI Agent 的 Python 代码执行工具
本包实现了 Python REPL 工具,支持:
- Python 代码执行
- 虚拟环境支持
- 模块导入控制
安全特性:
- 模块白名单/黑名单
- 执行超时
- 输出大小限制
使用示例:
repl := python.New(python.DefaultConfig())
tool := repl.Tool()
result, _ := tool.Execute(ctx, map[string]any{
"code": "print(1 + 1)",
})
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Config ¶
type Config struct {
// PythonPath Python 解释器路径
PythonPath string
// Timeout 执行超时时间
Timeout time.Duration
// WorkDir 工作目录
WorkDir string
// MaxOutputSize 最大输出大小(字节)
MaxOutputSize int
// AllowedModules 允许导入的模块(空表示允许所有)
AllowedModules []string
// DeniedModules 禁止导入的模块
DeniedModules []string
// VirtualEnv 虚拟环境路径
VirtualEnv string
}
Config Python REPL 工具配置
type ExecuteInput ¶
type ExecuteInput struct {
Code string `json:"code" desc:"要执行的 Python 代码" required:"true"`
Timeout int `json:"timeout" desc:"超时时间(秒)"`
Vars map[string]string `json:"vars" desc:"预设变量"`
}
ExecuteInput 执行代码输入
Click to show internal directories.
Click to hide internal directories.