Skip to content

feat: add inja example #1969

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,16 @@ add_compile_definitions(CORTEX_CPP_VERSION="${CORTEX_CPP_VERSION}")
add_compile_definitions(CORTEX_CONFIG_FILE_PATH="${CORTEX_CONFIG_FILE_PATH}")

option(CMAKE_BUILD_TEST "Enable testing" OFF)
option(CMAKE_BUILD_INJA_TEST "Enable inja example" OFF)
if(CMAKE_BUILD_TEST)
add_subdirectory(test)
endif()

add_subdirectory(cli)

if(CMAKE_BUILD_INJA_TEST)
add_subdirectory(examples/inja)
endif()


find_package(jsoncpp CONFIG REQUIRED)
Expand Down
66 changes: 66 additions & 0 deletions engine/examples/inja/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
project(inja-test C CXX)

include(CheckIncludeFileCXX)

check_include_file_cxx(any HAS_ANY)
check_include_file_cxx(string_view HAS_STRING_VIEW)
check_include_file_cxx(coroutine HAS_COROUTINE)
if(HAS_ANY
AND HAS_STRING_VIEW
AND HAS_COROUTINE)
set(CMAKE_CXX_STANDARD 20)
elseif(HAS_ANY AND HAS_STRING_VIEW)
set(CMAKE_CXX_STANDARD 17)
else()
set(CMAKE_CXX_STANDARD 14)
endif()

set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(OPENSSL_USE_STATIC_LIBS TRUE)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

if(MSVC)
add_compile_options(
$<$<CONFIG:>:/MT> #---------|
$<$<CONFIG:Debug>:/MTd> #---|-- Statically link the runtime libraries
$<$<CONFIG:Release>:/MT> #--|
)

add_compile_options(/utf-8)
add_definitions(-DUNICODE -D_UNICODE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /DUNICODE /D_UNICODE")
endif()

find_package(jsoncpp CONFIG REQUIRED)
find_package(Trantor CONFIG REQUIRED)
set(TARGET_NAME ${PROJECT_NAME})

add_executable(${TARGET_NAME} main.cc
${CMAKE_CURRENT_SOURCE_DIR}/../../extensions/template_renderer.cc
)

target_link_libraries(${TARGET_NAME} PRIVATE JsonCpp::JsonCpp)
target_link_libraries(${TARGET_NAME} PRIVATE Trantor::Trantor)

# ##############################################################################

if(CMAKE_CXX_STANDARD LESS 17)
# With C++14, use boost to support any and std::string_view
message(STATUS "use c++14")
find_package(Boost 1.61.0 REQUIRED)
target_include_directories(${TARGET_NAME} PRIVATE ${Boost_INCLUDE_DIRS})
elseif(CMAKE_CXX_STANDARD LESS 20)
message(STATUS "use c++17")
else()
message(STATUS "use c++20")
endif()

target_include_directories(${TARGET_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../..)
target_include_directories(${TARGET_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})

set_target_properties(${TARGET_NAME} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}
RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
)
5 changes: 5 additions & 0 deletions engine/examples/inja/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Build cortex with `CMAKE_BUILD_INJA_TEST=ON`

```bash title="Run inja-test"
./inja-test --template path_to_template_file --data path_to_data_file
```
17 changes: 17 additions & 0 deletions engine/examples/inja/data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"choices": [
{
"delta": {
"content": " questions"
},
"finish_reason": null,
"index": 0
}
],
"created": 1735372587,
"id": "",
"model": "o1-preview",
"object": "chat.completion.chunk",
"stream": true,
"system_fingerprint": "fp_1ddf0263de"
}
45 changes: 45 additions & 0 deletions engine/examples/inja/main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "extensions/template_renderer.h"
#include "utils/json_helper.h"

void print_help() {
std::cout << "Usage: \ninja-test [options]\n\n";
std::cout << "Options:\n";
std::cout << " --template Path to template file\n";
std::cout << " --data Path to data file\n";

exit(0);
}

int main(int argc, char* argv[]) {
std::filesystem::path template_path;
std::filesystem::path data_path;
for (int i = 0; i < argc; i++) {
if (strcmp(argv[i], "--template") == 0) {
template_path = argv[i + 1];
} else if (strcmp(argv[i], "--data") == 0) {
data_path = argv[i + 1];
} else if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0) {
print_help();
}
}

auto get_str = [](const std::filesystem::path& p) {
std::ifstream ifs(p);
std::stringstream buffer;
buffer << ifs.rdbuf();
return buffer.str();
};

std::string tpl = get_str(template_path);
std::string message = get_str(data_path);
auto data = json_helper::ParseJsonString(message);

extensions::TemplateRenderer rdr;
auto res = rdr.Render(tpl, data);
std::cout << std::endl;
std::cout << "Result: " << std::endl;
std::cout << res << std::endl;
std::cout << std::endl;

return 0;
}
10 changes: 10 additions & 0 deletions engine/examples/inja/template.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
{% set first = true %}
{% for key, value in input_request %}
{% if key == "choices" or key == "created" or key == "model" or key == "service_tier" or key == "system_fingerprint" or key == "stream" or key == "object" or key == "usage" %}
{% if not first %},{% endif %}
"{{ key }}": {{ tojson(value) }}
{% set first = false %}
{% endif %}
{% endfor %}
}
3 changes: 2 additions & 1 deletion engine/services/model_source_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ std::vector<ModelInfo> ParseJsonString(const std::string& json_str) {
ModelSourceService::ModelSourceService(
std::shared_ptr<DatabaseService> db_service)
: db_service_(db_service) {
sync_db_thread_ = std::thread(&ModelSourceService::SyncModelSource, this);
// TODO(sang) temporariy comment out because of race condition bug
// sync_db_thread_ = std::thread(&ModelSourceService::SyncModelSource, this);
running_ = true;
}

Expand Down
Loading