Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ feat: 调整模型列表,将自定义模型放在前面显示 #5180

Merged
merged 4 commits into from
Aug 5, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 35 additions & 4 deletions app/utils/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,29 @@ const customProvider = (providerName: string) => ({
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里排序,else的情况下,是否可以按providerType本身的字符顺序 + model_name的字符顺序排序?

这样的化,不是自定义模型的时候,其实也会有一个固定的顺序,而不会因为Object.values导致顺序不稳定。有的时候这个在前面,有的时候哪个在前面。

Copy link
Contributor Author

@frostime frostime Aug 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的,我回去后弄一下👌

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

启用 -all 后能否直接按照自定义模型字符串的顺序来

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

启用 -all 后能否直接按照自定义模型字符串的顺序来

你说的涉及到多个排序显示方案了。这个需要改动设置界面,增加更多选项。

这个 PR 里是写死了只有 custom first 的排序。虽然为了逻辑的完整,在 sort 函数里面定义了一个参数 rule ,但是实际上调用的时候只有 custom-first 一种方案会被使用。

后面也许会再提交别的 PR ,看看能否支持更多的排序方案。

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

另外还有一点:
如果aIsCustom=ture以及aIsCustom也为true的时候,是不是也应该按字母顺序排序?

Copy link
Contributor Author

@frostime frostime Aug 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

或许应该换一种思路:

  1. 引入一个新的sorted字段进行排序
  2. constant.ts中对内置的provider配置排序字段
// constant
let seq = 1000;  // 内置的模型序号生成器从1000开始
export const DEFAULT_MODELS = [
  ...openaiModels.map((name) => ({
    name,
    available: true,
    sorted: seq++,  // 这里使用一个全局的序号生成器
    provider: {
      id: "openai",
      providerName: "OpenAI",
      providerType: "openai",
      sorted: 1  // 这里是固定的,确保顺序与之前内置的版本一致
    },
  })),
  // ......
] as const;
  1. utils/model.ts中,针对新生成的模型,使用一个新的序号生成器(从-1000开始)确保排序在前面
let customSeq = -1000 // 这个是自定义的模型以及自定义的customProvider使用同一个序号生成器,从-1000开始自增

// 下面是确保自定义的model以及provider的sorted是负数,排序会在前面
const customProvider = (providerName: string) => ({
  id: providerName.toLowerCase(),
  providerName: providerName,
  providerType: "custom",
  sorted: customSeq++,
});

        modelTable[`${customModelName}@${provider?.id}`] = {
            name: customModelName,
            displayName: displayName || customModelName,
            available,
            provider, // Use optional chaining
            sorted: customSeq++
          };
  1. 更新sortModelTable函数,分别使用对应的sorted字段顺序排序即可

好的,commit 150fc 实现了你说的这个方案。

另外你提到的 customSeq 有点问题,由于 model.ts 会反复执行,所以这个变量会一直增长。出于鲁棒性的考量,我给改成了基于 map 的方案。其他的实现和你说的应该都是一致的。

排序的时候,首先比较 provider,然后比较 model。

Copy link
Contributor

@lloydzhou lloydzhou Aug 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

customSeq变量如果在module这一层。应该就不会无限制增长。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

啊呀,workflow 检查没过;加了一个 sorted 字段之后别的地方还得改,麻烦了😢。

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你的方案应该是正确的,这里会持续运行,确实会一直增长

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

提交之前,可以本地使用 yarn run build 或者 yarn run export检查一下。

}
});

export function collectModelTable(
models: readonly LLMModel[],
frostime marked this conversation as resolved.
Show resolved Hide resolved
customModels: string,
Expand Down Expand Up @@ -99,13 +122,16 @@ export function collectModelTableWithDefaultModel(
) {
let modelTable = collectModelTable(models, customModels);
if (defaultModel && defaultModel !== "") {
if (defaultModel.includes('@')) {
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) {
if (
modelTable[key].available &&
key.split("@").shift() == defaultModel
) {
modelTable[key].isDefault = true;
break;
}
Expand All @@ -123,7 +149,9 @@ export function collectModels(
customModels: string,
) {
const modelTable = collectModelTable(models, customModels);
const allModels = Object.values(modelTable);
let allModels = Object.values(modelTable);

allModels = sortModelTable(allModels, "custom-first");

return allModels;
}
Expand All @@ -138,7 +166,10 @@ export function collectModelsWithDefaultModel(
customModels,
defaultModel,
);
const allModels = Object.values(modelTable);
let allModels = Object.values(modelTable);

allModels = sortModelTable(allModels, "custom-first");

return allModels;
}

Expand Down
Loading