@xsai/model 0.5.1 → 1.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1 +1,23 @@
1
- https://xsai.js.org/docs/packages/model
1
+ # @xsai/model
2
+
3
+ <!-- automd:file src="/docs/snippets/model.md" -->
4
+
5
+ ```sh
6
+ pnpm add @xsai/model
7
+ ```
8
+
9
+ ```ts
10
+ import { listModels, models } from '@xsai/model'
11
+
12
+ const catalog = models({
13
+ apiKey: process.env.OPENAI_API_KEY,
14
+ baseURL: 'https://api.openai.com/v1/',
15
+ })
16
+
17
+ const entries = await listModels(catalog)
18
+ console.log(entries.map(entry => entry.id))
19
+ ```
20
+
21
+ <!-- /automd -->
22
+
23
+ Read the [documentation](https://xsai.js.org/model).
package/dist/index.d.ts CHANGED
@@ -1,21 +1,39 @@
1
- import { CommonRequestOptions } from '@xsai/shared';
1
+ import { Promisable, HttpOptions } from '@xsai/shared';
2
2
 
3
- interface Model {
4
- created: number;
3
+ interface ModelCatalog {
4
+ list: (options?: ModelCatalogOptions) => Promisable<ModelCatalogEntry[]>;
5
+ retrieve: (options: ModelCatalogOptions & {
6
+ id: string;
7
+ }) => Promisable<ModelCatalogEntry>;
8
+ }
9
+ interface ModelCatalogEntry {
5
10
  id: string;
6
- object: 'model';
7
- owned_by: string;
11
+ providerMetadata?: ModelCatalogEntryProviderMetadata;
8
12
  }
9
-
10
- interface ListModelsOptions extends Omit<CommonRequestOptions, 'model'> {
13
+ interface ModelCatalogEntryProviderMetadata {
11
14
  }
12
- interface ListModelsResponse {
13
- data: Model[];
14
- object: 'list';
15
+ interface ModelCatalogOptions {
16
+ providerOptions?: ModelCatalogProviderOptions;
17
+ signal?: AbortSignal;
15
18
  }
16
- declare const listModels: (options: ListModelsOptions) => Promise<Model[]>;
19
+ interface ModelCatalogProviderOptions {
20
+ }
21
+
22
+ declare const listModels: (catalog: ModelCatalog, options?: ModelCatalogOptions) => Promise<ModelCatalogEntry[]>;
17
23
 
18
- declare const retrieveModel: (options: CommonRequestOptions) => Promise<Model>;
24
+ declare module '@xsai/model' {
25
+ interface ModelCatalogEntryProviderMetadata {
26
+ models?: {
27
+ created?: number;
28
+ ownedBy?: string;
29
+ };
30
+ }
31
+ }
32
+ declare const models: (options: Omit<HttpOptions, "model">) => ModelCatalog;
33
+
34
+ declare const retrieveModel: (catalog: ModelCatalog, options: ModelCatalogOptions & {
35
+ id: string;
36
+ }) => Promise<ModelCatalogEntry>;
19
37
 
20
- export { listModels, retrieveModel };
21
- export type { ListModelsOptions, ListModelsResponse, Model };
38
+ export { listModels, models, retrieveModel };
39
+ export type { ModelCatalog, ModelCatalogEntry, ModelCatalogEntryProviderMetadata, ModelCatalogOptions, ModelCatalogProviderOptions };
package/dist/index.js CHANGED
@@ -1,13 +1,23 @@
1
- import { requestURL, requestHeaders, responseCatch, responseJSON } from '@xsai/shared';
1
+ import { sendRequest } from '@xsai/shared';
2
2
 
3
- const listModels = async (options) => (options.fetch ?? globalThis.fetch)(requestURL("models", options.baseURL), {
4
- headers: requestHeaders(options.headers, options.apiKey),
5
- signal: options.abortSignal
6
- }).then(responseCatch).then(responseJSON).then(({ data }) => data);
3
+ const listModels = async (catalog, options = {}) => catalog.list(options);
7
4
 
8
- const retrieveModel = async (options) => (options.fetch ?? globalThis.fetch)(requestURL(`models/${options.model}`, options.baseURL), {
9
- headers: requestHeaders(options.headers, options.apiKey),
10
- signal: options.abortSignal
11
- }).then(responseCatch).then(responseJSON);
5
+ const modelEntry = (model) => ({
6
+ id: model.id,
7
+ providerMetadata: { models: { created: model.created, ownedBy: model.owned_by } }
8
+ });
9
+ const models = (options) => ({
10
+ list: async (catalogOptions = {}) => {
11
+ const response = await sendRequest({ method: "GET", path: "models", signal: catalogOptions.signal }, options);
12
+ const json = await response.json();
13
+ return json.data.map(modelEntry);
14
+ },
15
+ retrieve: async (catalogOptions) => {
16
+ const response = await sendRequest({ method: "GET", path: `models/${encodeURIComponent(catalogOptions.id)}`, signal: catalogOptions.signal }, options);
17
+ return modelEntry(await response.json());
18
+ }
19
+ });
12
20
 
13
- export { listModels, retrieveModel };
21
+ const retrieveModel = async (catalog, options) => catalog.retrieve(options);
22
+
23
+ export { listModels, models, retrieveModel };
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@xsai/model",
3
3
  "type": "module",
4
- "version": "0.5.1",
5
- "description": "extra-small AI SDK.",
4
+ "version": "1.0.0-beta.1",
5
+ "description": "extra-small AI SDK: list and retrieve models.",
6
6
  "author": "Moeru AI",
7
7
  "license": "MIT",
8
8
  "homepage": "https://xsai.js.org",
@@ -14,8 +14,10 @@
14
14
  "bugs": "https://github.com/moeru-ai/xsai/issues",
15
15
  "keywords": [
16
16
  "xsai",
17
+ "ai",
17
18
  "openai",
18
- "ai"
19
+ "model",
20
+ "models"
19
21
  ],
20
22
  "sideEffects": false,
21
23
  "exports": {
@@ -29,11 +31,11 @@
29
31
  "dist"
30
32
  ],
31
33
  "dependencies": {
32
- "@xsai/shared": "0.5.1"
34
+ "@xsai/shared": "1.0.0-beta.1"
33
35
  },
34
36
  "scripts": {
35
- "build": "pkgroll",
36
- "test": "vitest run",
37
+ "build": "pkgroll --clean-dist",
38
+ "test": "vitest run --exclude e2e/**",
37
39
  "test:watch": "vitest"
38
40
  },
39
41
  "main": "./dist/index.js",