Skip to content

Commit 2538d4b

Browse files
feat: add download event (#22)
* feat: add download event * fix: wrong download state entity * feat: add download event * change rest api * refactor --------- Co-authored-by: Louis <louis@jan.ai>
1 parent a5e0572 commit 2538d4b

File tree

6 files changed

+125
-4
lines changed

6 files changed

+125
-4
lines changed

src/core.ts

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ async function defaultParseResponse<T>(props: APIResponseProps): Promise<T> {
6262
}
6363

6464
const contentType = response.headers.get('content-type');
65+
6566
const isJSON =
6667
contentType?.includes('application/json') || contentType?.includes('application/vnd.api+json');
6768
if (isJSON) {

src/index.ts

+7
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ export class OpenAI extends Core.APIClient {
159159
moderations: API.Moderations = new API.Moderations(this);
160160
models: API.Models = new API.Models(this);
161161
engines: API.Engines = new API.Engines(this);
162+
events: API.Events = new API.Events(this);
162163
fineTuning: API.FineTuning = new API.FineTuning(this);
163164
beta: API.Beta = new API.Beta(this);
164165
batches: API.Batches = new API.Batches(this);
@@ -300,6 +301,12 @@ export namespace OpenAI {
300301
export import Engine = API.Engine;
301302
export import EnginesPage = API.EnginesPage;
302303

304+
export import Events = API.Events;
305+
export import DownloadStateEvent = API.DownloadStateEvent;
306+
export import DownloadStatus = API.DownloadStatus;
307+
export import DownloadType = API.DownloadType;
308+
export import DownloadItem = API.DownloadItem;
309+
303310
export import FineTuning = API.FineTuning;
304311

305312
export import Beta = API.Beta;

src/resources/engines.ts

+18-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ import * as Core from '../core';
55
import { APIResource } from '../resource';
66
import * as EnginesAPI from './engines';
77

8+
export interface EngineInitRequestParams {
9+
runMode?: 'CPU' | 'GPU';
10+
gpuType?: 'Nvidia' | 'Others (Vulkan)';
11+
instructions?: 'AVX' | 'AVX2' | 'AVX512' | undefined;
12+
cudaVersion?: '11' | '12';
13+
silent?: boolean;
14+
vulkan?: boolean;
15+
}
16+
817
export class Engines extends APIResource {
918
/**
1019
* Retrieves an engine instance, providing basic information about the engine.
@@ -24,8 +33,15 @@ export class Engines extends APIResource {
2433
* Initializes an engine instance with the given name.
2534
* It will download the engine if it is not available locally.
2635
*/
27-
init(engine: string, options?: Core.RequestOptions): Core.APIPromise<Engine> {
28-
return this._client.post(`/engines/${engine}/init`, options);
36+
init(
37+
engine: string,
38+
initOptions: EngineInitRequestParams = {},
39+
options?: Core.RequestOptions,
40+
): Core.APIPromise<Engine> {
41+
return this._client.post(`/engines/${engine}/init`, {
42+
body: initOptions,
43+
...options,
44+
});
2945
}
3046

3147
/**

src/resources/events.ts

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2+
3+
import * as Core from '../core';
4+
import { APIResource } from '../resource';
5+
import * as EventsAPI from './events';
6+
import { Stream } from '../streaming';
7+
8+
export class Events extends APIResource {
9+
/**
10+
* Get download events.
11+
*/
12+
downloadEvent(): Core.APIPromise<Stream<DownloadStateEvent>> {
13+
return this._client.get(`/events/download`, {
14+
stream: true,
15+
}) as Core.APIPromise<Stream<DownloadStateEvent>>;
16+
}
17+
}
18+
19+
export enum DownloadStatus {
20+
Pending = 'pending',
21+
Downloading = 'downloading',
22+
Error = 'error',
23+
Downloaded = 'downloaded',
24+
}
25+
26+
export enum DownloadType {
27+
Model = 'model',
28+
Miscelanous = 'miscelanous',
29+
Engine = 'engine',
30+
}
31+
32+
export interface DownloadItem {
33+
/**
34+
* Filename of the download.
35+
*/
36+
id: string;
37+
38+
time: {
39+
elapsed: number;
40+
remaining: number;
41+
};
42+
43+
size: {
44+
total: number;
45+
transferred: number;
46+
};
47+
48+
checksum?: string;
49+
50+
status: DownloadStatus;
51+
52+
error?: string;
53+
54+
metadata?: Record<string, unknown>;
55+
}
56+
57+
export interface DownloadState {
58+
/**
59+
* The id of a particular download. Being used to prevent duplication of downloads.
60+
*/
61+
id: string;
62+
63+
/**
64+
* For displaying purposes.
65+
*/
66+
title: string;
67+
68+
/**
69+
* The type of download.
70+
*/
71+
type: DownloadType;
72+
73+
/**
74+
* The status of the download.
75+
*/
76+
status: DownloadStatus;
77+
78+
/**
79+
* Explanation of the error if the download failed.
80+
*/
81+
error?: string;
82+
83+
/**
84+
* The actual downloads. [DownloadState] is just a group to supporting for download multiple files.
85+
*/
86+
children: DownloadItem[];
87+
}
88+
89+
export type DownloadStateEvent = DownloadState[];
90+
91+
export namespace Events {
92+
export import DownloadStateEvent = EventsAPI.DownloadStateEvent;
93+
export import DownloadStatus = EventsAPI.DownloadStatus;
94+
export import DownloadType = EventsAPI.DownloadType;
95+
export import DownloadItem = EventsAPI.DownloadItem;
96+
}

src/resources/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,4 @@ export { Model, ModelDeleted, ModelsPage, Models } from './models';
4545
export { Moderation, ModerationCreateResponse, ModerationCreateParams, Moderations } from './moderations';
4646

4747
export { Engine, Engines, EnginesPage } from './engines';
48+
export { DownloadStateEvent, DownloadStatus, DownloadType, DownloadItem, Events } from './events';

src/resources/models.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ export class Models extends APIResource {
4848
* Download a model.
4949
*/
5050
download(model: string) {
51-
return this._client.get(`/models/download/${model}`);
51+
return this._client.post(`/models/pull/${model}`);
5252
}
5353

5454
/**
5555
* Abort a model download.
5656
*/
5757
abortDownload(downloadId: string) {
58-
return this._client.get(`/models/abort-download/${downloadId}`);
58+
return this._client.delete(`/models/${downloadId}/pull`);
5959
}
6060

6161
/**

0 commit comments

Comments
 (0)