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

Commit b466a58

Browse files
committed
Merge branch 'dev' of https://github.com/janhq/cortex into api-update
2 parents 67e872c + 6097606 commit b466a58

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1374
-332
lines changed

.github/pull_request_template.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
## Describe Your Changes
2+
3+
-
4+
5+
## Fixes Issues
6+
7+
- Closes #
8+
- Closes #
9+
10+
## Self Checklist
11+
12+
- [ ] Added relevant comments, esp in complex areas
13+
- [ ] Updated docs (for bug fixes / features)
14+
- [ ] Created issues for follow-up changes or refactoring needed

.github/workflows/cortex-cpp-build.yml

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,11 @@ jobs:
3131
GITHUB_REF: ${{ github.ref }}
3232
- name: Create Draft Release
3333
id: create_release
34-
uses: actions/create-release@v1
35-
env:
36-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
34+
uses: softprops/action-gh-release@v2
3735
with:
3836
tag_name: ${{ github.ref_name }}
39-
release_name: "${{ env.VERSION }}"
37+
token: ${{ secrets.GITHUB_TOKEN }}
38+
name: "${{ env.VERSION }}"
4039
draft: true
4140
prerelease: false
4241

@@ -235,4 +234,28 @@ jobs:
235234
upload_url: ${{ needs.create-draft-release.outputs.upload_url }}
236235
asset_path: ./cortex-cpp/cortex-cpp.tar.gz
237236
asset_name: cortex-cpp-${{ needs.create-draft-release.outputs.version }}-${{ matrix.os }}-${{ matrix.name }}.tar.gz
238-
asset_content_type: application/gzip
237+
asset_content_type: application/gzip
238+
239+
update_release_draft:
240+
needs: [build-and-test]
241+
permissions:
242+
# write permission is required to create a github release
243+
contents: write
244+
# write permission is required for autolabeler
245+
# otherwise, read permission is required at least
246+
pull-requests: write
247+
runs-on: ubuntu-latest
248+
steps:
249+
# (Optional) GitHub Enterprise requires GHE_HOST variable set
250+
#- name: Set GHE_HOST
251+
# run: |
252+
# echo "GHE_HOST=${GITHUB_SERVER_URL##https:\/\/}" >> $GITHUB_ENV
253+
254+
# Drafts your next Release notes as Pull Requests are merged into "master"
255+
- uses: release-drafter/release-drafter@v5
256+
# (Optional) specify config name to use, relative to .github/. Default: release-drafter.yml
257+
# with:
258+
# config-name: my-config.yml
259+
# disable-autolabeler: true
260+
env:
261+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

README.md

Lines changed: 135 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,133 +1,157 @@
11
# Cortex Monorepo
22

