Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit 14712da

Browse files
committed
Merge branch 'readme' of https://github.com/janhq/cortex.cpp into readme
2 parents 3eb7f1e + 4800097 commit 14712da

40 files changed

+1716
-236
lines changed

.github/workflows/cortex-cpp-quality-gate.yml

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ on:
66
paths:
77
[
88
"engine/**",
9+
".github/workflows/cortex-cpp-quality-gate.yml"
910
]
1011
workflow_dispatch:
1112

@@ -24,19 +25,19 @@ jobs:
2425
- os: "linux"
2526
name: "amd64"
2627
runs-on: "ubuntu-20-04-cuda-12-0"
27-
cmake-flags: "-DCORTEX_CPP_VERSION=${{github.event.pull_request.head.sha}} -DCMAKE_BUILD_TEST=ON -DCMAKE_TOOLCHAIN_FILE=/home/runner/actions-runner/_work/cortex.cpp/cortex.cpp/engine/vcpkg/scripts/buildsystems/vcpkg.cmake"
28+
cmake-flags: "-DCORTEX_CPP_VERSION=${{github.event.pull_request.head.sha}} -DCMAKE_BUILD_TEST=ON -DCMAKE_TOOLCHAIN_FILE=vcpkg/scripts/buildsystems/vcpkg.cmake"
2829
build-deps-cmake-flags: ""
2930
ccache-dir: ''
3031
- os: "mac"
3132
name: "amd64"
32-
runs-on: "macos-13"
33-
cmake-flags: "-DCORTEX_CPP_VERSION=${{github.event.pull_request.head.sha}} -DCMAKE_BUILD_TEST=ON -DCMAKE_TOOLCHAIN_FILE=/Users/runner/work/cortex.cpp/cortex.cpp/engine/vcpkg/scripts/buildsystems/vcpkg.cmake"
33+
runs-on: "macos-selfhosted-12"
34+
cmake-flags: "-DCORTEX_CPP_VERSION=${{github.event.pull_request.head.sha}} -DCMAKE_BUILD_TEST=ON -DCMAKE_TOOLCHAIN_FILE=vcpkg/scripts/buildsystems/vcpkg.cmake"
3435
build-deps-cmake-flags: ""
3536
ccache-dir: ''
3637
- os: "mac"
3738
name: "arm64"
38-
runs-on: "macos-latest"
39-
cmake-flags: "-DCORTEX_CPP_VERSION=${{github.event.pull_request.head.sha}} -DCMAKE_BUILD_TEST=ON -DMAC_ARM64=ON -DCMAKE_TOOLCHAIN_FILE=/Users/runner/work/cortex.cpp/cortex.cpp/engine/vcpkg/scripts/buildsystems/vcpkg.cmake"
39+
runs-on: "macos-silicon"
40+
cmake-flags: "-DCORTEX_CPP_VERSION=${{github.event.pull_request.head.sha}} -DCMAKE_BUILD_TEST=ON -DMAC_ARM64=ON -DCMAKE_TOOLCHAIN_FILE=vcpkg/scripts/buildsystems/vcpkg.cmake"
4041
build-deps-cmake-flags: ""
4142
ccache-dir: ''
4243
- os: "windows"
@@ -51,7 +52,18 @@ jobs:
5152
uses: actions/checkout@v3
5253
with:
5354
submodules: recursive
54-
55+
56+
- name: use python for linux
57+
if: runner.os == 'Linux'
58+
uses: actions/setup-python@v4
59+
with:
60+
python-version: '3.9'
61+
62+
- name: Install tools on Linux
63+
if: runner.os == 'Linux'
64+
run: |
65+
python3 -m pip install awscli
66+
5567
- name: Install choco on Windows
5668
if: runner.os == 'Windows'
5769
run: |
@@ -64,6 +76,17 @@ jobs:
6476
Import-Module "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
6577
refreshenv
6678
aws s3 cp s3://${{ secrets.MINIO_BUCKET_NAME }}/cortex-cpp-${{ matrix.os }}-${{ matrix.name }} ${{ matrix.ccache-dir }} --recursive --endpoint ${{ secrets.MINIO_ENDPOINT }}
79+
aws s3 cp s3://${{ secrets.MINIO_BUCKET_NAME }}/cortex-cpp-vcpkg-windows C:\Users\ContainerAdministrator\AppData\Local\vcpkg --recursive --endpoint ${{ secrets.MINIO_ENDPOINT }}
80+
env:
81+
AWS_ACCESS_KEY_ID: "${{ secrets.MINIO_ACCESS_KEY_ID }}"
82+
AWS_SECRET_ACCESS_KEY: "${{ secrets.MINIO_SECRET_ACCESS_KEY }}"
83+
AWS_DEFAULT_REGION: "${{ secrets.MINIO_REGION }}"
84+
85+
- name: Download vcpkg cache from s3
86+
if: runner.os == 'Linux'
87+
continue-on-error: true
88+
run: |
89+
aws s3 cp s3://${{ secrets.MINIO_BUCKET_NAME }}/cortex-cpp-vcpkg-linux /home/runner/.cache/vcpkg --recursive --endpoint ${{ secrets.MINIO_ENDPOINT }} --cli-read-timeout 0
6790
env:
6891
AWS_ACCESS_KEY_ID: "${{ secrets.MINIO_ACCESS_KEY_ID }}"
6992
AWS_SECRET_ACCESS_KEY: "${{ secrets.MINIO_SECRET_ACCESS_KEY }}"
@@ -73,7 +96,7 @@ jobs:
7396
run: |
7497
cd engine
7598
make configure-vcpkg
76-
99+
77100
- name: Build
78101
run: |
79102
cd engine
@@ -100,13 +123,24 @@ jobs:
100123
name: cortex-${{ matrix.os }}-${{ matrix.name }}
101124
path: ./engine/cortex
102125

