Documentation
¶
Index ¶
- func CreatePTCAgent(config PTCAgentConfig) (*graph.Runnable, error)
- type CodeExecutor
- func (ce *CodeExecutor) Execute(ctx context.Context, code string) (*ExecutionResult, error)
- func (ce *CodeExecutor) GetToolDefinitions() string
- func (ce *CodeExecutor) GetToolServerURL() string
- func (ce *CodeExecutor) Start(ctx context.Context) error
- func (ce *CodeExecutor) Stop(ctx context.Context) error
- type ExecutionLanguage
- type ExecutionMode
- type ExecutionResult
- type PTCAgentConfig
- type PTCToolNode
- type ToolRequest
- type ToolResponse
- type ToolServer
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CreatePTCAgent ¶
func CreatePTCAgent(config PTCAgentConfig) (*graph.Runnable, error)
CreatePTCAgent creates a new agent that uses programmatic tool calling This agent generates code to call tools programmatically rather than using traditional tool calling with round-trips
Example ¶
package main
import (
"context"
"fmt"
"github.com/tmc/langchaingo/tools"
)
// MockTool for testing
type MockTool struct {
name string
description string
response string
}
func (t MockTool) Name() string {
return t.name
}
func (t MockTool) Description() string {
return t.description
}
func (t MockTool) Call(ctx context.Context, input string) (string, error) {
return t.response, nil
}
func main() {
// This example shows how to create a PTC agent
// Note: This requires a real LLM and won't run in tests
// Create tools
_ = []tools.Tool{
MockTool{
name: "calculator",
description: "Performs arithmetic calculations",
response: "42",
},
}
// In real usage, you would use:
// model, _ := openai.New()
// agent, _ := ptc.CreatePTCAgent(ptc.PTCAgentConfig{
// Model: model,
// Tools: tools,
// Language: ptc.LanguagePython,
// MaxIterations: 10,
// })
// result, _ := agent.Invoke(context.Background(), initialState)
fmt.Println("PTC Agent created successfully")
}
Output: PTC Agent created successfully
Types ¶
type CodeExecutor ¶
type CodeExecutor struct {
Language ExecutionLanguage
Tools []tools.Tool
Timeout time.Duration
WorkDir string
Mode ExecutionMode
// contains filtered or unexported fields
}
CodeExecutor handles the execution of programmatic tool calling code
func NewCodeExecutor ¶
func NewCodeExecutor(language ExecutionLanguage, toolList []tools.Tool) *CodeExecutor
NewCodeExecutor creates a new code executor for PTC Default mode is ModeServer for reliability
func NewCodeExecutorWithMode ¶
func NewCodeExecutorWithMode(language ExecutionLanguage, toolList []tools.Tool, mode ExecutionMode) *CodeExecutor
NewCodeExecutorWithMode creates a new code executor with specified execution mode
func (*CodeExecutor) Execute ¶
func (ce *CodeExecutor) Execute(ctx context.Context, code string) (*ExecutionResult, error)
Execute runs the generated code with access to tools
Example ¶
package main
import (
"context"
"fmt"
"github.com/smallnest/langgraphgo/ptc"
"github.com/tmc/langchaingo/tools"
)
// MockTool for testing
type MockTool struct {
name string
description string
response string
}
func (t MockTool) Name() string {
return t.name
}
func (t MockTool) Description() string {
return t.description
}
func (t MockTool) Call(ctx context.Context, input string) (string, error) {
return t.response, nil
}
func main() {
tools := []tools.Tool{
MockTool{
name: "get_data",
description: "Gets some data",
response: `{"value": 100}`,
},
}
executor := ptc.NewCodeExecutor(ptc.LanguagePython, tools)
ctx := context.Background()
executor.Start(ctx)
defer executor.Stop(ctx)
code := `
# Process data
data = {"numbers": [1, 2, 3, 4, 5]}
total = sum(data["numbers"])
print(f"Total: {total}")
`
result, err := executor.Execute(ctx, code)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Printf("Executed successfully: %t\n", result.Output != "")
}
Output: Executed successfully: true
func (*CodeExecutor) GetToolDefinitions ¶
func (ce *CodeExecutor) GetToolDefinitions() string
GetToolDefinitions returns tool definitions for LLM prompting
func (*CodeExecutor) GetToolServerURL ¶
func (ce *CodeExecutor) GetToolServerURL() string
GetToolServerURL returns the URL of the tool server (empty in direct mode)
type ExecutionLanguage ¶
type ExecutionLanguage string
ExecutionLanguage defines the programming language for code execution
Example ¶
Example of using PTC with different execution languages
package main
import (
"context"
"fmt"
"github.com/smallnest/langgraphgo/ptc"
"github.com/tmc/langchaingo/tools"
)
// MockTool for testing
type MockTool struct {
name string
description string
response string
}
func (t MockTool) Name() string {
return t.name
}
func (t MockTool) Description() string {
return t.description
}
func (t MockTool) Call(ctx context.Context, input string) (string, error) {
return t.response, nil
}
func main() {
tools := []tools.Tool{
MockTool{name: "tool1", description: "A tool", response: "response"},
}
// Python executor
pythonExec := ptc.NewCodeExecutor(ptc.LanguagePython, tools)
fmt.Printf("Python executor created: %v\n", pythonExec != nil)
// Go executor
goExec := ptc.NewCodeExecutor(ptc.LanguageGo, tools)
fmt.Printf("Go executor created: %v\n", goExec != nil)
}
Output: Python executor created: true Go executor created: true
const ( LanguagePython ExecutionLanguage = "python" LanguageGo ExecutionLanguage = "go" )
type ExecutionMode ¶
type ExecutionMode string
ExecutionMode defines how tools are executed in the code
const ( // ModeServer: Tools are called via HTTP server (default, recommended) // - Fully implemented and tested // - Better isolation (sandboxed) // - Reliable tool execution ModeServer ExecutionMode = "server" // ModeDirect: Tools are directly embedded in code (experimental) // - Currently uses placeholder implementations // - Not recommended for production use // - Work in progress ModeDirect ExecutionMode = "direct" )
type ExecutionResult ¶
ExecutionResult contains the result of code execution
type PTCAgentConfig ¶
type PTCAgentConfig struct {
// Model is the LLM to use
Model llms.Model
// Tools are the available tools
Tools []tools.Tool
// Language is the execution language for code
Language ExecutionLanguage
// ExecutionMode determines how tools are executed (default: ModeServer)
// - ModeServer: Tools are executed via HTTP server (recommended, fully implemented)
// - ModeDirect: Tools are executed directly via subprocess (experimental, placeholder implementation)
ExecutionMode ExecutionMode
// SystemPrompt is the system prompt for the agent
SystemPrompt string
// MaxIterations is the maximum number of iterations (default: 10)
MaxIterations int
}
PTCAgentConfig configures a PTC agent
type PTCToolNode ¶
type PTCToolNode struct {
Executor *CodeExecutor
}
PTCToolNode is a graph node that handles programmatic tool calling It receives code from the LLM and executes it with tool access
func NewPTCToolNode ¶
func NewPTCToolNode(language ExecutionLanguage, toolList []tools.Tool) *PTCToolNode
NewPTCToolNode creates a new PTC tool node with default execution mode (server)
func NewPTCToolNodeWithMode ¶
func NewPTCToolNodeWithMode(language ExecutionLanguage, toolList []tools.Tool, mode ExecutionMode) *PTCToolNode
NewPTCToolNodeWithMode creates a new PTC tool node with specified execution mode
type ToolRequest ¶
type ToolRequest struct {
ToolName string `json:"tool_name"`
Input interface{} `json:"input"`
}
ToolRequest represents a tool execution request
type ToolResponse ¶
type ToolResponse struct {
Success bool `json:"success"`
Result string `json:"result"`
Error string `json:"error,omitempty"`
Tool string `json:"tool"`
Input interface{} `json:"input"`
}
ToolResponse represents a tool execution response
type ToolServer ¶
type ToolServer struct {
// contains filtered or unexported fields
}
ToolServer provides an HTTP API for tool execution This allows code in any language to call Go tools via HTTP
Example (RequestFormat) ¶
Example of tool server request/response format
package main
import (
"encoding/json"
"fmt"
)
func main() {
request := map[string]interface{}{
"tool_name": "calculator",
"input": "2 + 2",
}
requestJSON, _ := json.MarshalIndent(request, "", " ")
fmt.Printf("Tool Request:\n%s\n", string(requestJSON))
response := map[string]interface{}{
"success": true,
"result": "4",
"tool": "calculator",
"input": "2 + 2",
}
responseJSON, _ := json.MarshalIndent(response, "", " ")
fmt.Printf("\nTool Response:\n%s\n", string(responseJSON))
}
Output: Tool Request: { "input": "2 + 2", "tool_name": "calculator" } Tool Response: { "input": "2 + 2", "result": "4", "success": true, "tool": "calculator" }
func NewToolServer ¶
func NewToolServer(toolList []tools.Tool) *ToolServer
NewToolServer creates a new tool server
func (*ToolServer) GetBaseURL ¶
func (ts *ToolServer) GetBaseURL() string
GetBaseURL returns the base URL of the server
func (*ToolServer) GetPort ¶
func (ts *ToolServer) GetPort() int
GetPort returns the port the server is listening on