model.ts 3.3 KB

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