python

package
v0.5.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 13, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

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

func CalculatorTool

func CalculatorTool() tool.Tool

CalculatorTool 返回计算器工具

func DataAnalysisTool

func DataAnalysisTool() tool.Tool

DataAnalysisTool 返回数据分析工具

func JSONProcessorTool

func JSONProcessorTool() tool.Tool

JSONProcessorTool 返回 JSON 处理工具

func TextProcessorTool

func TextProcessorTool() tool.Tool

TextProcessorTool 返回文本处理工具

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 工具配置

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig 返回默认配置

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 执行代码输入

type ExecuteOutput

type ExecuteOutput struct {
	Output   string `json:"output"`
	Error    string `json:"error,omitempty"`
	ExitCode int    `json:"exit_code"`
	Duration string `json:"duration"`
}

ExecuteOutput 执行代码输出

type Tool

type Tool struct {
	// contains filtered or unexported fields
}

Tool Python REPL 工具

func New

func New(config *Config) *Tool

New 创建 Python REPL 工具

func (*Tool) REPLTool

func (t *Tool) REPLTool() tool.Tool

REPLTool 返回 Python REPL 工具

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL