model.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { DEFAULT_MODELS } from "../constant";
  2. import { LLMModel } from "../client/api";
  3. const customProvider = (modelName: string) => ({
  4. id: modelName,
  5. 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. modelTable[m.name] = {
  25. ...m,
  26. displayName: m.name, // 'provider' is copied over if it exists
  27. };
  28. });
  29. // server custom models
  30. customModels
  31. .split(",")
  32. .filter((v) => !!v && v.length > 0)
  33. .forEach((m) => {
  34. const available = !m.startsWith("-");
  35. const nameConfig =
  36. m.startsWith("+") || m.startsWith("-") ? m.slice(1) : m;
  37. const [name, displayName] = nameConfig.split("=");
  38. // enable or disable all models
  39. if (name === "all") {
  40. Object.values(modelTable).forEach(
  41. (model) => (model.available = available),
  42. );
  43. } else {
  44. modelTable[name] = {
  45. name,
  46. displayName: displayName || name,
  47. available,
  48. provider: modelTable[name]?.provider ?? customProvider(name), // Use optional chaining
  49. };
  50. }
  51. });
  52. return modelTable;
  53. }
  54. export function collectModelTableWithDefaultModel(
  55. models: readonly LLMModel[],
  56. customModels: string,
  57. defaultModel: string,
  58. ) {
  59. let modelTable = collectModelTable(models, customModels);
  60. if (defaultModel && defaultModel !== "") {
  61. modelTable[defaultModel] = {
  62. ...modelTable[defaultModel],
  63. name: defaultModel,
  64. available: true,
  65. isDefault: true,
  66. };
  67. }
  68. return modelTable;
  69. }
  70. /**
  71. * Generate full model table.
  72. */
  73. export function collectModels(
  74. models: readonly LLMModel[],
  75. customModels: string,
  76. ) {
  77. const modelTable = collectModelTable(models, customModels);
  78. const allModels = Object.values(modelTable);
  79. return allModels;
  80. }
  81. export function collectModelsWithDefaultModel(
  82. models: readonly LLMModel[],
  83. customModels: string,
  84. defaultModel: string,
  85. ) {
  86. const modelTable = collectModelTableWithDefaultModel(
  87. models,
  88. customModels,
  89. defaultModel,
  90. );
  91. const allModels = Object.values(modelTable);
  92. return allModels;
  93. }
  94. export function isModelAvailableInServer(customModels, modelName) {
  95. const modelTable = collectModelTable(DEFAULT_MODELS, customModels);
  96. return modelTable[modelName ?? ""].available === false;
  97. }