model.ts 1.2 KB

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