From c022972cf81540d5de880d3e462097174458acca Mon Sep 17 00:00:00 2001 From: Henry Heng Date: Wed, 15 May 2024 19:39:05 +0100 Subject: [PATCH] Bugfix/Local Models Json (#2416) fallback to fetch local models json file --- packages/components/src/modelLoader.ts | 33 ++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/packages/components/src/modelLoader.ts b/packages/components/src/modelLoader.ts index e697045dd..dca4cdbf9 100644 --- a/packages/components/src/modelLoader.ts +++ b/packages/components/src/modelLoader.ts @@ -1,5 +1,7 @@ import { INodeOptionsValue } from './Interface' import axios from 'axios' +import * as fs from 'fs' +import * as path from 'path' const MASTER_MODEL_LIST = 'https://raw.githubusercontent.com/FlowiseAI/Flowise/main/packages/components/models.json' @@ -9,15 +11,38 @@ export enum MODEL_TYPE { EMBEDDING = 'embedding' } +const getModelsJSONPath = (): string => { + const checkModelsPaths = [path.join(__dirname, '..', 'models.json'), path.join(__dirname, '..', '..', 'models.json')] + for (const checkPath of checkModelsPaths) { + if (fs.existsSync(checkPath)) { + return checkPath + } + } + return '' +} + const getModelConfig = async (category: MODEL_TYPE, name: string) => { const modelFile = process.env.MODEL_LIST_CONFIG_JSON || MASTER_MODEL_LIST if (!modelFile) { throw new Error('MODEL_LIST_CONFIG_JSON not set') } - const resp = await axios.get(modelFile) - const models = resp.data - const categoryModels = models[category] - return categoryModels.find((model: any) => model.name === name) + try { + const resp = await axios.get(modelFile) + if (resp.status === 200 && resp.data) { + const models = resp.data + const categoryModels = models[category] + return categoryModels.find((model: INodeOptionsValue) => model.name === 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 categoryModels.find((model: INodeOptionsValue) => model.name === name) + } + return {} + } } export const getModels = async (category: MODEL_TYPE, name: string) => {