model.ts 1.3 KB

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