Refactor the modelLoader and fix a small bug (#4838)

Refactor the modelLoader to remove duplicate code.

Also fix an issue where if MODEL_LIST_CONFIG_JSON is set, but does not exist, fallback did not work
This commit is contained in:
Karl Stoney 2025-07-18 11:25:27 +01:00 committed by GitHub
parent 0ac01d3cbb
commit cf965f3d8e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 31 additions and 80 deletions

View File

@ -1,9 +1,7 @@
import { INodeOptionsValue } from './Interface'
import axios from 'axios' import axios from 'axios'
import * as fs from 'fs' import * as fs from 'fs'
import * as path from 'path' import * as path from 'path'
import { INodeOptionsValue } from './Interface'
const MASTER_MODEL_LIST = 'https://raw.githubusercontent.com/FlowiseAI/Flowise/main/packages/components/models.json'
export enum MODEL_TYPE { export enum MODEL_TYPE {
CHAT = 'chat', CHAT = 'chat',
@ -31,96 +29,49 @@ const isValidUrl = (urlString: string) => {
return url.protocol === 'http:' || url.protocol === 'https:' return url.protocol === 'http:' || url.protocol === 'https:'
} }
const getModelConfig = async (category: MODEL_TYPE, name: string) => { /**
const modelFile = process.env.MODEL_LIST_CONFIG_JSON || MASTER_MODEL_LIST * Load the raw model file from either a URL or a local file
* If any of the loading fails, fallback to the default models.json file on disk
if (!modelFile) { */
throw new Error('MODEL_LIST_CONFIG_JSON not set') const getRawModelFile = async () => {
} const modelFile =
if (isValidUrl(modelFile)) { process.env.MODEL_LIST_CONFIG_JSON ?? 'https://raw.githubusercontent.com/FlowiseAI/Flowise/main/packages/components/models.json'
try { try {
if (isValidUrl(modelFile)) {
const resp = await axios.get(modelFile) const resp = await axios.get(modelFile)
if (resp.status === 200 && resp.data) { if (resp.status === 200 && resp.data) {
const models = resp.data return resp.data
const categoryModels = models[category]
return categoryModels.find((model: INodeOptionsValue) => model.name === name)
} else { } else {
throw new Error('Error fetching model list') throw new Error('Error fetching model list')
} }
} catch (e) { } else if (fs.existsSync(modelFile)) {
const models = await fs.promises.readFile(getModelsJSONPath(), 'utf8') const models = await fs.promises.readFile(modelFile, 'utf8')
if (models) { if (models) {
const categoryModels = JSON.parse(models)[category] return JSON.parse(models)
return categoryModels.find((model: INodeOptionsValue) => model.name === name)
} }
return {}
} }
} else { throw new Error('Model file does not exist or is empty')
try { } catch (e) {
if (fs.existsSync(modelFile)) { const models = await fs.promises.readFile(getModelsJSONPath(), 'utf8')
const models = await fs.promises.readFile(modelFile, 'utf8') if (models) {
if (models) { return JSON.parse(models)
const categoryModels = JSON.parse(models)[category]
return categoryModels.find((model: INodeOptionsValue) => model.name === name)
}
}
return {}
} catch (e) {
const models = await fs.promises.readFile(getModelsJSONPath(), 'utf8')
if (models) {
const categoryModels = JSON.parse(models)[category]
return categoryModels.find((model: INodeOptionsValue) => model.name === name)
}
return {}
} }
return {}
} }
} }
export const getModelConfigByModelName = async (category: MODEL_TYPE, provider: string | undefined, name: string | undefined) => { const getModelConfig = async (category: MODEL_TYPE, name: string) => {
const modelFile = process.env.MODEL_LIST_CONFIG_JSON || MASTER_MODEL_LIST const models = await getRawModelFile()
if (!modelFile) { const categoryModels = models[category]
throw new Error('MODEL_LIST_CONFIG_JSON not set') return categoryModels.find((model: INodeOptionsValue) => model.name === name)
} }
if (isValidUrl(modelFile)) {
try { export const getModelConfigByModelName = async (category: MODEL_TYPE, provider: string | undefined, name: string | undefined) => {
const resp = await axios.get(modelFile) const models = await getRawModelFile()
if (resp.status === 200 && resp.data) {
const models = resp.data const categoryModels = models[category]
const categoryModels = models[category] return getSpecificModelFromCategory(categoryModels, provider, name)
// each element of categoryModels is an object, with an array of models (models) and regions (regions)
// check if the name is in models
return getSpecificModelFromCategory(categoryModels, provider, name)
} else {
throw new Error('Error fetching model list')
}
} catch (e) {
const models = await fs.promises.readFile(getModelsJSONPath(), 'utf8')
if (models) {
const categoryModels = JSON.parse(models)[category]
return getSpecificModelFromCategory(categoryModels, provider, name)
}
return {}
}
} else {
try {
if (fs.existsSync(modelFile)) {
const models = await fs.promises.readFile(modelFile, 'utf8')
if (models) {
const categoryModels = JSON.parse(models)[category]
return getSpecificModelFromCategory(categoryModels, provider, name)
}
}
return {}
} catch (e) {
const models = await fs.promises.readFile(getModelsJSONPath(), 'utf8')
if (models) {
const categoryModels = JSON.parse(models)[category]
return getSpecificModelFromCategory(categoryModels, provider, name)
}
return {}
}
}
} }
const getSpecificModelFromCategory = (categoryModels: any, provider: string | undefined, name: string | undefined) => { const getSpecificModelFromCategory = (categoryModels: any, provider: string | undefined, name: string | undefined) => {