humanus.cpp/config.cpp

97 lines
3.6 KiB
C++

#include "config.h"
#include "logger.h"
#include "toml.hpp"
#include <iostream>
#include <filesystem>
namespace humanus {
// Initialize static members
Config* Config::_instance = nullptr;
std::mutex Config::_mutex;
void Config::_load_initial_config() {
try {
auto config_path = _get_config_path();
std::cout << "Loading config file from: " << config_path.string() << std::endl;
const auto& data = toml::parse_file(config_path.string());
// Load LLM configuration
// Check if llm configuration exists
if (!data.contains("llm") || !data["llm"].is_table()) {
throw std::runtime_error("Config file does not contain `llm` table");
}
const auto& llm_table = *data["llm"].as_table();
LLMSettings llm_settings;
if (llm_table.contains("model") && llm_table["model"].is_string()) {
llm_settings.model = llm_table["model"].as_string()->get();
} else {
throw std::runtime_error("Invalid `model` configuration");
}
if (llm_table.contains("api_key") && llm_table["api_key"].is_string()) {
llm_settings.api_key = llm_table["api_key"].as_string()->get();
} else {
throw std::runtime_error("Invalid `api_key` configuration");
}
if (llm_table.contains("base_url") && llm_table["base_url"].is_string()) {
llm_settings.base_url = llm_table["base_url"].as_string()->get();
} else {
throw std::runtime_error("Invalid `base_url` configuration");
}
if (llm_table.contains("end_point") && llm_table["end_point"].is_string()) {
llm_settings.end_point = llm_table["end_point"].as_string()->get();
}
if (llm_table.contains("max_tokens") && llm_table["max_tokens"].is_integer()) {
llm_settings.max_tokens = llm_table["max_tokens"].as_integer()->get();
}
if (llm_table.contains("timeout") && llm_table["timeout"].is_integer()) {
llm_settings.timeout = llm_table["timeout"].as_integer()->get();
}
if (llm_table.contains("temperature") && llm_table["temperature"].is_floating_point()) {
llm_settings.temperature = llm_table["temperature"].as_floating_point()->get();
}
if (llm_table.contains("oai_tool_support") && llm_table["oai_tool_support"].is_boolean()) {
llm_settings.oai_tool_support = llm_table["oai_tool_support"].as_boolean()->get();
}
_config.llm["default"] = llm_settings;
// Load tool helper configurations
ToolHelper tool_helper;
if (data.contains("tool_helper") && data["tool_helper"].is_table()) {
const auto& tool_helper_table = *data["tool_helper"].as_table();
if (tool_helper_table.contains("tool_start")) {
tool_helper.tool_start = tool_helper_table["tool_start"].as_string()->get();
}
if (tool_helper_table.contains("tool_end")) {
tool_helper.tool_end = tool_helper_table["tool_end"].as_string()->get();
}
if (tool_helper_table.contains("tool_hint_template")) {
tool_helper.tool_hint_template = tool_helper_table["tool_hint_template"].as_string()->get();
}
}
_config.tool_helper["default"] = tool_helper;
} catch (const std::exception& e) {
std::cerr << "Loading config file failed: " << e.what() << std::endl;
// Set default configuration
_config.llm["default"] = LLMSettings();
_config.tool_helper["default"] = ToolHelper();
}
}
} // namespace humanus