model.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { LLMModel } from "../client/api";
  2. export function collectModelTable(
  3. models: readonly LLMModel[],
  4. customModels: string,
  5. ) {
  6. const modelTable: { [key: string]: LLMModel } = {};
  7. // default models
  8. models.forEach(
  9. (m) =>
  10. (modelTable[m.name] = {
  11. ...m,
  12. displayName: m.name,
  13. }),
  14. );
  15. // server custom models
  16. customModels
  17. .split(",")
  18. .filter((v) => !!v && v.length > 0)
  19. .map((m) => {
  20. const available = !m.startsWith("-");
  21. const nameConfig =
  22. m.startsWith("+") || m.startsWith("-") ? m.slice(1) : m;
  23. const [name, displayName] = nameConfig.split("=");
  24. // enable or disable all models
  25. if (name === "all") {
  26. Object.values(modelTable).forEach((m) => (m.available = available));
  27. }
  28. modelTable[name] = {
  29. name,
  30. displayName: displayName || name,
  31. available,
  32. provider: modelTable[name].provider,
  33. };
  34. });
  35. return modelTable;
  36. }
  37. /**
  38. * Generate full model table.
  39. */
  40. export function collectModels(
  41. models: readonly LLMModel[],
  42. customModels: string,
  43. ) {
  44. const modelTable = collectModelTable(models, customModels);
  45. const allModels = Object.values(modelTable);
  46. return allModels;
  47. }