185 lines
5.0 KiB
TypeScript
185 lines
5.0 KiB
TypeScript
import { DEFAULT_MODELS } from "../constant";
|
|
import { LLMModel } from "../client/api";
|
|
|
|
const customProvider = (providerName: string) => ({
|
|
id: providerName.toLowerCase(),
|
|
providerName: providerName,
|
|
providerType: "custom",
|
|
});
|
|
|
|
const sortModelTable = (
|
|
models: ReturnType<typeof collectModels>,
|
|
rule: "custom-first" | "default-first",
|
|
) =>
|
|
models.sort((a, b) => {
|
|
if (a.provider === undefined && b.provider === undefined) {
|
|
return 0;
|
|
}
|
|
|
|
let aIsCustom = a.provider?.providerType === "custom";
|
|
let bIsCustom = b.provider?.providerType === "custom";
|
|
|
|
if (aIsCustom === bIsCustom) {
|
|
return 0;
|
|
}
|
|
|
|
if (aIsCustom) {
|
|
return rule === "custom-first" ? -1 : 1;
|
|
} else {
|
|
return rule === "custom-first" ? 1 : -1;
|
|
}
|
|
});
|
|
|
|
export function collectModelTable(
|
|
models: readonly LLMModel[],
|
|
customModels: string,
|
|
) {
|
|
const modelTable: Record<
|
|
string,
|
|
{
|
|
available: boolean;
|
|
name: string;
|
|
displayName: string;
|
|
provider?: LLMModel["provider"]; // Marked as optional
|
|
isDefault?: boolean;
|
|
}
|
|
> = {};
|
|
|
|
// default models
|
|
models.forEach((m) => {
|
|
// using <modelName>@<providerId> as fullName
|
|
modelTable[`${m.name}@${m?.provider?.id}`] = {
|
|
...m,
|
|
displayName: m.name, // 'provider' is copied over if it exists
|
|
};
|
|
});
|
|
|
|
// server custom models
|
|
customModels
|
|
.split(",")
|
|
.filter((v) => !!v && v.length > 0)
|
|
.forEach((m) => {
|
|
const available = !m.startsWith("-");
|
|
const nameConfig =
|
|
m.startsWith("+") || m.startsWith("-") ? m.slice(1) : m;
|
|
let [name, displayName] = nameConfig.split("=");
|
|
|
|
// enable or disable all models
|
|
if (name === "all") {
|
|
Object.values(modelTable).forEach(
|
|
(model) => (model.available = available),
|
|
);
|
|
} else {
|
|
// 1. find model by name, and set available value
|
|
const [customModelName, customProviderName] = name.split("@");
|
|
let count = 0;
|
|
for (const fullName in modelTable) {
|
|
const [modelName, providerName] = fullName.split("@");
|
|
if (
|
|
customModelName == modelName &&
|
|
(customProviderName === undefined ||
|
|
customProviderName === providerName)
|
|
) {
|
|
count += 1;
|
|
modelTable[fullName]["available"] = available;
|
|
// swap name and displayName for bytedance
|
|
if (providerName === "bytedance") {
|
|
[name, displayName] = [displayName, modelName];
|
|
modelTable[fullName]["name"] = name;
|
|
}
|
|
if (displayName) {
|
|
modelTable[fullName]["displayName"] = displayName;
|
|
}
|
|
}
|
|
}
|
|
// 2. if model not exists, create new model with available value
|
|
if (count === 0) {
|
|
let [customModelName, customProviderName] = name.split("@");
|
|
const provider = customProvider(
|
|
customProviderName || customModelName,
|
|
);
|
|
// swap name and displayName for bytedance
|
|
if (displayName && provider.providerName == "ByteDance") {
|
|
[customModelName, displayName] = [displayName, customModelName];
|
|
}
|
|
modelTable[`${customModelName}@${provider?.id}`] = {
|
|
name: customModelName,
|
|
displayName: displayName || customModelName,
|
|
available,
|
|
provider, // Use optional chaining
|
|
};
|
|
}
|
|
}
|
|
});
|
|
|
|
return modelTable;
|
|
}
|
|
|
|
export function collectModelTableWithDefaultModel(
|
|
models: readonly LLMModel[],
|
|
customModels: string,
|
|
defaultModel: string,
|
|
) {
|
|
let modelTable = collectModelTable(models, customModels);
|
|
if (defaultModel && defaultModel !== "") {
|
|
if (defaultModel.includes("@")) {
|
|
if (defaultModel in modelTable) {
|
|
modelTable[defaultModel].isDefault = true;
|
|
}
|
|
} else {
|
|
for (const key of Object.keys(modelTable)) {
|
|
if (
|
|
modelTable[key].available &&
|
|
key.split("@").shift() == defaultModel
|
|
) {
|
|
modelTable[key].isDefault = true;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return modelTable;
|
|
}
|
|
|
|
/**
|
|
* Generate full model table.
|
|
*/
|
|
export function collectModels(
|
|
models: readonly LLMModel[],
|
|
customModels: string,
|
|
) {
|
|
const modelTable = collectModelTable(models, customModels);
|
|
let allModels = Object.values(modelTable);
|
|
|
|
allModels = sortModelTable(allModels, "custom-first");
|
|
|
|
return allModels;
|
|
}
|
|
|
|
export function collectModelsWithDefaultModel(
|
|
models: readonly LLMModel[],
|
|
customModels: string,
|
|
defaultModel: string,
|
|
) {
|
|
const modelTable = collectModelTableWithDefaultModel(
|
|
models,
|
|
customModels,
|
|
defaultModel,
|
|
);
|
|
let allModels = Object.values(modelTable);
|
|
|
|
allModels = sortModelTable(allModels, "custom-first");
|
|
|
|
return allModels;
|
|
}
|
|
|
|
export function isModelAvailableInServer(
|
|
customModels: string,
|
|
modelName: string,
|
|
providerName: string,
|
|
) {
|
|
const fullName = `${modelName}@${providerName}`;
|
|
const modelTable = collectModelTable(DEFAULT_MODELS, customModels);
|
|
return modelTable[fullName]?.available === false;
|
|
}
|