103-
- name: Upload ccache to s3
126+
- name: Upload windows ccache to s3
104127
continue-on-error: true
105128
if: always() && runner.os == 'Windows'
106129
run: |
107130
Import-Module "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
108131
refreshenv
109132
aws s3 cp ${{ matrix.ccache-dir }} s3://${{ secrets.MINIO_BUCKET_NAME }}/cortex-${{ matrix.os }}-${{ matrix.name }} --recursive --endpoint ${{ secrets.MINIO_ENDPOINT }}
133+
aws s3 cp C:\Users\ContainerAdministrator\AppData\Local\vcpkg s3://${{ secrets.MINIO_BUCKET_NAME }}/cortex-cpp-vcpkg-windows --recursive --endpoint ${{ secrets.MINIO_ENDPOINT }}
134+
env:
135+
AWS_ACCESS_KEY_ID: "${{ secrets.MINIO_ACCESS_KEY_ID }}"
136+
AWS_SECRET_ACCESS_KEY: "${{ secrets.MINIO_SECRET_ACCESS_KEY }}"
137+
AWS_DEFAULT_REGION: "${{ secrets.MINIO_REGION }}"
138+
139+
- name: Upload linux vcpkg cache to s3
140+
continue-on-error: true
141+
if: always() && runner.os == 'Linux'
142+
run: |
143+
aws s3 cp /home/runner/.cache/vcpkg s3://${{ secrets.MINIO_BUCKET_NAME }}/cortex-cpp-vcpkg-linux --recursive --endpoint ${{ secrets.MINIO_ENDPOINT }}
110144
env:
111145
AWS_ACCESS_KEY_ID: "${{ secrets.MINIO_ACCESS_KEY_ID }}"
112146
AWS_SECRET_ACCESS_KEY: "${{ secrets.MINIO_SECRET_ACCESS_KEY }}"

engine/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ find_package(CURL REQUIRED)
8282
add_executable(${TARGET_NAME} main.cc
8383
${CMAKE_CURRENT_SOURCE_DIR}/utils/cpuid/cpu_info.cc
8484
${CMAKE_CURRENT_SOURCE_DIR}/utils/file_logger.cc
85+
${CMAKE_CURRENT_SOURCE_DIR}/utils/modellist_utils.cc
8586
)
8687

8788
target_link_libraries(${TARGET_NAME} PRIVATE httplib::httplib)

