model.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. name,
  28. displayName: m.name, // 'provider' is copied over if it exists
  29. };
  30. });
  31. // server custom models
  32. customModels
  33. .split(",")
  34. .filter((v) => !!v && v.length > 0)
  35. .forEach((m) => {
  36. const available = !m.startsWith("-");
  37. const nameConfig =
  38. m.startsWith("+") || m.startsWith("-") ? m.slice(1) : m;
  39. const [name, displayName] = nameConfig.split("=");
  40. // enable or disable all models
  41. if (name === "all") {
  42. Object.values(modelTable).forEach(
  43. (model) => (model.available = available),
  44. );
  45. } else {
  46. // 1. find model by name(), and set available value
  47. let count = 0;
  48. for (const fullName in modelTable) {
  49. if (fullName.split("@").shift() == name) {
  50. count += 1;
  51. modelTable[fullName]["available"] = available;
  52. if (displayName) {
  53. modelTable[fullName]["displayName"] = displayName;
  54. }
  55. }
  56. }
  57. // 2. if model not exists, create new model with available value
  58. if (count === 0) {
  59. const provider = customProvider(name);
  60. modelTable[`${name}@${provider?.id}`] = {
  61. name,
  62. displayName: displayName || name,
  63. available,
  64. provider, // Use optional chaining
  65. };
  66. }
  67. }
  68. });
  69. return modelTable;
  70. }
  71. export function collectModelTableWithDefaultModel(
  72. models: readonly LLMModel[],
  73. customModels: string,
  74. defaultModel: string,
  75. ) {
  76. let modelTable = collectModelTable(models, customModels);
  77. if (defaultModel && defaultModel !== "") {
  78. modelTable[defaultModel] = {
  79. ...modelTable[defaultModel],
  80. name: defaultModel,
  81. available: true,
  82. isDefault: true,
  83. };
  84. }
  85. return modelTable;
  86. }
  87. /**
  88. * Generate full model table.
  89. */
  90. export function collectModels(
  91. models: readonly LLMModel[],
  92. customModels: string,
  93. ) {
  94. const modelTable = collectModelTable(models, customModels);
  95. const allModels = Object.values(modelTable);
  96. return allModels;
  97. }
  98. export function collectModelsWithDefaultModel(
  99. models: readonly LLMModel[],
  100. customModels: string,
  101. defaultModel: string,
  102. ) {
  103. const modelTable = collectModelTableWithDefaultModel(
  104. models,
  105. customModels,
  106. defaultModel,
  107. );
  108. const allModels = Object.values(modelTable);
  109. return allModels;
  110. }
  111. export function isModelAvailableInServer(
  112. customModels: string,
  113. modelName: string,
  114. providerName: string,
  115. ) {
  116. const fullName = `${modelName}@${providerName}`;
  117. const modelTable = collectModelTable(DEFAULT_MODELS, customModels);
  118. return modelTable[fullName]?.available === false;
  119. }