all repos — nixfiles @ 4b8a6dbe53b4635a332ea98f9b97ac82bc473fa1

System and user configuration, managed by nix and home-manager

user/modules/llama-swap.nix (view raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
{
  config,
  lib,
  pkgs,
  ...
}:
let
  cfg = config.services.llama-swap;
  settingsFormat = pkgs.formats.yaml { };
  configFile =
    if cfg.configFile != null then
      cfg.configFile
    else
      settingsFormat.generate "config.yaml" cfg.settings;

  args = [
    "--listen=${cfg.listenAddress}:${toString cfg.port}"
    "--config=${configFile}"
  ]
  ++ cfg.extraArgs;
in
{
  options.services.llama-swap = {
    enable = lib.mkEnableOption "the llama-swap service";

    package = lib.mkPackageOption pkgs "llama-swap" { };

    packages = lib.mkOption {
      default = lib.optionals (cfg.configFile != null) [ pkgs.llama-cpp ];
      defaultText = lib.literalExpression ''
        lib.optionals (cfg.configFile != null) [ pkgs.llama-cpp ]
      '';
      example = lib.literalExpression ''
        [
          pkgs.llama-cpp-rocm
          pkgs.vllm
        ]
      '';
      type = lib.types.listOf lib.types.package;
      description = ''
        Packages to add to llama-swap process's PATH.
        Useful if using `configFile` instead of `settings`.
      '';
    };

    exposePackages = lib.mkOption {
      default = cfg.configFile != null;
      defaultText = lib.literalExpression "cfg.configFile != null";
      type = lib.types.bool;
      description = ''
        Whether to additionally add `packages` to user profile.
        Useful when using `configFile` instead of `settings`.
      '';
    };

    listenAddress = lib.mkOption {
      type = lib.types.str;
      default = "localhost";
      example = "0.0.0.0";
      description = ''
        Address that llama-swap listens on.
      '';
    };

    port = lib.mkOption {
      default = 8080;
      example = 11343;
      type = lib.types.port;
      description = ''
        Port that llama-swap listens on.
      '';
    };

    configFile = lib.mkOption {
      type = with lib.types; nullOr (either path str);
      default = null;
      description = ''
        Optional configuration file path to use instead of
        generating from settings
      '';
    };

    extraArgs = lib.mkOption {
      type = with lib.types; listOf str;
      default = [ ];
      description = ''
        Additional arguments to pass on llama-swap startup
      '';
    };

    settings = lib.mkOption {
      type = lib.types.submodule { freeformType = settingsFormat.type; };
      description = ''
        llama-swap configuration. Refer to the [llama-swap example configuration](https://github.com/mostlygeek/llama-swap/blob/main/config.example.yaml)
        for details on supported values.
      '';
      example = lib.literalExpression ''
        let
          llama-cpp = pkgs.llama-cpp.override { rocmSupport = true; };
          llama-server = lib.getExe' llama-cpp "llama-server";
        in
        {
          healthCheckTimeout = 60;
          models = {
            "some-model" = {
              cmd = "''${llama-server} --port ''${PORT} -m /var/lib/llama-cpp/models/some-model.gguf -ngl 0 --no-webui";
              aliases = [
                "the-best"
              ];
            };
            "other-model" = {
              proxy = "http://127.0.0.1:5555";
              cmd = "''${llama-server} --port 5555 -m /var/lib/llama-cpp/models/other-model.gguf -ngl 0 -c 4096 -np 4 --no-webui";
              concurrencyLimit = 4;
            };
          };
        };
      '';
    };
  };
  config = lib.mkIf cfg.enable {
    assertions = [
      {
        assertion = cfg.configFile != null -> (builtins.attrNames cfg.settings) == [ ];
        message = "configFile is incompatible with non-empty settings";
      }
    ];

    # untested
    systemd.user.services.llama-swap = {
      description = "Model swapping for LLaMA C++ Server (or any local OpenAPI compatible server)";

      serviceConfig = {
        Type = "exec";
        ExecStart = "${lib.getExe cfg.package} ${lib.escapeShellArgs args}";
        Restart = "on-failure";
        RestartSec = 3;

        # for GPU acceleration
        PrivateDevices = false;
      };
      path = cfg.packages;
    };

    launchd.agents.llama-swap = {
      enable = true;
      config = {
        ProgramArguments = [ (lib.getExe cfg.package) ] ++ args;
        KeepAlive = {
          Crashed = true;
          SuccessfulExit = false;
        };
        ProcessType = "Background";
        EnvironmentVariables = {
          PATH = lib.makeBinPath cfg.packages;
        };
      };
    };

    home.packages = [
      cfg.package
    ]
    ++ (lib.optionals cfg.exposePackages cfg.packages);
  };
}