Skip to content

Commit

Permalink
fix: rounding float
Browse files Browse the repository at this point in the history
  • Loading branch information
namchuai committed Nov 28, 2024
1 parent 7dd43fb commit aff3d79
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 15 deletions.
13 changes: 11 additions & 2 deletions engine/config/model_config.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#pragma once

#include <json/json.h>
#include <cmath>
#include <iomanip>
#include <limits>
#include <sstream>
#include <string>
#include <vector>
#include "utils/format_utils.h"

namespace config {
struct ModelConfig {
std::string name;
Expand Down Expand Up @@ -173,6 +172,16 @@ struct ModelConfig {
tp = json["tp"].asInt();
}
}

std::string ToJsonString() const {
auto obj = ToJson();
obj["id"] = obj["model"].asString();
Json::StreamWriterBuilder wbuilder;
wbuilder.settings_["precision"] = 2;
std::string document = Json::writeString(wbuilder, obj);
return document;
}

Json::Value ToJson() const {
Json::Value obj;

Expand Down
1 change: 0 additions & 1 deletion engine/config/yaml_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <string>

#include "model_config.h"
#include "yaml-cpp/yaml.h"

namespace config {
class YamlHandler {
Expand Down
10 changes: 3 additions & 7 deletions engine/controllers/models.cc
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,9 @@ void Models::GetModel(const HttpRequestPtr& req,
.string());
auto model_config = yaml_handler.GetModelConfig();

ret = model_config.ToJson();

ret["id"] = model_config.model;
ret["object"] = "model";
ret["result"] = "OK";
auto resp = cortex_utils::CreateCortexHttpJsonResponse(ret);
resp->setStatusCode(k200OK);
auto ret = model_config.ToJsonString();
auto resp = cortex_utils::CreateCortexHttpTextAsJsonResponse(ret);
resp->setStatusCode(drogon::k200OK);
callback(resp);
} catch (const std::exception& e) {
std::string message =
Expand Down
7 changes: 2 additions & 5 deletions engine/database/models.cc
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
#include "models.h"
#include <algorithm>
#include <iostream>
#include <sstream>
#include "database.h"
#include "utils/result.hpp"
#include "utils/scope_exit.h"

namespace cortex::db {

Models::Models() : db_(cortex::db::Database::GetInstance().db()) {
}
Models::Models() : db_(cortex::db::Database::GetInstance().db()) {}

Models::Models(SQLite::Database& db) : db_(db) {
}
Models::Models(SQLite::Database& db) : db_(db) {}

Models::~Models() {}

Expand Down
11 changes: 11 additions & 0 deletions engine/utils/cortex_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ inline drogon::HttpResponsePtr CreateCortexHttpResponse() {
return res;
}

inline drogon::HttpResponsePtr CreateCortexHttpTextAsJsonResponse(
const std::string& data) {
auto res = drogon::HttpResponse::newHttpResponse();
res->setBody(data);
res->setContentTypeCode(drogon::CT_APPLICATION_JSON);
#if defined(_WIN32)
res->addHeader("date", GetDateRFC1123());
#endif
return res;
};

inline drogon::HttpResponsePtr CreateCortexHttpJsonResponse(
const Json::Value& data) {
auto res = drogon::HttpResponse::newHttpJsonResponse(data);
Expand Down

0 comments on commit aff3d79

Please sign in to comment.