engine/commands/cortex_upd_cmd.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ inline bool ReplaceBinaryInflight(const std::filesystem::path& src,
127127
std::filesystem::perms::others_read |
128128
std::filesystem::perms::others_exec);
129129
} catch (const std::exception& e) {
130-
CTL_ERR("Something wrong happened: " << e.what());
130+
CTL_ERR("Something went wrong: " << e.what());
131131
if (std::filesystem::exists(temp)) {
132132
std::rename(temp.string().c_str(), dst.string().c_str());
133133
CLI_LOG("Restored binary file");

engine/commands/model_get_cmd.cc

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,16 @@
44
#include <vector>
55
#include "cmd_info.h"
66
#include "config/yaml_config.h"
7-
#include "trantor/utils/Logger.h"
8-
#include "utils/cortex_utils.h"
97
#include "utils/file_manager_utils.h"
108
#include "utils/logging_utils.h"
119

1210
namespace commands {
1311

14-
ModelGetCmd::ModelGetCmd(std::string model_handle)
15-
: model_handle_(std::move(model_handle)) {}
16-
17-
void ModelGetCmd::Exec() {
12+
void ModelGetCmd::Exec(const std::string& model_handle) {
1813
auto models_path = file_manager_utils::GetModelsContainerPath();
1914
if (std::filesystem::exists(models_path) &&
2015
std::filesystem::is_directory(models_path)) {
21-
CmdInfo ci(model_handle_);
16+
CmdInfo ci(model_handle);
2217
std::string model_file =
2318
ci.branch == "main" ? ci.model_name : ci.model_name + "-" + ci.branch;
2419
bool found_model = false;
@@ -149,4 +144,4 @@ void ModelGetCmd::Exec() {
149144
CLI_LOG("Model not found!");
150145
}
151146
}
152-
}; // namespace commands
147+
}; // namespace commands

engine/commands/model_get_cmd.h

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
#pragma once
22

3-
4-
#include <cmath> // For std::isnan
53
#include <string>
64
namespace commands {
75

86
class ModelGetCmd {
97
public:
10-
11-
ModelGetCmd(std::string model_handle);
12-
void Exec();
13-
14-
private:
15-
std::string model_handle_;
8+
void Exec(const std::string& model_handle);
169
};
17-
} // namespace commands
10+
} // namespace commands

engine/commands/run_cmd.cc

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,8 @@
22
#include "chat_cmd.h"
33
#include "cmd_info.h"
44
#include "config/yaml_config.h"
5-
#include "engine_install_cmd.h"
6-
#include "httplib.h"
7-
#include "model_pull_cmd.h"
85
#include "model_start_cmd.h"
96
#include "server_start_cmd.h"
10-
#include "trantor/utils/Logger.h"
11-
#include "utils/cortex_utils.h"
127
#include "utils/file_manager_utils.h"
138

149
namespace commands {
@@ -46,7 +41,7 @@ void RunCmd::Exec() {
4641
if (!commands::IsServerAlive(host_, port_)) {
4742
CLI_LOG("Starting server ...");
4843
commands::ServerStartCmd ssc;
49-
if(!ssc.Exec(host_, port_)) {
44+
if (!ssc.Exec(host_, port_)) {
5045
return;
5146
}
5247
}

engine/config/gguf_parser.cc

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,23 @@ void GGUFHandler::ModelConfigFromMetadata() {
409409
model_config_.created = std::time(nullptr);
410410
model_config_.model = "model";
411411
model_config_.owned_by = "";
412-
model_config_.version;
412+
model_config_.seed = -1;
413+
model_config_.dynatemp_range = 0.0f;
414+
model_config_.dynatemp_exponent = 1.0f;
415+
model_config_.top_k = 40;
416+
model_config_.min_p = 0.05f;
417+
model_config_.tfs_z = 1.0f;
418+
model_config_.typ_p = 1.0f;
419+
model_config_.repeat_last_n = 64;
420+
model_config_.repeat_penalty = 1.0f;
421+
model_config_.mirostat = false;
422+
model_config_.mirostat_tau = 5.0f;
423+
model_config_.mirostat_eta = 0.1f;
424+
model_config_.penalize_nl = false;
425+
model_config_.ignore_eos = false;
426+
model_config_.n_probs = 0;
427+
model_config_.min_keep = 0;
428+
model_config_.grammar = "";
413429

414430
// Get version, bos, eos id, contex_len, ngl from meta data
415431
for (const auto& [key, value] : metadata_uint8_) {
@@ -522,7 +538,7 @@ void GGUFHandler::ModelConfigFromMetadata() {
522538
for (const auto& [key, value] : metadata_string_) {
523539
if (key.compare("general.name") == 0) {
524540
name = std::regex_replace(value, std::regex(" "), "-");
525-
} else if (key.compare("tokenizer.chat_template") == 0) {
541+
} else if (key.find("chat_template") != std::string::npos) {
526542
if (value.compare(ZEPHYR_JINJA) == 0) {
527543
chat_template =
528544
"<|system|>\n{system_message}</s>\n<|user|>\n{prompt}</"
@@ -564,12 +580,21 @@ void GGUFHandler::ModelConfigFromMetadata() {
564580
}
565581
}
566582

567-
eos_string = tokens[eos_token];
568-
bos_string = tokens[bos_token];
569-
stop.push_back(std::move(eos_string));
583+
try {
584+
if (tokens.size() > eos_token) {
585+
eos_string = tokens[eos_token];
586+
stop.push_back(std::move(eos_string));
587+
} else {
588+
LOG_ERROR << "Can't find stop token";
589+
}
590+
} catch (const std::exception& e) {
591+
LOG_ERROR << "Can't find stop token";
592+
}
570593

571594
model_config_.stop = std::move(stop);
572-
595+
if (chat_template.empty())
596+
chat_template =
597+
"[INST] <<SYS>>\n{system_message}\n<</SYS>>\n{prompt}[/INST]";
573598
model_config_.prompt_template = std::move(chat_template);
574599
model_config_.name = name;
575600
model_config_.model = name;

engine/config/model_config.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,23 @@ struct ModelConfig {
3636
std::size_t created;
3737
std::string object;
3838
std::string owned_by = "";
39+
40+
int seed = -1;
41+
float dynatemp_range = 0.0f;
42+
float dynatemp_exponent = 1.0f;
43+
int top_k = 40;
44+
float min_p = 0.05f;
45+
float tfs_z = 1.0f;
46+
float typ_p = 1.0f;
47+
int repeat_last_n = 64;
48+
float repeat_penalty = 1.0f;
49+
bool mirostat = false;
50+
float mirostat_tau = 5.0f;
51+
float mirostat_eta = 0.1f;
52+
bool penalize_nl = false;
53+
bool ignore_eos = false;
54+
int n_probs = 0;
55+
int min_keep = 0;
56+
std::string grammar;
3957
};
4058
} // namespace config

0 commit comments

Comments
 (0)