Merge pull request #5180 from frostime/contrib-modellist

 feat: 调整模型列表,将自定义模型放在前面显示
This commit is contained in:
Dogtiti 2024-08-05 20:49:07 +08:00 committed by GitHub
commit a6b7432358
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 69 additions and 4 deletions

View File

@ -64,12 +64,14 @@ export interface LLMModel {
displayName?: string; displayName?: string;
available: boolean; available: boolean;
provider: LLMModelProvider; provider: LLMModelProvider;
sorted: number;
} }
export interface LLMModelProvider { export interface LLMModelProvider {
id: string; id: string;
providerName: string; providerName: string;
providerType: string; providerType: string;
sorted: number;
} }
export abstract class LLMApi { export abstract class LLMApi {

View File

@ -411,13 +411,17 @@ export class ChatGPTApi implements LLMApi {
return []; return [];
} }
//由于目前 OpenAI 的 disableListModels 默认为 true所以当前实际不会运行到这场
let seq = 1000; //同 Constant.ts 中的排序保持一致
return chatModels.map((m) => ({ return chatModels.map((m) => ({
name: m.id, name: m.id,
available: true, available: true,
sorted: seq++,
provider: { provider: {
id: "openai", id: "openai",
providerName: "OpenAI", providerName: "OpenAI",
providerType: "openai", providerType: "openai",
sorted: 1,
}, },
})); }));
} }

View File

