#include "jsondata.h" #include #include CJsonOper::CJsonOper(const std::string& user_name, const std::string& model, const std::string& assistant_name) : user_(user_name), model_(model), assistant_(assistant_name) { } CJsonOper::~CJsonOper() { } std::string CJsonOper::format_request(const std::string& content) { std::string model = model_; std::string role = user_; nlohmann::json json_data = {{"model", model}, {"messages", {{{"role", role}, {"content", content}}}}}; return json_data.dump(); } std::vector CJsonOper::split(const std::string& input, const std::string& delimiter) { std::vector result; size_t pos = 0, prev = 0; while ((pos = input.find(delimiter, prev)) != std::string::npos) { result.push_back(input.substr(prev, pos - prev)); prev = pos + delimiter.size(); } result.push_back(input.substr(prev)); return result; } Message CJsonOper::parse(const std::string& data) { Message re; json j = json::parse(data); if (j.contains("choices") && j["choices"].is_array() && !j["choices"].empty()) { const auto& message = j["choices"][0]["message"]; if (message.contains("content")) { re.message_content = message["content"].get(); } if (message.contains("reasoning_content")) { re.reasoning_content = message["reasoning_content"].get(); } } if (j.contains("usage")) { const auto& usage = j["usage"]; if (usage.contains("prompt_tokens")) { re.prompt_tokens = usage["prompt_tokens"].get(); } if (usage.contains("completion_tokens")) { re.completion_tokens = usage["completion_tokens"].get(); } if (usage.contains("total_tokens")) { re.total_tokens = usage["total_tokens"].get(); } } if (j.contains("id")) { re.id = j["id"].get(); } return re; } bool CJsonOper::save_md(const std::string& data, const std::string& id) { std::ofstream of(id + ".md"); if (!of.is_open()) { std::cout << "can't open " << id << std::endl; return false; } of << data; of.close(); return true; } bool CJsonOper::read_txt(const std::string& path, std::string& out) { std::ifstream file(path); if (!file.is_open()) { std::cout << "open failed: " << path << std::endl; return false; } std::istreambuf_iterator iterf(file); std::istreambuf_iterator iter; std::string content(iterf, iter); out = content; return true; } size_t CJsonOper::get_u8_len(unsigned char ch) { if (ch <= 0x7F) { return 1; } else if ((ch & 0xE0) == 0xC0) { return 2; } else if ((ch & 0xF0) == 0xE0) { return 3; } else if ((ch & 0xF8) == 0xF0) { return 4; } else if ((ch & 0xFC) == 0xF8) { return 5; } else if ((ch & 0xFE) == 0xFC) { return 6; } else { std::cerr << "invalid u8 first ch." << std::endl; exit(1); } return 0; } std::string CJsonOper::trim(const std::string& input) { size_t start = input.find_first_not_of(" \t\n\r\f\v"); if (start == std::string::npos) { return ""; } size_t end = input.find_last_not_of(" \t\n\r\f\v"); return input.substr(start, end - start + 1); }