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

Commit d19f451

Browse files
committed
update model
Signed-off-by: James <namnh0122@gmail.com>
1 parent 67d98fb commit d19f451

File tree

6 files changed

+82
-91
lines changed

6 files changed

+82
-91
lines changed

cortex-js/src/domain/models/model.interface.ts

Lines changed: 55 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
/**
2-
* Model type defines the shape of a model object.
3-
* @stored
4-
*/
5-
export interface Model {
1+
import { Model as OpenAiModel } from 'openai/resources/models';
2+
3+
export interface Model
4+
extends OpenAiModel,
5+
ModelSettingParams,
6+
ModelRuntimeParams {
67
/**
78
* Model identifier.
89
*/
@@ -22,58 +23,22 @@ export interface Model {
2223
* The model download source. It can be an external url or a local filepath.
2324
*/
2425
files: string[] | ModelArtifact;
26+
}
2527

26-
/**
27-
* GGUF metadata: tokenizer.chat_template
28-
*/
29-
prompt_template?: string;
30-
31-
/**
32-
* Defines specific tokens or phrases at which the model will stop generating further output.
33-
*/
34-
stop?: string[];
35-
36-
/// Inferencing
37-
/**
38-
* Set probability threshold for more relevant outputs.
39-
*/
40-
top_p?: number;
41-
42-
/**
43-
* Controls the randomness of the model’s output.
44-
*/
45-
temperature?: number;
46-
47-
/**
48-
* Adjusts the likelihood of the model repeating words or phrases in its output.
49-
*/
50-
frequency_penalty?: number;
51-
52-
/**
53-
* Influences the generation of new and varied concepts in the model’s output.
54-
*/
55-
presence_penalty?: number;
56-
57-
/// Engines
28+
/**
29+
* The available model settings.
30+
*/
31+
export interface ModelSettingParams {
5832
/**
5933
* The context length for model operations varies; the maximum depends on the specific model used.
6034
*/
6135
ctx_len?: number;
6236

63-
/**
64-
* Enable real-time data processing for faster predictions.
65-
*/
66-
stream?: boolean;
67-
68-
/*
69-
* The maximum number of tokens the model will generate in a single response.
70-
*/
71-
max_tokens?: number;
72-
7337
/**
7438
* The number of layers to load onto the GPU for acceleration.
7539
*/
7640
ngl?: number;
41+
embedding?: boolean;
7742

7843
/**
7944
* Number of parallel sequences to decode
@@ -85,6 +50,22 @@ export interface Model {
8550
*/
8651
cpu_threads?: number;
8752

53+
/**
54+
* GGUF metadata: tokenizer.chat_template
55+
*/
56+
prompt_template?: string;
57+
system_prompt?: string;
58+
ai_prompt?: string;
59+
user_prompt?: string;
60+
llama_model_path?: string;
61+
mmproj?: string;
62+
cont_batching?: boolean;
63+
64+
/**
65+
* The model engine.
66+
*/
67+
engine?: string;
68+
8869
/**
8970
* The prompt to use for internal configuration
9071
*/
@@ -134,59 +115,48 @@ export interface Model {
134115
* To enable mmap, default is true
135116
*/
136117
use_mmap?: boolean;
137-
138-
/**
139-
* The model engine.
140-
*/
141-
engine?: string;
142-
}
143-
144-
/**
145-
* The available model settings.
146-
*/
147-
export interface ModelSettingParams {
148-
ctx_len?: number;
149-
ngl?: number;
150-
embedding?: boolean;
151-
n_parallel?: number;
152-
cpu_threads?: number;
153-
prompt_template?: string;
154-
system_prompt?: string;
155-
ai_prompt?: string;
156-
user_prompt?: string;
157-
llama_model_path?: string;
158-
mmproj?: string;
159-
cont_batching?: boolean;
160-
engine?: string;
161-
stop?: string[];
162-
pre_prompt?: string;
163-
n_batch?: number;
164-
caching_enabled?: boolean;
165-
grp_attn_n?: number;
166-
grp_attn_w?: number;
167-
mlock?: boolean;
168-
grammar_file?: string;
169-
model_type?: string;
170-
model_alias?: string;
171-
flash_attn?: boolean;
172-
cache_type?: string;
173-
use_mmap?: boolean;
174118
}
175119

176120
/**
177121
* The available model runtime parameters.
178122
*/
179123
export interface ModelRuntimeParams {
124+
/**
125+
* Controls the randomness of the model’s output.
126+
*/
180127
temperature?: number;
181128
token_limit?: number;
182129
top_k?: number;
130+
131+
/**
132+
* Set probability threshold for more relevant outputs.
133+
*/
183134
top_p?: number;
135+
136+
/**
137+
* Enable real-time data processing for faster predictions.
138+
*/
184139
stream?: boolean;
140+
141+
/*
142+
* The maximum number of tokens the model will generate in a single response.
143+
*/
185144
max_tokens?: number;
145+
146+
/**
147+
* Defines specific tokens or phrases at which the model will stop generating further output.
148+
*/
186149
stop?: string[];
150+
151+
/**
152+
* Adjusts the likelihood of the model repeating words or phrases in its output.
153+
*/
187154
frequency_penalty?: number;
155+
156+
/**
157+
* Influences the generation of new and varied concepts in the model’s output.
158+
*/
188159
presence_penalty?: number;
189-
engine?: string;
190160
}
191161

192162
/**

cortex-js/src/infrastructure/controllers/models.controller.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,7 @@ export class ModelsController {
163163
})
164164
@Get()
165165
findAll() {
166-
return this.modelsUsecases
167-
.findAll()
168-
.then((data) => data.map((e) => ({ id: e.model, ...e })));
166+
return this.modelsUsecases.findAll();
169167
}
170168

171169
@HttpCode(200)

cortex-js/src/infrastructure/dtos/models/create-model.dto.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,11 @@ export class CreateModelDto implements Partial<Model> {
136136
@IsOptional()
137137
@IsString()
138138
engine?: string;
139+
140+
@ApiProperty({
141+
description: 'The owner of the model.',
142+
example: '',
143+
default: '',
144+
})
145+
owned_by?: string;
139146
}

cortex-js/src/usecases/assistants/assistants.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { Module } from '@nestjs/common';
22
import { AssistantsUsecases } from './assistants.usecases';
33
import { DatabaseModule } from '@/infrastructure/database/database.module';
4+
import { ModelRepositoryModule } from '@/infrastructure/repositories/models/model.module';
45

56
@Module({
6-
imports: [DatabaseModule],
7+
imports: [DatabaseModule, ModelRepositoryModule],
78
controllers: [],
89
providers: [AssistantsUsecases],
910
exports: [AssistantsUsecases],

cortex-js/src/usecases/assistants/assistants.usecases.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,26 @@ import { Repository } from 'typeorm';
44
import { CreateAssistantDto } from '@/infrastructure/dtos/assistants/create-assistant.dto';
55
import { Assistant } from '@/domain/models/assistant.interface';
66
import { PageDto } from '@/infrastructure/dtos/page.dto';
7+
import { ModelRepository } from '@/domain/repositories/model.interface';
8+
import { ModelNotFoundException } from '@/infrastructure/exception/model-not-found.exception';
79

810
@Injectable()
911
export class AssistantsUsecases {
1012
constructor(
1113
@Inject('ASSISTANT_REPOSITORY')
12-
private assistantRepository: Repository<AssistantEntity>,
14+
private readonly assistantRepository: Repository<AssistantEntity>,
15+
private readonly modelRepository: ModelRepository,
1316
) {}
1417

1518
async create(createAssistantDto: CreateAssistantDto) {
16-
const { top_p, temperature } = createAssistantDto;
19+
const { top_p, temperature, model } = createAssistantDto;
20+
if (model !== '*') {
21+
const modelEntity = await this.modelRepository.findOne(model);
22+
if (!modelEntity) {
23+
throw new ModelNotFoundException(model);
24+
}
25+
}
26+
1727
const assistant: AssistantEntity = {
1828
...createAssistantDto,
1929
object: 'assistant',

cortex-js/src/usecases/models/models.usecases.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,13 @@ export class ModelsUsecases {
6060
* @param createModelDto Model data
6161
*/
6262
async create(createModelDto: CreateModelDto) {
63+
const { model: modelId, owned_by } = createModelDto;
6364
const model: Model = {
6465
...createModelDto,
66+
id: modelId,
67+
created: Date.now(),
68+
object: 'model',
69+
owned_by: owned_by ?? '',
6570
};
6671

6772
await this.modelRepository.create(model);

0 commit comments

Comments
 (0)