Skip to content

Commit

Permalink
feat: new package model
Browse files Browse the repository at this point in the history
  • Loading branch information
kwaa committed Dec 6, 2024
1 parent 6f53b3e commit c1a4aff
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 0 deletions.
37 changes: 37 additions & 0 deletions packages/model/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "@xsai/model",
"version": "0.0.15",
"type": "module",
"author": "Moeru AI",
"license": "MIT",
"homepage": "https://xsai.js.org",
"repository": {
"type": "git",
"url": "git+https://github.com/moeru-ai/xsai.git",
"directory": "packages/model"
},
"bugs": "https://github.com/moeru-ai/xsai/issues",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
},
"files": [
"dist"
],
"scripts": {
"build": "pkgroll",
"build:watch": "pkgroll --watch",
"test": "vitest run",
"test:watch": "vitest"
},
"dependencies": {
"@xsai/shared": "workspace:"
},
"devDependencies": {
"@xsai/providers": "workspace:"
}
}
2 changes: 2 additions & 0 deletions packages/model/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export type * from './types'
export * from './utils'
1 change: 1 addition & 0 deletions packages/model/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type { Model } from './model'
6 changes: 6 additions & 0 deletions packages/model/src/types/model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface Model {
created: number
id: string
object: 'model'
owned_by: string
}
2 changes: 2 additions & 0 deletions packages/model/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './list-models'
export * from './retrieve-model'
21 changes: 21 additions & 0 deletions packages/model/src/utils/list-models.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { type CommonRequestOptions, requestHeaders } from '@xsai/shared'

import type { Model } from '../types/model'

export interface ListModelsOptions extends Omit<CommonRequestOptions, 'model'> {}

export interface ListModelsResponse {
data: Model[]
object: 'list'
}

export const listModels = async (options: ListModelsOptions): Promise<Model[]> =>
await fetch(options.url, {
headers: requestHeaders({
'Content-Type': 'application/json',
...options.headers,
}, options.apiKey),
signal: options.abortSignal,
})
.then(res => res.json() as Promise<ListModelsResponse>)
.then(({ data }) => data)
15 changes: 15 additions & 0 deletions packages/model/src/utils/retrieve-model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { type CommonRequestOptions, requestHeaders } from '@xsai/shared'

import type { Model } from '../types/model'

export interface RetrieveModelOptions extends Omit<CommonRequestOptions, 'model'> {}

export const retrieveModel = async (options: RetrieveModelOptions): Promise<Model> =>
await fetch(options.url, {
headers: requestHeaders({
'Content-Type': 'application/json',
...options.headers,
}, options.apiKey),
signal: options.abortSignal,
})
.then(res => res.json() as Promise<Model>)
27 changes: 27 additions & 0 deletions packages/model/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { describe, expect, it } from 'vitest'

import { listModels, retrieveModel } from '../src'

describe('@xsai/model', () => {
it('listModels', async () => {
const models = await listModels({
url: new URL('models', 'http://localhost:11434/v1/'),
})

const llama = models.find(({ id }) => id === 'llama3.2:latest')!

expect(llama.id).toBe('llama3.2:latest')
expect(llama.object).toBe('model')
expect(llama.owned_by).toBe('library')
})

it('retrieveModel', async () => {
const llama = await retrieveModel({
url: new URL('models/llama3.2:latest', 'http://localhost:11434/v1/'),
})

expect(llama.id).toBe('llama3.2:latest')
expect(llama.object).toBe('model')
expect(llama.owned_by).toBe('library')
})
})
4 changes: 4 additions & 0 deletions packages/model/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "@importantimport/tsconfig/app.json",
"include": ["src"]
}
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c1a4aff

Please sign in to comment.