humanus.cpp/agent/toolcall.h

63 lines
2.2 KiB
C
Raw Normal View History

2025-03-16 17:17:01 +08:00
#ifndef HUMANUS_AGENT_TOOLCALL_H
#define HUMANUS_AGENT_TOOLCALL_H
#include "react.h"
#include "../prompt.h"
#include "../tool/tool_collection.h"
#include "../tool/create_chat_completion.h"
#include "../tool/terminate.h"
namespace humanus {
const char* TOOL_CALL_REQUIRED = "Tool calls required but none provided";
// Base agent class for handling tool/function calls with enhanced abstraction
struct ToolCallAgent : ReActAgent {
std::vector<ToolCall> tool_calls;
ToolCollection available_tools;
std::string tool_choice;
std::set<std::string> special_tool_names;
ToolCallAgent(
const ToolCollection& available_tools = ToolCollection(
{
std::make_shared<CreateChatCompletion>(),
std::make_shared<Terminate>()
}
),
const std::string& tool_choice = "auto",
const std::set<std::string>& special_tool_names = {"terminate"},
const std::string& name = "toolcall",
const std::string& description = "an agent that can execute tool calls.",
const std::string& system_prompt = prompt::toolcall::SYSTEM_PROMPT,
const std::string& next_step_prompt = prompt::toolcall::NEXT_STEP_PROMPT,
int max_steps = 30,
int current_step = 0,
int duplicate_threshold = 2
) : ReActAgent(name, description, system_prompt, next_step_prompt, max_steps, current_step, duplicate_threshold),
available_tools(available_tools),
tool_choice(tool_choice),
special_tool_names(special_tool_names) {}
// Process current state and decide next actions using tools
bool think() override;
// Execute tool calls and handle their results
std::string act() override;
std::string execute_tool(ToolCall tool_call);
// Handle special tool execution and state changes
void _handle_special_tool(const std::string& name, const ToolResult& result, const json& kwargs = {});
// Determine if tool execution should finish the agent
static bool _should_finish_execution(const std::string& name, const ToolResult& result, const json& kwargs = {});
bool _is_special_tool(const std::string& name);
};
}
#endif // HUMANUS_AGENT_TOOLCALL_H