model.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { DEFAULT_MODELS } from "../constant";
  2. import { LLMModel } from "../client/api";
  3. const customProvider = (modelName: string) => ({
  4. id: modelName,
  5. providerName: "Custom",
  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. let count = 0;
  47. for (const fullName in modelTable) {
  48. const [modelName, providerName] = fullName.split("@");
  49. if (modelName === name) {
  50. count += 1;
  51. modelTable[fullName]["available"] = available;
  52. // swap name and displayName for bytedance
  53. if (providerName === "bytedance") {
  54. const tempName = name;
  55. name = displayName;
  56. displayName = tempName;
  57. modelTable[fullName]["name"] = name;
  58. }
  59. if (displayName) {
  60. modelTable[fullName]["displayName"] = displayName;
  61. }
  62. }
  63. }
  64. // 2. if model not exists, create new model with available value
  65. if (count === 0) {
  66. const provider = customProvider(name);
  67. modelTable[`${name}@${provider?.id}`] = {
  68. name,
  69. displayName: displayName || name,
  70. available,
  71. provider, // Use optional chaining
  72. };
  73. }
  74. }
  75. });
  76. return modelTable;
  77. }
  78. export function collectModelTableWithDefaultModel(
  79. models: readonly LLMModel[],
  80. customModels: string,
  81. defaultModel: string,
  82. ) {
  83. let modelTable = collectModelTable(models, customModels);
  84. if (defaultModel && defaultModel !== "") {
  85. modelTable[defaultModel] = {
  86. ...modelTable[defaultModel],
  87. name: defaultModel,
  88. available: true,
  89. isDefault: true,
  90. };
  91. }
  92. return modelTable;
  93. }
  94. /**
  95. * Generate full model table.
  96. */
  97. export function collectModels(
  98. models: readonly LLMModel[],
  99. customModels: string,
  100. ) {
  101. const modelTable = collectModelTable(models, customModels);
  102. const allModels = Object.values(modelTable);
  103. return allModels;
  104. }
  105. export function collectModelsWithDefaultModel(
  106. models: readonly LLMModel[],
  107. customModels: string,
  108. defaultModel: string,
  109. ) {
  110. const modelTable = collectModelTableWithDefaultModel(
  111. models,
  112. customModels,
  113. defaultModel,
  114. );
  115. const allModels = Object.values(modelTable);
  116. return allModels;
  117. }
  118. export function isModelAvailableInServer(
  119. customModels: string,
  120. modelName: string,
  121. providerName: string,
  122. ) {
  123. const fullName = `${modelName}@${providerName}`;
  124. const modelTable = collectModelTable(DEFAULT_MODELS, customModels);
  125. return modelTable[fullName]?.available === false;
  126. }