@ -320,86 +320,105 @@ const tencentModels = [
const moonshotModes = ["moonshot-v1-8k", "moonshot-v1-32k", "moonshot-v1-128k"]; const moonshotModes = ["moonshot-v1-8k", "moonshot-v1-32k", "moonshot-v1-128k"];
let seq = 1000; // 内置的模型序号生成器从1000开始
export const DEFAULT_MODELS = [ export const DEFAULT_MODELS = [
...openaiModels.map((name) => ({ ...openaiModels.map((name) => ({
name, name,
available: true, available: true,
sorted: seq++, // Global sequence sort(index)
provider: { provider: {
id: "openai", id: "openai",
providerName: "OpenAI", providerName: "OpenAI",
providerType: "openai", providerType: "openai",
sorted: 1, // 这里是固定的,确保顺序与之前内置的版本一致
}, },
})), })),
...openaiModels.map((name) => ({ ...openaiModels.map((name) => ({
name, name,
available: true, available: true,
sorted: seq++,
provider: { provider: {
id: "azure", id: "azure",
providerName: "Azure", providerName: "Azure",
providerType: "azure", providerType: "azure",
sorted: 2,
}, },
})), })),
...googleModels.map((name) => ({ ...googleModels.map((name) => ({
name, name,
available: true, available: true,
sorted: seq++,
provider: { provider: {
id: "google", id: "google",
providerName: "Google", providerName: "Google",
providerType: "google", providerType: "google",
sorted: 3,
}, },
})), })),
...anthropicModels.map((name) => ({ ...anthropicModels.map((name) => ({
name, name,
available: true, available: true,
sorted: seq++,
provider: { provider: {
id: "anthropic", id: "anthropic",
providerName: "Anthropic", providerName: "Anthropic",
providerType: "anthropic", providerType: "anthropic",
sorted: 4,
}, },
})), })),
...baiduModels.map((name) => ({ ...baiduModels.map((name) => ({
name, name,
available: true, available: true,
sorted: seq++,
provider: { provider: {
id: "baidu", id: "baidu",
providerName: "Baidu", providerName: "Baidu",
providerType: "baidu", providerType: "baidu",
sorted: 5,
}, },
})), })),
...bytedanceModels.map((name) => ({ ...bytedanceModels.map((name) => ({
name, name,
available: true, available: true,
sorted: seq++,
provider: { provider: {
id: "bytedance", id: "bytedance",
providerName: "ByteDance", providerName: "ByteDance",
providerType: "bytedance", providerType: "bytedance",
sorted: 6,
}, },
})), })),
...alibabaModes.map((name) => ({ ...alibabaModes.map((name) => ({
name, name,
available: true, available: true,
sorted: seq++,
provider: { provider: {
id: "alibaba", id: "alibaba",
providerName: "Alibaba", providerName: "Alibaba",
providerType: "alibaba", providerType: "alibaba",
sorted: 7,
}, },
})), })),
...tencentModels.map((name) => ({ ...tencentModels.map((name) => ({
name, name,
available: true, available: true,
sorted: seq++,
provider: { provider: {
id: "tencent", id: "tencent",
providerName: "Tencent", providerName: "Tencent",
providerType: "tencent", providerType: "tencent",
sorted: 8,
}, },
})), })),
...moonshotModes.map((name) => ({ ...moonshotModes.map((name) => ({
name, name,
available: true, available: true,
sorted: seq++,
provider: { provider: {
id: "moonshot", id: "moonshot",
providerName: "Moonshot", providerName: "Moonshot",
providerType: "moonshot", providerType: "moonshot",
sorted: 9,
}, },
})), })),
] as const; ] as const;

View File

@ -1,12 +1,42 @@
import { DEFAULT_MODELS } from "../constant"; import { DEFAULT_MODELS } from "../constant";
import { LLMModel } from "../client/api"; import { LLMModel } from "../client/api";
const CustomSeq = {
val: -1000, //To ensure the custom model located at front, start from -1000, refer to constant.ts
cache: new Map<string, number>(),
next: (id: string) => {
if (CustomSeq.cache.has(id)) {
return CustomSeq.cache.get(id) as number;
} else {
let seq = CustomSeq.val++;
CustomSeq.cache.set(id, seq);
return seq;
}
},
};
const customProvider = (providerName: string) => ({ const customProvider = (providerName: string) => ({
id: providerName.toLowerCase(), id: providerName.toLowerCase(),
providerName: providerName, providerName: providerName,
providerType: "custom", providerType: "custom",
sorted: CustomSeq.next(providerName),
}); });
/**
* Sorts an array of models based on specified rules.
*
* First, sorted by provider; if the same, sorted by model
*/
const sortModelTable = (models: ReturnType<typeof collectModels>) =>
models.sort((a, b) => {
if (a.provider && b.provider) {
let cmp = a.provider.sorted - b.provider.sorted;
return cmp === 0 ? a.sorted - b.sorted : cmp;
} else {
return a.sorted - b.sorted;
}
});
export function collectModelTable( export function collectModelTable(
models: readonly LLMModel[], models: readonly LLMModel[],
customModels: string, customModels: string,
@ -17,6 +47,7 @@ export function collectModelTable(
available: boolean; available: boolean;
name: string; name: string;
displayName: string; displayName: string;
sorted: number;
provider?: LLMModel["provider"]; // Marked as optional provider?: LLMModel["provider"]; // Marked as optional
isDefault?: boolean; isDefault?: boolean;
} }
@ -84,6 +115,7 @@ export function collectModelTable(
displayName: displayName || customModelName, displayName: displayName || customModelName,
available, available,
provider, // Use optional chaining provider, // Use optional chaining
sorted: CustomSeq.next(`${customModelName}@${provider?.id}`),
}; };
} }
} }
@ -99,13 +131,16 @@ export function collectModelTableWithDefaultModel(
) { ) {
let modelTable = collectModelTable(models, customModels); let modelTable = collectModelTable(models, customModels);
if (defaultModel && defaultModel !== "") { if (defaultModel && defaultModel !== "") {
if (defaultModel.includes('@')) { if (defaultModel.includes("@")) {
if (defaultModel in modelTable) { if (defaultModel in modelTable) {
modelTable[defaultModel].isDefault = true; modelTable[defaultModel].isDefault = true;
} }
} else { } else {
for (const key of Object.keys(modelTable)) { for (const key of Object.keys(modelTable)) {
if (modelTable[key].available && key.split('@').shift() == defaultModel) { if (
modelTable[key].available &&
key.split("@").shift() == defaultModel
) {
modelTable[key].isDefault = true; modelTable[key].isDefault = true;
break; break;
} }
@ -123,7 +158,9 @@ export function collectModels(
customModels: string, customModels: string,
) { ) {
const modelTable = collectModelTable(models, customModels); const modelTable = collectModelTable(models, customModels);
const allModels = Object.values(modelTable); let allModels = Object.values(modelTable);
allModels = sortModelTable(allModels);
return allModels; return allModels;
} }
@ -138,7 +175,10 @@ export function collectModelsWithDefaultModel(
customModels, customModels,
defaultModel, defaultModel,
); );
const allModels = Object.values(modelTable); let allModels = Object.values(modelTable);
allModels = sortModelTable(allModels);
return allModels; return allModels;
} }