humanus.cpp/config.h

129 lines
3.1 KiB
C++

#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"
namespace humanus {
// Get project root directory
static std::filesystem::path get_project_root() {
return std::filesystem::path(__FILE__).parent_path();
}
inline const std::filesystem::path PROJECT_ROOT = get_project_root();
inline const std::filesystem::path WORKSPACE_ROOT = PROJECT_ROOT / "workspace";
struct LLMSettings {
std::string model;
std::string api_key;
std::string base_url;
std::string end_point;
int max_tokens;
double temperature;
LLMSettings(
const std::string& model = "",
const std::string& api_key = "",
const std::string& base_url = "",
const std::string& end_point = "/chat/completions",
int max_tokens = 4096,
double temperature = 1.0
) : model(model), api_key(api_key), base_url(base_url), end_point(end_point),
max_tokens(max_tokens), temperature(temperature) {}
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;
}
};
struct AppConfig {
std::map<std::string, LLMSettings> llm;
};
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;
/**
* @brief Get the config path
* @return The config path
*/
static std::filesystem::path _get_config_path() {
auto root = PROJECT_ROOT;
auto config_path = root / "config" / "config.toml";
if (std::filesystem::exists(config_path)) {
return config_path;
}
auto example_path = root / "config" / "config.example.toml";
if (std::filesystem::exists(example_path)) {
return example_path;
}
throw std::runtime_error("Config file not found");
}
/**
* @brief Load the initial config
*/
void _load_initial_config();
public:
/**
* @brief Get the singleton instance
* @return The config instance
*/
static Config& get_instance() {
if (_instance == nullptr) {
std::lock_guard<std::mutex> lock(_mutex);
if (_instance == nullptr) {
_instance = new Config();
}
}
return *_instance;
}
/**
* @brief Get the LLM settings
* @return The LLM settings map
*/
const std::map<std::string, LLMSettings>& llm() const {
return _config.llm;
}
/**
* @brief Get the app config
* @return The app config
*/
const AppConfig& get_config() const {
return _config;
}
};
} // namespace humanus
#endif // HUMANUS_CONFIG_H