Skip to content

Commit c9f5d0e

Browse files
feat: add inja example (#1969)
* feat: add inja example * chore: add README.md * fix: temp comment out sync --------- Co-authored-by: sangjanai <sang@jan.ai>
1 parent da69edf commit c9f5d0e

File tree

7 files changed

+149
-1
lines changed

7 files changed

+149
-1
lines changed

Diff for: engine/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,16 @@ add_compile_definitions(CORTEX_CPP_VERSION="${CORTEX_CPP_VERSION}")
6262
add_compile_definitions(CORTEX_CONFIG_FILE_PATH="${CORTEX_CONFIG_FILE_PATH}")
6363

6464
option(CMAKE_BUILD_TEST "Enable testing" OFF)
65+
option(CMAKE_BUILD_INJA_TEST "Enable inja example" OFF)
6566
if(CMAKE_BUILD_TEST)
6667
add_subdirectory(test)
6768
endif()
6869

6970
add_subdirectory(cli)
7071

72+
if(CMAKE_BUILD_INJA_TEST)
73+
add_subdirectory(examples/inja)
74+
endif()
7175

7276

7377
find_package(jsoncpp CONFIG REQUIRED)

Diff for: engine/examples/inja/CMakeLists.txt

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
project(inja-test C CXX)
2+
3+
include(CheckIncludeFileCXX)
4+
5+
check_include_file_cxx(any HAS_ANY)
6+
check_include_file_cxx(string_view HAS_STRING_VIEW)
7+
check_include_file_cxx(coroutine HAS_COROUTINE)
8+
if(HAS_ANY
9+
AND HAS_STRING_VIEW
10+
AND HAS_COROUTINE)
11+
set(CMAKE_CXX_STANDARD 20)
12+
elseif(HAS_ANY AND HAS_STRING_VIEW)
13+
set(CMAKE_CXX_STANDARD 17)
14+
else()
15+
set(CMAKE_CXX_STANDARD 14)
16+
endif()
17+
18+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
19+
set(CMAKE_CXX_EXTENSIONS OFF)
20+
set(OPENSSL_USE_STATIC_LIBS TRUE)
21+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
22+
23+
if(MSVC)
24+
add_compile_options(
25+
$<$<CONFIG:>:/MT> #---------|
26+
$<$<CONFIG:Debug>:/MTd> #---|-- Statically link the runtime libraries
27+
$<$<CONFIG:Release>:/MT> #--|
28+
)
29+
30+
add_compile_options(/utf-8)
31+
add_definitions(-DUNICODE -D_UNICODE)
32+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /DUNICODE /D_UNICODE")
33+
endif()
34+
35+
find_package(jsoncpp CONFIG REQUIRED)
36+
find_package(Trantor CONFIG REQUIRED)
37+
set(TARGET_NAME ${PROJECT_NAME})
38+
39+
add_executable(${TARGET_NAME} main.cc
40+
${CMAKE_CURRENT_SOURCE_DIR}/../../extensions/template_renderer.cc
41+
)
42+
43+
target_link_libraries(${TARGET_NAME} PRIVATE JsonCpp::JsonCpp)
44+
target_link_libraries(${TARGET_NAME} PRIVATE Trantor::Trantor)
45+
46+
# ##############################################################################
47+
48+
if(CMAKE_CXX_STANDARD LESS 17)
49+
# With C++14, use boost to support any and std::string_view
50+
message(STATUS "use c++14")
51+
find_package(Boost 1.61.0 REQUIRED)
52+
target_include_directories(${TARGET_NAME} PRIVATE ${Boost_INCLUDE_DIRS})
53+
elseif(CMAKE_CXX_STANDARD LESS 20)
54+
message(STATUS "use c++17")
55+
else()
56+
message(STATUS "use c++20")
57+
endif()
58+
59+
target_include_directories(${TARGET_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../..)
60+
target_include_directories(${TARGET_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
61+
62+
set_target_properties(${TARGET_NAME} PROPERTIES
63+
RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}
64+
RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}
65+
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
66+
)

Diff for: engine/examples/inja/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Build cortex with `CMAKE_BUILD_INJA_TEST=ON`
2+
3+
```bash title="Run inja-test"
4+
./inja-test --template path_to_template_file --data path_to_data_file
5+
```

Diff for: engine/examples/inja/data.txt

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"choices": [
3+
{
4+
"delta": {
5+
"content": " questions"
6+
},
7+
"finish_reason": null,
8+
"index": 0
9+
}
10+
],
11+
"created": 1735372587,
12+
"id": "",
13+
"model": "o1-preview",
14+
"object": "chat.completion.chunk",
15+
"stream": true,
16+
"system_fingerprint": "fp_1ddf0263de"
17+
}

Diff for: engine/examples/inja/main.cc

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include "extensions/template_renderer.h"
2+
#include "utils/json_helper.h"
3+
4+
void print_help() {
5+
std::cout << "Usage: \ninja-test [options]\n\n";
6+
std::cout << "Options:\n";
7+
std::cout << " --template Path to template file\n";
8+
std::cout << " --data Path to data file\n";
9+
10+
exit(0);
11+
}
12+
13+
int main(int argc, char* argv[]) {
14+
std::filesystem::path template_path;
15+
std::filesystem::path data_path;
16+
for (int i = 0; i < argc; i++) {
17+
if (strcmp(argv[i], "--template") == 0) {
18+
template_path = argv[i + 1];
19+
} else if (strcmp(argv[i], "--data") == 0) {
20+
data_path = argv[i + 1];
21+
} else if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0) {
22+
print_help();
23+
}
24+
}
25+
26+
auto get_str = [](const std::filesystem::path& p) {
27+
std::ifstream ifs(p);
28+
std::stringstream buffer;
29+
buffer << ifs.rdbuf();
30+
return buffer.str();
31+
};
32+
33+
std::string tpl = get_str(template_path);
34+
std::string message = get_str(data_path);
35+
auto data = json_helper::ParseJsonString(message);
36+
37+
extensions::TemplateRenderer rdr;
38+
auto res = rdr.Render(tpl, data);
39+
std::cout << std::endl;
40+
std::cout << "Result: " << std::endl;
41+
std::cout << res << std::endl;
42+
std::cout << std::endl;
43+
44+
return 0;
45+
}

Diff for: engine/examples/inja/template.txt

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
{% set first = true %}
3+
{% for key, value in input_request %}
4+
{% 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" %}
5+
{% if not first %},{% endif %}
6+
"{{ key }}": {{ tojson(value) }}
7+
{% set first = false %}
8+
{% endif %}
9+
{% endfor %}
10+
}

Diff for: engine/services/model_source_service.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ std::vector<ModelInfo> ParseJsonString(const std::string& json_str) {
6464
ModelSourceService::ModelSourceService(
6565
std::shared_ptr<DatabaseService> db_service)
6666
: db_service_(db_service) {
67-
sync_db_thread_ = std::thread(&ModelSourceService::SyncModelSource, this);
67+
// TODO(sang) temporariy comment out because of race condition bug
68+
// sync_db_thread_ = std::thread(&ModelSourceService::SyncModelSource, this);
6869
running_ = true;
6970
}
7071

0 commit comments

Comments
 (0)