Skip to content

Commit

Permalink
feat: nitro add debug build
Browse files Browse the repository at this point in the history
  • Loading branch information
tikikun committed Oct 13, 2023
1 parent b1e1e59 commit 4bd035b
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 39 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ if(LLAMA_CUBLAS)
add_compile_definitions(GGML_USE_CUBLAS)
endif()
endif()

if(DEBUG)
message(STATUS "NITRO DEBUG IS ON")
add_compile_definitions(ALLOW_ALL_CORS)
endif()

add_subdirectory(llama.cpp)
add_executable(${PROJECT_NAME} main.cc)

Expand Down
3 changes: 2 additions & 1 deletion controllers/health.cc
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#include "health.h"
#include "utils/nitro_utils.h"

void health::asyncHandleHttpRequest(
const HttpRequestPtr &req,
std::function<void(const HttpResponsePtr &)> &&callback) {
auto resp = HttpResponse::newHttpResponse();
auto resp = nitro_utils::nitroHttpResponse();
resp->setStatusCode(k200OK);
resp->setContentTypeCode(CT_TEXT_HTML);
resp->setBody("Nitro is alive!!!");
Expand Down
12 changes: 6 additions & 6 deletions controllers/llamaCPP.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void llamaCPP::chatCompletion(
if (!model_loaded) {
Json::Value jsonResp;
jsonResp["message"] = "Model is not loaded yet";
auto resp = drogon::HttpResponse::newHttpJsonResponse(jsonResp);
auto resp = nitro_utils::nitroHttpJsonResponse(jsonResp);
resp->setStatusCode(drogon::k500InternalServerError);
callback(resp);
return;
Expand Down Expand Up @@ -205,8 +205,8 @@ void llamaCPP::chatCompletion(
return 0;
};

auto resp = drogon::HttpResponse::newStreamResponse(chunked_content_provider,
"chat_completions.txt");
auto resp = nitro_utils::nitroStreamResponse(chunked_content_provider,
"chat_completions.txt");
callback(resp);
}

Expand All @@ -216,7 +216,7 @@ void llamaCPP::embedding(
if (!model_loaded) {
Json::Value jsonResp;
jsonResp["message"] = "Model is not loaded yet";
auto resp = drogon::HttpResponse::newHttpJsonResponse(jsonResp);
auto resp = nitro_utils::nitroHttpJsonResponse(jsonResp);
resp->setStatusCode(drogon::k500InternalServerError);
callback(resp);
return;
Expand Down Expand Up @@ -282,13 +282,13 @@ void llamaCPP::loadModel(
LOG_ERROR << "Error loading the model will exit the program";
Json::Value jsonResp;
jsonResp["message"] = "Model loaded failed";
auto resp = drogon::HttpResponse::newHttpJsonResponse(jsonResp);
auto resp = nitro_utils::nitroHttpJsonResponse(jsonResp);
resp->setStatusCode(drogon::k500InternalServerError);
callback(resp);
}
Json::Value jsonResp;
jsonResp["message"] = "Model loaded successfully";
model_loaded = true;
auto resp = drogon::HttpResponse::newHttpJsonResponse(jsonResp);
auto resp = nitro_utils::nitroHttpJsonResponse(jsonResp);
callback(resp);
}
104 changes: 72 additions & 32 deletions utils/nitro_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
#include "cstdio"
#include "random"
#include "string"
#include <algorithm>
#include <drogon/HttpResponse.h>
#include <iostream>
#include <ostream>
#include <algorithm>

namespace nitro_utils {
inline std::string generate_random_string(std::size_t length) {
Expand All @@ -23,39 +24,78 @@ inline std::string generate_random_string(std::size_t length) {
return random_string;
}

inline void nitro_logo(){
std::string rainbowColors[] = {
"\033[93m", // Yellow
"\033[94m", // Blue
};

std::string resetColor = "\033[0m";
std::string asciiArt =
" ___ ___ ___ \n"
" /__/\ ___ ___ / /\\ / /\\ \n"
" \\ \\:\\ / /\\ / /\\ / /::\\ / /::\\ \n"
" \\ \\:\\ / /:/ / /:/ / /:/\\:\\ / /:/\\:\\ \n"
" _____\\__\\:\\ /__/::\\ / /:/ / /:/ \\:\\ / /:/ \\:\\ \n"
" /__/::::::::\\ \\__\\/\\:\\__ / /::\\ /__/:/ /:/___ /__/:/ \\__\\:\\\n"
" \\ \\:\\~~\\~~\\/ \\ \\:\\/\\ /__/:/\\:\\ \\ \\:\\/:::::/ \\ \\:\\ / /:/\n"
" \\ \\:\\ ~~~ \\__\\::/ \\__\\/ \\:\\ \\ \\::/~~~~ \\ \\:\\ /:/ \n"
" \\ \\:\\ /__/:/ \\ \\:\\ \\ \\:\\ \\ \\:\\/:/ \n"
" \\ \\:\\ \\__\\/ \\__\\/ \\ \\:\\ \\ \\::/ \n"
" \\__\\/ \\__\\/ \\__\\/ \n";

int colorIndex = 0;

for (char c : asciiArt) {
if (c == '\n') {
std::cout << resetColor << c;
colorIndex = 0;
} else {
std::cout << rainbowColors[colorIndex % 2] << c;
colorIndex++;
}
inline void nitro_logo() {
std::string rainbowColors[] = {
"\033[93m", // Yellow
"\033[94m", // Blue
};

std::string resetColor = "\033[0m";
std::string asciiArt =
" ___ ___ ___ \n"
" /__/\ ___ ___ / /\\ / /\\ \n"
" \\ \\:\\ / /\\ / /\\ / /::\\ / /::\\ "
" \n"
" \\ \\:\\ / /:/ / /:/ / /:/\\:\\ / /:/\\:\\ "
" \n"
" _____\\__\\:\\ /__/::\\ / /:/ / /:/ \\:\\ / /:/ "
"\\:\\ \n"
" /__/::::::::\\ \\__\\/\\:\\__ / /::\\ /__/:/ /:/___ /__/:/ "
"\\__\\:\\\n"
" \\ \\:\\~~\\~~\\/ \\ \\:\\/\\ /__/:/\\:\\ \\ \\:\\/:::::/ \\ "
"\\:\\ / /:/\n"
" \\ \\:\\ ~~~ \\__\\::/ \\__\\/ \\:\\ \\ \\::/~~~~ \\ "
"\\:\\ /:/ \n"
" \\ \\:\\ /__/:/ \\ \\:\\ \\ \\:\\ \\ "
"\\:\\/:/ \n"
" \\ \\:\\ \\__\\/ \\__\\/ \\ \\:\\ \\ "
"\\::/ \n"
" \\__\\/ \\__\\/ \\__\\/ "
"\n";

int colorIndex = 0;

for (char c : asciiArt) {
if (c == '\n') {
std::cout << resetColor << c;
colorIndex = 0;
} else {
std::cout << rainbowColors[colorIndex % 2] << c;
colorIndex++;
}
}

std::cout << resetColor; // Reset color at the endreturn;
}

inline drogon::HttpResponsePtr nitroHttpResponse() {
auto resp = drogon::HttpResponse::newHttpResponse();
#ifdef ALLOW_ALL_CORS
LOG_INFO << "Respond for all cors!";
resp->addHeader("Access-Control-Allow-Origin", "*");
#endif
return resp;
}

inline drogon::HttpResponsePtr nitroHttpJsonResponse(const Json::Value &data) {
auto resp = drogon::HttpResponse::newHttpJsonResponse(data);
#ifdef ALLOW_ALL_CORS
LOG_INFO << "Respond for all cors!";
resp->addHeader("Access-Control-Allow-Origin", "*");
#endif
return resp;
};

std::cout << resetColor; // Reset color at the endreturn;
inline drogon::HttpResponsePtr nitroStreamResponse(
const std::function<std::size_t(char *, std::size_t)> &callback,
const std::string &attachmentFileName = "") {
auto resp =
drogon::HttpResponse::newStreamResponse(callback, attachmentFileName);
#ifdef ALLOW_ALL_CORS
LOG_INFO << "Respond for all cors!";
resp->addHeader("Access-Control-Allow-Origin", "*");
#endif
return resp;
}

} // namespace nitro_utils

0 comments on commit 4bd035b

Please sign in to comment.