Langchain-Chatchat/frontend/src/utils/parseModels.ts

38 lines
973 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { CustomModels } from '@/types/settings';
export const parseModelString = (modelString: string = '') => {
let models: CustomModels = [];
let removeAll = false;
const removedModels: string[] = [];
const modelNames = modelString.split(/[,]/).filter(Boolean);
for (const item of modelNames) {
const disable = item.startsWith('-');
const nameConfig = item.startsWith('+') || item.startsWith('-') ? item.slice(1) : item;
const [id, displayName] = nameConfig.split('=');
if (disable) {
// Disable all models.
if (id === 'all') {
removeAll = true;
}
removedModels.push(id);
continue;
}
// Remove duplicate model entries.
const existingIndex = models.findIndex(({ id: n }) => n === id);
if (existingIndex !== -1) {
models.splice(existingIndex, 1);
}
models.push({ displayName, id: id });
}
return {
add: models,
removeAll,
removed: removedModels,
};
};