humanus.cpp/config.h

236 lines
7.4 KiB
C
Raw Normal View History

2025-03-16 17:17:01 +08:00
#ifndef HUMANUS_CONFIG_H
#define HUMANUS_CONFIG_H
#include <string>
#include <map>
#include <fstream>
#include <sstream>
#include <mutex>
#include <filesystem>
#include <memory>
#include "schema.h"
2025-03-19 18:44:54 +08:00
#include "prompt.h"
2025-03-16 17:17:01 +08:00
namespace humanus {
2025-03-17 16:35:11 +08:00
// Get project root directory
2025-03-16 17:17:01 +08:00
static std::filesystem::path get_project_root() {
2025-03-17 01:58:37 +08:00
return std::filesystem::path(__FILE__).parent_path();
2025-03-16 17:17:01 +08:00
}
2025-03-18 16:40:16 +08:00
static const std::filesystem::path PROJECT_ROOT = get_project_root();
2025-03-16 17:17:01 +08:00
2025-03-20 16:03:26 +08:00
struct LLMConfig {
2025-03-16 17:17:01 +08:00
std::string model;
std::string api_key;
std::string base_url;
std::string end_point;
int max_tokens;
2025-03-19 18:44:54 +08:00
int timeout;
2025-03-16 17:17:01 +08:00
double temperature;
2025-03-19 18:44:54 +08:00
bool oai_tool_support;
2025-03-16 17:17:01 +08:00
2025-03-20 16:03:26 +08:00
LLMConfig(
2025-03-19 18:44:54 +08:00
const std::string& model = "deepseek-chat",
const std::string& api_key = "sk-",
const std::string& base_url = "https://api.deepseek.com",
const std::string& end_point = "/v1/chat/completions",
2025-03-16 17:17:01 +08:00
int max_tokens = 4096,
2025-03-20 01:12:15 +08:00
int timeout = 120,
2025-03-19 18:44:54 +08:00
double temperature = 1.0,
bool oai_tool_support = true
2025-03-16 17:17:01 +08:00
) : model(model), api_key(api_key), base_url(base_url), end_point(end_point),
2025-03-19 18:44:54 +08:00
max_tokens(max_tokens), timeout(timeout), temperature(temperature), oai_tool_support(oai_tool_support) {}
2025-03-16 17:17:01 +08:00
json to_json() const {
json j;
j["model"] = model;
j["api_key"] = api_key;
j["base_url"] = base_url;
j["end_point"] = end_point;
j["max_tokens"] = max_tokens;
j["temperature"] = temperature;
return j;
}
};
2025-03-23 14:35:54 +08:00
struct ToolParser {
2025-03-19 18:44:54 +08:00
std::string tool_start;
std::string tool_end;
std::string tool_hint_template;
2025-03-23 14:35:54 +08:00
ToolParser(const std::string& tool_start = "<tool_call>", const std::string& tool_end = "</tool_call>", const std::string& tool_hint_template = prompt::toolcall::TOOL_HINT_TEMPLATE)
2025-03-19 18:44:54 +08:00
: tool_start(tool_start), tool_end(tool_end), tool_hint_template(tool_hint_template) {}
2025-03-23 14:35:54 +08:00
static ToolParser get_instance() {
static ToolParser instance;
2025-03-19 18:44:54 +08:00
return instance;
}
static std::string str_replace(std::string& str, const std::string& from, const std::string& to) {
size_t start_pos = 0;
while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
}
return str;
}
std::string hint(const std::string& tool_list) const {
std::string hint_str = tool_hint_template;
hint_str = str_replace(hint_str, "{tool_start}", tool_start);
hint_str = str_replace(hint_str, "{tool_end}", tool_end);
hint_str = str_replace(hint_str, "{tool_list}", tool_list);
return hint_str;
}
json parse(const std::string& content) const {
std::string new_content = content;
json tool_calls = json::array();
size_t pos_start = new_content.find(tool_start);
size_t pos_end = pos_start == std::string::npos ? std::string::npos : new_content.find(tool_end, pos_start + tool_start.size());
if (pos_start != std::string::npos && pos_end == std::string::npos) { // Some might not have tool_end
pos_end = new_content.size();
}
while (pos_start != std::string::npos) {
std::string tool_content = new_content.substr(pos_start + tool_start.size(), pos_end - pos_start - tool_start.size());
if (!tool_content.empty()) {
try {
tool_calls.push_back({
{"type", "function"},
{"function", json::parse(tool_content)}
});
tool_calls.back()["id"] = "call_" + std::to_string(std::chrono::system_clock::now().time_since_epoch().count());
2025-03-23 14:46:39 +08:00
} catch (const json::exception& /* e */) {
2025-03-19 18:44:54 +08:00
throw std::runtime_error("Invalid tool call: " + tool_content);
}
}
auto trim = [](const std::string& str) -> std::string {
auto not_space = [](unsigned char ch) { return !std::isspace(ch); };
auto start = std::find_if(str.begin(), str.end(), not_space);
auto end = std::find_if(str.rbegin(), str.rend(), not_space).base();
if (start >= end) return "";
return std::string(start, end);
};
std::string lhs = trim(new_content.substr(0, pos_start));
std::string rhs = trim(new_content.substr(std::min(pos_end + tool_end.size(), new_content.size())));
new_content = lhs + rhs;
pos_start = new_content.find(tool_start, pos_start); // Previous tool_call has been cut off
pos_end = pos_start == std::string::npos ? std::string::npos : new_content.find(tool_end, pos_start + tool_start.size());
if (pos_start != std::string::npos && pos_end == std::string::npos) { // Some might not have tool_end
pos_end = new_content.size();
}
}
return {
{"content", new_content},
{"tool_calls", tool_calls} // Might be empty if no tool calls found
};
}
json dump(const json& tool_calls) const {
std::string content;
if (!tool_calls.is_array()) {
throw std::runtime_error("Tool calls should be an array");
}
for (const auto& tool_call : tool_calls) {
content += tool_start;
content += tool_call[tool_call["type"]].dump(2);
content += tool_end;
}
return content;
}
};
2025-03-16 17:17:01 +08:00
struct AppConfig {
2025-03-20 16:03:26 +08:00
std::map<std::string, LLMConfig> llm;
2025-03-23 14:35:54 +08:00
std::map<std::string, ToolParser> tool_parser;
2025-03-16 17:17:01 +08:00
};
class Config {
private:
static Config* _instance;
static std::mutex _mutex;
bool _initialized = false;
AppConfig _config;
Config() {
_load_initial_config();
_initialized = true;
}
Config(const Config&) = delete;
Config& operator=(const Config&) = delete;
/**
2025-03-17 16:35:11 +08:00
* @brief Get the config path
* @return The config path
2025-03-16 17:17:01 +08:00
*/
static std::filesystem::path _get_config_path() {
auto root = PROJECT_ROOT;
2025-03-20 16:03:26 +08:00
auto config_path = root / "config" / "config_llm.toml";
2025-03-16 17:17:01 +08:00
if (std::filesystem::exists(config_path)) {
return config_path;
}
2025-03-17 16:35:11 +08:00
throw std::runtime_error("Config file not found");
2025-03-16 17:17:01 +08:00
}
/**
2025-03-17 16:35:11 +08:00
* @brief Load the initial config
2025-03-16 17:17:01 +08:00
*/
void _load_initial_config();
public:
/**
2025-03-17 16:35:11 +08:00
* @brief Get the singleton instance
* @return The config instance
2025-03-16 17:17:01 +08:00
*/
2025-03-16 22:56:03 +08:00
static Config& get_instance() {
2025-03-16 17:17:01 +08:00
if (_instance == nullptr) {
std::lock_guard<std::mutex> lock(_mutex);
if (_instance == nullptr) {
_instance = new Config();
}
}
return *_instance;
}
/**
2025-03-17 16:35:11 +08:00
* @brief Get the LLM settings
* @return The LLM settings map
2025-03-16 17:17:01 +08:00
*/
2025-03-20 16:03:26 +08:00
const std::map<std::string, LLMConfig>& llm() const {
2025-03-16 17:17:01 +08:00
return _config.llm;
}
2025-03-19 18:44:54 +08:00
/**
* @brief Get the tool helpers
* @return The tool helpers map
*/
2025-03-23 14:35:54 +08:00
const std::map<std::string, ToolParser>& tool_parser() const {
return _config.tool_parser;
2025-03-19 18:44:54 +08:00
}
2025-03-16 17:17:01 +08:00
/**
2025-03-17 16:35:11 +08:00
* @brief Get the app config
* @return The app config
2025-03-16 17:17:01 +08:00
*/
2025-03-16 22:56:03 +08:00
const AppConfig& get_config() const {
2025-03-16 17:17:01 +08:00
return _config;
}
};
} // namespace humanus
#endif // HUMANUS_CONFIG_H