3-
This monorepo contains two projects: CortexJS and CortexCPP.
4-
5-
## CortexJS: Stateful Business Backend
6-
7-
* All of the stateful endpoints:
8-
+ /threads
9-
+ /messages
10-
+ /models
11-
+ /runs
12-
+ /vector_store
13-
+ /settings
14-
+ /?auth
15-
+ …
16-
* Database & Filesystem
17-
* API Gateway
18-
* Authentication & Authorization
19-
* Observability
20-
21-
## CortexCPP: Stateless Embedding Backend
22-
23-
* All of the high performance, stateless endpoints:
24-
+ /chat/completion
25-
+ /audio
26-
+ /fine_tuning
27-
+ /embeddings
28-
+ /load_model
29-
+ /unload_model
30-
* Kernel - Hardware Recognition
31-
32-
## Project Structure
33-
34-
```
35-
.
36-
├── cortex-js/
37-
│ ├── package.json
38-
│ ├── README.md
39-
│ ├── Dockerfile
40-
│ ├── docker-compose.yml
41-
│ ├── src/
42-
│ │ ├── controllers/
43-
│ │ ├── modules/
44-
│ │ ├── services/
45-
│ │ └── ...
46-
│ └── ...
47-
├── cortex-cpp/
48-
│ ├── app/
49-
│ │ ├── controllers/
50-
│ │ ├── models/
51-
│ │ ├── services/
52-
│ │ ├── ?engines/
53-
│ │ │ ├── llama.cpp
54-
│ │ │ ├── tensorrt-llm
55-
│ │ │ └── ...
56-
│ │ └── ...
57-
│ ├── CMakeLists.txt
58-
│ ├── config.json
59-
│ ├── Dockerfile
60-
│ ├── docker-compose.yml
61-
│ ├── README.md
62-
│ └── ...
63-
├── scripts/
64-
│ └── ...
65-
├── README.md
66-
├── package.json
67-
├── Dockerfile
68-
├── docker-compose.yml
69-
└── docs/
70-
└── ...
71-
```
72-
73-
## Installation
74-
75-
### NPM Install
76-
77-
* Pre-install script:
3+
# Installation
4+
5+
## Prerequisites
6+
7+
### **Dependencies**
8+
9+
Before installation, ensure that you have installed the following:
10+
11+
- **Node.js**: Required for running the installation.
12+
- **NPM**: Needed to manage packages.
13+
- **CPU Instruction Sets**: Available for download from the [Cortex GitHub Releases](https://github.com/janhq/cortex/releases) page.
14+
15+
<aside>
16+
💡 The **CPU instruction sets** are not required for the initial installation of Cortex. This dependency will be automatically installed during the Cortex initialization if they are not already on your system.
17+
18+
</aside>
19+
20+
### **Hardware**
21+
22+
Ensure that your system meets the following requirements to run Cortex:
23+
24+
- **OS**:
25+
- MacOSX 13.6 or higher.
26+
- Windows 10 or higher.
27+
- Ubuntu 12.04 and later.
28+
- **RAM (CPU Mode):**
29+
- 8GB for running up to 3B models.
30+
- 16GB for running up to 7B models.
31+
- 32GB for running up to 13B models.
32+
- **VRAM (GPU Mode):**
33+
34+
- 6GB can load the 3B model (int4) with `ngl` at 120 ~ full speed on CPU/ GPU.
35+
- 8GB can load the 7B model (int4) with `ngl` at 120 ~ full speed on CPU/ GPU.
36+
- 12GB can load the 13B model (int4) with `ngl` at 120 ~ full speed on CPU/ GPU.
37+
38+
- **Disk**: At least 10GB for app and model download.
39+
40+
## Cortex Installation
41+
42+
To install Cortex, follow the steps below:
43+
44+
### Step 1: Install Cortex
45+
46+
Run the following command to install Cortex globally on your machine:
47+
48+
```bash
49+
# Install using NPM globally
50+
npm i -g @janhq/cortex
51+
```
52+
53+
### Step 2: Verify the Installation
54+
55+
After installation, you can verify that Cortex is installed correctly by getting help information.
56+
57+
```bash
58+
# Get the help information
59+
cortex -h
60+
```
61+
62+
### Step 3: Initialize Cortex
63+
64+
Once verified, you need to initialize the Cortex engine.
65+
66+
1. Initialize the Cortex engine:
67+
68+
```
69+
cortex init
70+
```
71+
72+
1. Select between `CPU` and `GPU` modes.
73+
7874
```bash
79-
npm pre-install script; platform specific (MacOS / Windows / Linux)
75+
? Select run mode (Use arrow keys)
76+
> CPU
77+
GPU
8078
```
81-
* Tag based:
82-
```json
83-
npm install @janhq/cortex
84-
npm install @janhq/cortex#cuda
85-
npm install @janhq/cortex#cuda-avx512
86-
npm install @janhq/cortex#cuda-avx
79+
80+
2. Select between GPU types.
81+
82+
```bash
83+
? Select GPU types (Use arrow keys)
84+
> Nvidia
85+
Others (Vulkan)
8786
```
8887

89-
### CLI Install Script
88+
3. Select CPU instructions (will be deprecated soon).
9089

9190
```bash
92-
cortex init (AVX2 + Cuda)
91+
? Select CPU instructions (Use arrow keys)
92+
> AVX2
93+
AVX
94+
AVX-512
95+
```
9396

94-
Enable GPU Acceleration?
95-
1. Nvidia (default) - detected
96-
2. AMD
97-
3. Mac Metal
97+
1. Cortex will download the required CPU instruction sets if you choose `CPU` mode. If you choose `GPU` mode, Cortex will download the necessary dependencies to use your GPU.
98+
2. Once downloaded, Cortex is ready to use!
9899

99-
Enter your choice:
100+
### Step 4: Pull a model
100101

101-
CPU Instructions
102-
1. AVX2 (default) - Recommend based on what the user has
103-
2. AVX (old CPU)
104-
3. AVX512
102+
From HuggingFace
105103

106-
Enter your choice:
104+
```bash
105+
cortex pull janhq/phi-3-medium-128k-instruct-GGUF
106+
```
107107

108-
Downloading cortex-cuda-avx.so........................25%
108+
From Jan Hub (TBD)
109109

110-
Cortex is ready!
110+
```bash
111+
cortex pull llama3
112+
```
111113

112-
It seems like you have installed models from other applications. Do you want to import them?
113-
1. Import from /Users/HOME/jan/models
114-
2. Import from /Users/HOME/lmstudio/models
115-
3. Import everything
114+
### Step 5: Chat
116115

117-
Importing from /Users/HOME/jan/models..................17%
116+
```bash
117+
cortex run janhq/phi-3-medium-128k-instruct-GGUF
118118
```
119119

120-
## Backend (jan app)
120+
## Run as an API server
121121

122-
```json
123-
POST /settings
124-
{
125-
"gpu_enabled": true,
126-
"gpu_family": "Nvidia",
127-
"cpu_instructions": "AVX2"
128-
}
122+
```bash
123+
cortex serve
129124
```
130125

131-
## Client Library Configuration
126+
## Build from Source
132127

133-
TBD
128+
To install Cortex from the source, follow the steps below:
129+
130+
1. Clone the Cortex repository [here](https://github.com/janhq/cortex/tree/dev).
131+
2. Navigate to the `cortex-js` folder.
132+
3. Open the terminal and run the following command to build the Cortex project:
133+
134+
```bash
135+
npx nest build
136+
```
137+
138+
1. Make the `command.js` executable:
139+
140+
```bash
141+
chmod +x '[path-to]/cortex/cortex-js/dist/src/command.js'
142+
```
143+
144+
1. Link the package globally:
145+
146+
```bash
147+
npm link
148+
```
149+
150+
## Uninstall Cortex
151+
152+
Run the following command to uninstall Cortex globally on your machine:
153+
154+
```
155+
# Uninstall globally using NPM
156+
npm uninstall -g @janhq/cortex
157+
```

cortex-cpp/common/base.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ class BaseModel {
1616
virtual void ModelStatus(
1717
const HttpRequestPtr& req,
1818
std::function<void(const HttpResponsePtr&)>&& callback) = 0;
19+
20+
virtual void GetModels(const HttpRequestPtr& req,
21+
std::function<void(const HttpResponsePtr&)>&& callback) = 0;
1922
};
2023

2124
class BaseChatCompletion {

cortex-cpp/controllers/server.cc

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,40 @@ void server::ModelStatus(
129129
LOG_TRACE << "Done get model status";
130130
}
131131

132+
void server::GetModels(const HttpRequestPtr& req,
133+
std::function<void(const HttpResponsePtr&)>&& callback) {
134+
if (!IsEngineLoaded()) {
135+
Json::Value res;
136+
res["message"] = "Engine is not loaded yet";
137+
auto resp = cortex_utils::nitroHttpJsonResponse(res);
138+
resp->setStatusCode(k409Conflict);
139+
callback(resp);
140+
LOG_WARN << "Engine is not loaded yet";
141+
return;
142+
}
143+
144+
LOG_TRACE << "Start to get models";
145+
if (engine_->IsSupported("GetModels")) {
146+
engine_->GetModels(
147+
req->getJsonObject(),
148+
[cb = std::move(callback)](Json::Value status, Json::Value res) {
149+
auto resp = cortex_utils::nitroHttpJsonResponse(res);
150+
resp->setStatusCode(static_cast<drogon::HttpStatusCode>(
151+
status["status_code"].asInt()));
152+
cb(resp);
153+
});
154+
} else {
155+
Json::Value res;
156+
res["message"] = "Method is not supported yet";
157+
auto resp = cortex_utils::nitroHttpJsonResponse(res);
158+
resp->setStatusCode(k500InternalServerError);
159+
callback(resp);
160+
LOG_WARN << "Method is not supported yet";
161+
}
162+
163+
LOG_TRACE << "Done get models";
164+
}
165+
132166
void server::LoadModel(const HttpRequestPtr& req,
133167
std::function<void(const HttpResponsePtr&)>&& callback) {
134168
auto engine_type =
@@ -144,10 +178,9 @@ void server::LoadModel(const HttpRequestPtr& req,
144178
};
145179

146180
try {
147-
std::string abs_path = cortex_utils::GetCurrentPath() +
148-
get_engine_path(cur_engine_name_);
149-
dylib_ =
150-
std::make_unique<cortex_cpp::dylib>(abs_path, "engine");
181+
std::string abs_path =
182+
cortex_utils::GetCurrentPath() + get_engine_path(cur_engine_name_);
183+
dylib_ = std::make_unique<cortex_cpp::dylib>(abs_path, "engine");
151184
} catch (const cortex_cpp::dylib::load_error& e) {
152185
LOG_ERROR << "Could not load engine: " << e.what();
153186
dylib_.reset();

0 commit comments

Comments
 (0)