model.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import { DEFAULT_MODELS } from "../constant";
  2. import { LLMModel } from "../client/api";
  3. const CustomSeq = {
  4. val: -1000, //To ensure the custom model located at front, start from -1000, refer to constant.ts
  5. cache: new Map<string, number>(),
  6. next: (id: string) => {
  7. if (CustomSeq.cache.has(id)) {
  8. return CustomSeq.cache.get(id) as number;
  9. } else {
  10. let seq = CustomSeq.val++;
  11. CustomSeq.cache.set(id, seq);
  12. return seq;
  13. }
  14. },
  15. };
  16. const customProvider = (providerName: string) => ({
  17. id: providerName.toLowerCase(),
  18. providerName: providerName,
  19. providerType: "custom",
  20. sorted: CustomSeq.next(providerName),
  21. });
  22. /**
  23. * Sorts an array of models based on specified rules.
  24. *
  25. * First, sorted by provider; if the same, sorted by model
  26. */
  27. const sortModelTable = (models: ReturnType<typeof collectModels>) =>
  28. models.sort((a, b) => {
  29. if (a.provider && b.provider) {
  30. let cmp = a.provider.sorted - b.provider.sorted;
  31. return cmp === 0 ? a.sorted - b.sorted : cmp;
  32. } else {
  33. return a.sorted - b.sorted;
  34. }
  35. });
  36. export function collectModelTable(
  37. models: readonly LLMModel[],
  38. customModels: string,
  39. ) {
  40. const modelTable: Record<
  41. string,
  42. {
  43. available: boolean;
  44. name: string;
  45. displayName: string;
  46. sorted: number;
  47. provider?: LLMModel["provider"]; // Marked as optional
  48. isDefault?: boolean;
  49. }
  50. > = {};
  51. // default models
  52. models.forEach((m) => {
  53. // using <modelName>@<providerId> as fullName
  54. modelTable[`${m.name}@${m?.provider?.id}`] = {
  55. ...m,
  56. displayName: m.name, // 'provider' is copied over if it exists
  57. };
  58. });
  59. // server custom models
  60. customModels
  61. .split(",")
  62. .filter((v) => !!v && v.length > 0)
  63. .forEach((m) => {
  64. const available = !m.startsWith("-");
  65. const nameConfig =
  66. m.startsWith("+") || m.startsWith("-") ? m.slice(1) : m;
  67. let [name, displayName] = nameConfig.split("=");
  68. // enable or disable all models
  69. if (name === "all") {
  70. Object.values(modelTable).forEach(
  71. (model) => (model.available = available),
  72. );
  73. } else {
  74. // 1. find model by name, and set available value
  75. const [customModelName, customProviderName] = name.split(/@(?!.*@)/);
  76. let count = 0;
  77. for (const fullName in modelTable) {
  78. const [modelName, providerName] = fullName.split(/@(?!.*@)/);
  79. if (
  80. customModelName == modelName &&
  81. (customProviderName === undefined ||
  82. customProviderName === providerName)
  83. ) {
  84. count += 1;
  85. modelTable[fullName]["available"] = available;
  86. // swap name and displayName for bytedance
  87. if (providerName === "bytedance") {
  88. [name, displayName] = [displayName, modelName];
  89. modelTable[fullName]["name"] = name;
  90. }
  91. if (displayName) {
  92. modelTable[fullName]["displayName"] = displayName;
  93. }
  94. }
  95. }
  96. // 2. if model not exists, create new model with available value
  97. if (count === 0) {
  98. let [customModelName, customProviderName] = name.split(/@(?!.*@)/);
  99. const provider = customProvider(
  100. customProviderName || customModelName,
  101. );
  102. // swap name and displayName for bytedance
  103. if (displayName && provider.providerName == "ByteDance") {
  104. [customModelName, displayName] = [displayName, customModelName];
  105. }
  106. modelTable[`${customModelName}@${provider?.id}`] = {
  107. name: customModelName,
  108. displayName: displayName || customModelName,
  109. available,
  110. provider, // Use optional chaining
  111. sorted: CustomSeq.next(`${customModelName}@${provider?.id}`),
  112. };
  113. }
  114. }
  115. });
  116. return modelTable;
  117. }
  118. export function collectModelTableWithDefaultModel(
  119. models: readonly LLMModel[],
  120. customModels: string,
  121. defaultModel: string,
  122. ) {
  123. let modelTable = collectModelTable(models, customModels);
  124. if (defaultModel && defaultModel !== "") {
  125. if (defaultModel.includes("@")) {
  126. if (defaultModel in modelTable) {
  127. modelTable[defaultModel].isDefault = true;
  128. }
  129. } else {
  130. for (const key of Object.keys(modelTable)) {
  131. if (
  132. modelTable[key].available &&
  133. key.split(/@(?!.*@)/).shift() == defaultModel
  134. ) {
  135. modelTable[key].isDefault = true;
  136. break;
  137. }
  138. }
  139. }
  140. }
  141. return modelTable;
  142. }
  143. /**
  144. * Generate full model table.
  145. */
  146. export function collectModels(
  147. models: readonly LLMModel[],
  148. customModels: string,
  149. ) {
  150. const modelTable = collectModelTable(models, customModels);
  151. let allModels = Object.values(modelTable);
  152. allModels = sortModelTable(allModels);
  153. return allModels;
  154. }
  155. export function collectModelsWithDefaultModel(
  156. models: readonly LLMModel[],
  157. customModels: string,
  158. defaultModel: string,
  159. ) {
  160. const modelTable = collectModelTableWithDefaultModel(
  161. models,
  162. customModels,
  163. defaultModel,
  164. );
  165. let allModels = Object.values(modelTable);
  166. allModels = sortModelTable(allModels);
  167. return allModels;
  168. }
  169. export function isModelAvailableInServer(
  170. customModels: string,
  171. modelName: string,
  172. providerName: string,
  173. ) {
  174. const fullName = `${modelName}@${providerName}`;
  175. const modelTable = collectModelTable(DEFAULT_MODELS, customModels);
  176. return modelTable[fullName]?.available === false;
  177. }