110 lines
2.8 KiB
Julia
110 lines
2.8 KiB
Julia
# Tool Use Example - MCTS with External Tools
|
|
|
|
This example shows how MCTS can coordinate with external tools (like APIs, databases, or other services).
|
|
|
|
```julia
|
|
using LLMMCTS
|
|
|
|
# Simulated tool interface
|
|
struct Tool
|
|
name::String
|
|
description::String
|
|
end
|
|
|
|
const AVAILABLE_TOOLS = [
|
|
Tool("calculator", "Perform mathematical calculations"),
|
|
Tool("web_search", "Search the web for information"),
|
|
Tool("database_query", "Query a database")
|
|
]
|
|
|
|
# State tracks which tools have been used and their results
|
|
function tool_use_transition(state::Dict, args::NamedTuple)
|
|
current_step = get(state, :step, 0)
|
|
tools_used = get(state, :tools_used, String[])
|
|
|
|
# LLM would decide which tool to use
|
|
# For this example, we try tools in order
|
|
tool_idx = (current_step - 1) % length(AVAILABLE_TOOLS) + 1
|
|
|
|
if tool_idx > length(AVAILABLE_TOOLS)
|
|
# All tools tried, return terminal state
|
|
newstate = Dict(
|
|
:step => current_step + 1,
|
|
:tools_used => tools_used,
|
|
:reward => 8.0,
|
|
:isterminal => true
|
|
)
|
|
return Dict(
|
|
:newNodeKey => "all_tools_tried",
|
|
:newstate => newstate,
|
|
:progressvalue => 8.0
|
|
)
|
|
end
|
|
|
|
tool = AVAILABLE_TOOLS[tool_idx]
|
|
|
|
# Simulate tool execution
|
|
tool_result = "Tool '$(tool.name)' executed successfully"
|
|
|
|
# Calculate reward based on progress
|
|
progress = length(tools_used) / length(AVAILABLE_TOOLS)
|
|
reward = progress * 5
|
|
|
|
# Progress value: LLM estimates how close we are to solving
|
|
progressvalue = progress * 10
|
|
|
|
new_tools_used = vcat(tools_used, tool.name)
|
|
|
|
newstate = Dict(
|
|
:step => current_step + 1,
|
|
:tools_used => new_tools_used,
|
|
:current_tool => tool.name,
|
|
:tool_result => tool_result,
|
|
:reward => reward,
|
|
:isterminal => false
|
|
)
|
|
|
|
return Dict(
|
|
:newNodeKey => "tool_$(tool.name)_$current_step",
|
|
:newstate => newstate,
|
|
:progressvalue => progressvalue
|
|
)
|
|
end
|
|
|
|
# Initial state
|
|
initialstate = Dict(
|
|
:step => 0,
|
|
:tools_used => String[],
|
|
:reward => 0,
|
|
:isterminal => false
|
|
)
|
|
|
|
# Transition arguments
|
|
transitionargs = (max_tools = 3,)
|
|
|
|
# Run MCTS
|
|
result = runMCTS(
|
|
initialstate,
|
|
tool_use_transition,
|
|
transitionargs;
|
|
maxiterations = 20,
|
|
explorationweight = 1.2,
|
|
maxSimulationDepth = 4,
|
|
horizontalSampleExpansionPhase = 3
|
|
)
|
|
|
|
# Display results
|
|
println("Available tools:")
|
|
for tool in AVAILABLE_TOOLS
|
|
println(" - $(tool.name): $(tool.description)")
|
|
end
|
|
println()
|
|
println("Best tool usage sequence:")
|
|
for tool in result.bestTerminalState[:tools_used]
|
|
println(" → Used: $tool")
|
|
end
|
|
println()
|
|
println("Root node visits: ", result.root.visits)
|
|
println("High value states found: ", length(result.highValueStateList))
|
|
```
|