model.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import { DEFAULT_MODELS } from "../constant";
  2. import { LLMModel } from "../client/api";
  3. const customProvider = (providerName: string) => ({
  4. id: providerName.toLowerCase(),
  5. providerName: providerName,
  6. providerType: "custom",
  7. });
  8. export function collectModelTable(
  9. models: readonly LLMModel[],
  10. customModels: string,
  11. ) {
  12. const modelTable: Record<
  13. string,
  14. {
  15. available: boolean;
  16. name: string;
  17. displayName: string;
  18. provider?: LLMModel["provider"]; // Marked as optional
  19. isDefault?: boolean;
  20. }
  21. > = {};
  22. // default models
  23. models.forEach((m) => {
  24. // using <modelName>@<providerId> as fullName
  25. modelTable[`${m.name}@${m?.provider?.id}`] = {
  26. ...m,
  27. displayName: m.name, // 'provider' is copied over if it exists
  28. };
  29. });
  30. // server custom models
  31. customModels
  32. .split(",")
  33. .filter((v) => !!v && v.length > 0)
  34. .forEach((m) => {
  35. const available = !m.startsWith("-");
  36. const nameConfig =
  37. m.startsWith("+") || m.startsWith("-") ? m.slice(1) : m;
  38. let [name, displayName] = nameConfig.split("=");
  39. // enable or disable all models
  40. if (name === "all") {
  41. Object.values(modelTable).forEach(
  42. (model) => (model.available = available),
  43. );
  44. } else {
  45. // 1. find model by name, and set available value
  46. const [customModelName, customProviderName] = name.split("@");
  47. let count = 0;
  48. for (const fullName in modelTable) {
  49. const [modelName, providerName] = fullName.split("@");
  50. if (
  51. customModelName == modelName &&
  52. (customProviderName === undefined ||
  53. customProviderName === providerName)
  54. ) {
  55. count += 1;
  56. modelTable[fullName]["available"] = available;
  57. // swap name and displayName for bytedance
  58. if (providerName === "bytedance") {
  59. [name, displayName] = [displayName, modelName];
  60. modelTable[fullName]["name"] = name;
  61. }
  62. if (displayName) {
  63. modelTable[fullName]["displayName"] = displayName;
  64. }
  65. }
  66. }
  67. // 2. if model not exists, create new model with available value
  68. if (count === 0) {
  69. let [customModelName, customProviderName] = name.split("@");
  70. const provider = customProvider(
  71. customProviderName || customModelName,
  72. );
  73. // swap name and displayName for bytedance
  74. if (displayName && provider.providerName == "ByteDance") {
  75. [customModelName, displayName] = [displayName, customModelName];
  76. }
  77. modelTable[`${customModelName}@${provider?.id}`] = {
  78. name: customModelName,
  79. displayName: displayName || customModelName,
  80. available,
  81. provider, // Use optional chaining
  82. };
  83. }
  84. }
  85. });
  86. return modelTable;
  87. }
  88. export function collectModelTableWithDefaultModel(
  89. models: readonly LLMModel[],
  90. customModels: string,
  91. defaultModel: string,
  92. ) {
  93. let modelTable = collectModelTable(models, customModels);
  94. if (defaultModel && defaultModel !== "") {
  95. modelTable[defaultModel] = {
  96. ...modelTable[defaultModel],
  97. name: defaultModel,
  98. available: true,
  99. isDefault: true,
  100. };
  101. }
  102. return modelTable;
  103. }
  104. /**
  105. * Generate full model table.
  106. */
  107. export function collectModels(
  108. models: readonly LLMModel[],
  109. customModels: string,
  110. ) {
  111. const modelTable = collectModelTable(models, customModels);
  112. const allModels = Object.values(modelTable);
  113. return allModels;
  114. }
  115. export function collectModelsWithDefaultModel(
  116. models: readonly LLMModel[],
  117. customModels: string,
  118. defaultModel: string,
  119. ) {
  120. const modelTable = collectModelTableWithDefaultModel(
  121. models,
  122. customModels,
  123. defaultModel,
  124. );
  125. const allModels = Object.values(modelTable);
  126. return allModels;
  127. }
  128. export function isModelAvailableInServer(
  129. customModels: string,
  130. modelName: string,
  131. providerName: string,
  132. ) {
  133. const fullName = `${modelName}@${providerName}`;
  134. const modelTable = collectModelTable(DEFAULT_MODELS, customModels);
  135. return modelTable[fullName]?.available === false;
  136. }