Skip to content

Commit

Permalink
feat: add refresh models from huggingface directly
Browse files Browse the repository at this point in the history
  • Loading branch information
snowyu committed Jul 10, 2024
1 parent 860a65f commit 57bcdfc
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 33 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@isdk/ai-tool-downloader": "workspace:*",
"@isdk/ai-tool-llm": "workspace:*",
"@isdk/ai-tool-llm-llamacpp": "workspace:*",
"@isdk/ai-tool-model": "workspace:*",
"@isdk/ai-tool-prompt": "workspace:*",
"@isdk/ai-tool-sqlite": "workspace:*",
"@isdk/detect-text-language": "^0.1.0",
Expand Down
62 changes: 35 additions & 27 deletions src/lib/brain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import path from 'path'
import {
ServerTools as ToolFunc,
} from '@isdk/ai-tool'
import { AIModelQuantType, AIModelSettings, LlmModelsFunc } from '@isdk/ai-tool-llm'
import { AIModelQuantType, AIModelSettings } from '@isdk/ai-tool-llm'
import type { LlmModelsFunc } from '@isdk/ai-tool-model'
// import { LlamaCppProviderName, llamaCpp } from '@isdk/ai-tool-llm-llamacpp'
// import { AIPromptsFunc, AIPromptsName } from '@isdk/ai-tool-prompt'
import { DownloadProgressEventName, DownloadStatusEventName, FileDownloadStatus, download } from '@isdk/ai-tool-downloader'
Expand All @@ -12,44 +13,51 @@ import EventEmitter from 'events'
EventEmitter.defaultMaxListeners = 1000

// note: need initTools() first
export function upgradeBrains() {
export async function upgradeBrains(url?: string) {
const brains = ToolFunc.get(BRAINS_FUNC_NAME) as LlmModelsFunc
brains.updateDBFromDir()
const count = await brains.$refresh({url})
return count
}

export function listBrains(userConfig: any, flags: any) {
export async function listBrains(userConfig: any, flags: any) {
const brainDir = userConfig.brainDir
const result: any[] = searchBrains(brainDir, flags)
const result = await searchBrains(brainDir, flags)
return result
}

export function searchBrains(brainDir: string, flags: any) {
export async function searchBrains(brainDir: string, flags: any) {
const brains = ToolFunc.get(BRAINS_FUNC_NAME) as LlmModelsFunc
const filter:any = flags.search ?? {}
const onlyFeatured = flags.onlyFeatured
if (!flags.all) {
if (flags.downloaded) {
filter.downloaded = {'=': true}
} else {
filter.$or = [{downloaded: {'!=': true}}, {downloaded: null}]
let result: AIModelSettings[]|undefined
if (flags.name && flags.name.startsWith('hf://')) {
const model = await brains.getModel(flags.name, flags.hubUrl)
if (model) {result = [model]}
} else {
const filter:any = flags.search ?? {}
const onlyFeatured = flags.onlyFeatured
if (!flags.all) {
if (flags.downloaded) {
filter.downloaded = {'=': true}
} else {
filter.$or = [{downloaded: {'!=': true}}, {downloaded: null}]
}
// filter.downloaded = flags.downloaded ? {'=': true} : {'!=': true}
// if (!flags.downloaded && onlyFeatured === undefined) {
// // the defaults for online is true
// onlyFeatured = true
// }

if (onlyFeatured) {
filter.featured = true
}
}
// filter.downloaded = flags.downloaded ? {'=': true} : {'!=': true}
// if (!flags.downloaded && onlyFeatured === undefined) {
// // the defaults for online is true
// onlyFeatured = true
// }

if (onlyFeatured) {
filter.featured = true

if (flags.name) {
filter._id = {'$like': `%${flags.name}%`}
}
}

if (flags.name) {
filter._id = {'$like': `%${flags.name}%`}
result = brains.$search({filter}) as AIModelSettings[]
}

const result = brains.$search({filter})
return result as AIModelSettings[]
return result
}

export function printBrains(brains: AIModelSettings[], flags?: {count?: number}) {
Expand Down
3 changes: 2 additions & 1 deletion src/lib/init-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
ResServerTools,
ServerTools,
} from '@isdk/ai-tool'
import { LlmModelsFunc, llm } from '@isdk/ai-tool-llm'
import { llm } from '@isdk/ai-tool-llm'
import {LlmModelsFunc } from '@isdk/ai-tool-model'
import { LlamaCppProviderName, llamaCpp } from '@isdk/ai-tool-llm-llamacpp'
import { AIPromptsFunc, AIPromptsName } from '@isdk/ai-tool-prompt'
import { download } from '@isdk/ai-tool-downloader'
Expand Down
2 changes: 1 addition & 1 deletion src/oclif/commands/brain/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export default class DownloadBrainCommand extends AICommand {
}
flags.onlyFeatured = false
flags.all = true
let brain: any = listBrains(userConfig, flags)
let brain: any = await listBrains(userConfig, flags)
if (!brain || brain.length === 0) {
this.log('No Such brains found')
return
Expand Down
12 changes: 10 additions & 2 deletions src/oclif/commands/brain/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ export default class Brain extends AICommand {
char: 'r',
description: 'refresh the online brains list',
}),
hubUrl: Flags.string({
char: 'u',
aliases: ['hub-url'],
description: 'the hub mirror url',
dependsOn: ['refresh'],
}),
}

static summary = '🧠 The AI Agent Brains(LLM) Manager.'
Expand Down Expand Up @@ -55,13 +61,15 @@ export default class Brain extends AICommand {
await this.config.runHook('init_tools', {id: 'brain', userConfig})

if (flags.refresh) {
upgradeBrains()
const count = await upgradeBrains(flags.hubUrl)
this.log(`${count} brains updated`)
return count
}

if (userConfig.banner && !isJson) {showBanner('Brain')}
flags.name = args.name
flags.downloaded = true
const result = listBrains(userConfig, flags)
const result = await listBrains(userConfig, flags)
if (!isJson) {
if (!result || result.length === 0) {
this.log('No brains found')
Expand Down
11 changes: 9 additions & 2 deletions src/oclif/commands/brain/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ export default class AIBrainListCommand extends AICommand {
char: 'r',
description: 'refresh the online brains list',
}),
hubUrl: Flags.string({
char: 'u',
aliases: ['hub-url'],
description: 'the hub mirror url',
dependsOn: ['refresh'],
}),
}

async run(): Promise<any> {
Expand All @@ -60,12 +66,13 @@ export default class AIBrainListCommand extends AICommand {
await this.config.runHook('init_tools', {id: 'brain', userConfig})

if (flags.refresh) {
upgradeBrains()
const count = await upgradeBrains(flags.hubUrl)
this.log(`${count} brains updated`)
}

if (userConfig.banner && !isJson) {showBanner('Brain')}
flags.name = args.name
const result = listBrains(userConfig, flags)
const result = await listBrains(userConfig, flags)
if (!isJson) {
if (!result || result.length === 0) {
this.log('No brains found')
Expand Down

0 comments on commit 57bcdfc

Please sign in to comment.