about summary refs log tree commit diff
path: root/nixpkgs/pkgs/by-name/lo/local-ai/package.nix
blob: a5b5b82b139bfe764a1676602ea45a0c4288599f (plain) (blame)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
{ stdenv
, lib
, fetchpatch
, fetchFromGitHub
, ncurses
, protobuf
, grpc
, openssl
  # needed for audio-to-text
, ffmpeg
, cmake
, buildGoModule
, makeWrapper
, runCommand
, testers

  # apply feature parameter names according to
  # https://github.com/NixOS/rfcs/pull/169

, with_tinydream ? false

, with_openblas ? false
, openblas
, pkg-config

, with_cublas ? false
, cudaPackages

, with_clblas ? false
, clblast
, ocl-icd
, opencl-headers

, with_stablediffusion ? false
, opencv

, with_tts ? false
, onnxruntime
, sonic
, spdlog
, fmt
}:
let
  go-llama-ggml = fetchFromGitHub {
    owner = "go-skynet";
    repo = "go-llama.cpp";
    rev = "2b57a8ae43e4699d3dc5d1496a1ccd42922993be";
    hash = "sha256-D6SEg5pPcswGyKAmF4QTJP6/Y1vjRr7m7REguag+too=";
    fetchSubmodules = true;
  };

  # possible improvement: use Nix package llama-cpp
  llama_cpp = fetchFromGitHub {
    owner = "ggerganov";
    repo = "llama.cpp";
    rev = "d01b3c4c32357567f3531d4e6ceffc5d23e87583";
    hash = "sha256-7eaQV+XTCXdrJlo7y21q5j/8ecVwuTMJScRTATcF6oM=";
    fetchSubmodules = true;
  };

  llama_cpp' = runCommand "llama_cpp_src" { } ''
    cp -r --no-preserve=mode,ownership ${llama_cpp} $out
    sed -i $out/CMakeLists.txt \
      -e 's;pkg_check_modules(DepBLAS REQUIRED openblas);pkg_check_modules(DepBLAS REQUIRED openblas64);'
  '';

  gpt4all = fetchFromGitHub {
    owner = "nomic-ai";
    repo = "gpt4all";
    rev = "27a8b020c36b0df8f8b82a252d261cda47cf44b8";
    hash = "sha256-djq1eK6ncvhkO3MNDgasDBUY/7WWcmZt/GJsHAulLdI=";
    fetchSubmodules = true;
  };

  go-piper = fetchFromGitHub {
    owner = "mudler";
    repo = "go-piper";
    rev = "9d0100873a7dbb0824dfea40e8cec70a1b110759";
    hash = "sha256-Yv9LQkWwGpYdOS0FvtP0vZ0tRyBAx27sdmziBR4U4n8=";
    fetchSubmodules = true;
  };

  go-rwkv = fetchFromGitHub {
    owner = "donomii";
    repo = "go-rwkv.cpp";
    rev = "661e7ae26d442f5cfebd2a0881b44e8c55949ec6";
    hash = "sha256-byTNZQSnt7qpBMng3ANJmpISh3GJiz+F15UqfXaz6nQ=";
    fetchSubmodules = true;
  };

  whisper = fetchFromGitHub {
    owner = "ggerganov";
    repo = "whisper.cpp";
    rev = "a56f435fd475afd7edf02bfbf9f8c77f527198c2";
    hash = "sha256-ozTnxEuftAQQr5v/kwg5EKHuKF21d9ETIyvXcvr0Qos=";
    fetchSubmodules = true;
  };

  go-bert = fetchFromGitHub {
    owner = "go-skynet";
    repo = "go-bert.cpp";
    rev = "6abe312cded14042f6b7c3cd8edf082713334a4d";
    hash = "sha256-lh9cvXc032Eq31kysxFOkRd0zPjsCznRl0tzg9P2ygo=";
    fetchSubmodules = true;
  };

  go-stable-diffusion = stdenv.mkDerivation {
    pname = "go_stable_diffusion";
    version = "unstable";
    src = fetchFromGitHub {
      owner = "mudler";
      repo = "go-stable-diffusion";
      rev = "362df9da29f882dbf09ade61972d16a1f53c3485";
      hash = "sha256-A5KvMZOviPsIpPHxM8cacT+qE2x1iFJAbPsRs4sLijY=";
      fetchSubmodules = true;
    };
    buildFlags = [ "libstablediffusion.a" ];
    dontUseCmakeConfigure = true;
    nativeBuildInputs = [ cmake ];
    buildInputs = [ opencv ];
    env.NIX_CFLAGS_COMPILE = " -isystem ${opencv}/include/opencv4";
    installPhase = ''
      install -Dt $out libstablediffusion.a Makefile go.mod *.go stablediffusion.h
    '';
  };

  go-tiny-dream = fetchFromGitHub {
    owner = "M0Rf30";
    repo = "go-tiny-dream";
    rev = "772a9c0d9aaf768290e63cca3c904fe69faf677a";
    hash = "sha256-r+wzFIjaI6cxAm/eXN3q8LRZZz+lE5EA4lCTk5+ZnIY=";
    fetchSubmodules = true;
  };

  go-tiny-dream' = runCommand "go_tiny_dream_src" { } ''
    cp -r --no-preserve=mode,ownership ${go-tiny-dream} $out
    sed -i $out/Makefile \
      -e 's;lib/libncnn;lib64/libncnn;g'
  '';

  GO_TAGS = lib.optional with_tinydream "tinydream"
    ++ lib.optional with_tts "tts"
    ++ lib.optional with_stablediffusion "stablediffusion";

  buildEnv =
    if with_cublas then
    # It's necessary to consistently use backendStdenv when building with CUDA support,
    # otherwise we get libstdc++ errors downstream.
      buildGoModule.override { stdenv = cudaPackages.backendStdenv; }
    else
      buildGoModule;

  self = buildEnv rec {
    pname = "local-ai";
    version = "2.10.1";

    src = fetchFromGitHub {
      owner = "go-skynet";
      repo = "LocalAI";
      rev = "v${version}";
      hash = "sha256-135s1Gw8mfOIx4kXlw2pYrD3ewwajUtnz3sPY/CtoLw=";
    };

    vendorHash = "sha256-UCeG0TKS+VBW8D87VmxTHS2tCAf0ADEYTJayaSiua6s=";

    # Workaround for
    # `cc1plus: error: '-Wformat-security' ignored without '-Wformat' [-Werror=format-security]`
    # when building jtreg
    env.NIX_CFLAGS_COMPILE = "-Wformat";

    postPatch =
      let
        cp = "cp -r --no-preserve=mode,ownership";
      in
      ''
        sed -i Makefile \
          -e 's;git clone.*go-llama-ggml$;${cp} ${go-llama-ggml} sources/go-llama-ggml;' \
          -e 's;git clone.*gpt4all$;${cp} ${gpt4all} sources/gpt4all;' \
          -e 's;git clone.*go-piper$;${cp} ${go-piper} sources/go-piper;' \
          -e 's;git clone.*go-rwkv$;${cp} ${go-rwkv} sources/go-rwkv;' \
          -e 's;git clone.*whisper\.cpp$;${cp} ${whisper} sources/whisper\.cpp;' \
          -e 's;git clone.*go-bert$;${cp} ${go-bert} sources/go-bert;' \
          -e 's;git clone.*diffusion$;${cp} ${if with_stablediffusion then go-stable-diffusion else go-stable-diffusion.src} sources/go-stable-diffusion;' \
          -e 's;git clone.*go-tiny-dream$;${cp} ${go-tiny-dream'} sources/go-tiny-dream;' \
          -e 's, && git checkout.*,,g' \
          -e '/mod download/ d' \

        sed -i backend/cpp/llama/Makefile \
          -e 's;git clone.*llama\.cpp$;${cp} ${llama_cpp'} llama\.cpp;' \
          -e 's, && git checkout.*,,g' \

      ''
    ;

    modBuildPhase = ''
      mkdir sources
      make prepare-sources
      go mod tidy -v
    '';

    proxyVendor = true;

    buildPhase =
      let
        buildType =
          assert (lib.count lib.id [ with_openblas with_cublas with_clblas ]) <= 1;
          if with_openblas then "openblas"
          else if with_cublas then "cublas"
          else if with_clblas then "clblas"
          else "";

        buildFlags = [
          "VERSION=v${version}"
          "BUILD_TYPE=${buildType}"
          "GO_TAGS=\"${builtins.concatStringsSep " " GO_TAGS}\""
        ]
        ++ lib.optional with_cublas "CUDA_LIBPATH=${cudaPackages.cuda_cudart}/lib";
      in
      ''
        mkdir sources
        make ${builtins.concatStringsSep " " buildFlags} build
      '';

    installPhase = ''
      install -Dt $out/bin ${pname}
    '';

    buildInputs = [
      protobuf # provides also abseil_cpp as propagated build input
      grpc
      openssl
    ]
    ++ lib.optionals with_stablediffusion
      [ opencv ]
    ++ lib.optionals with_tts
      [ sonic spdlog fmt onnxruntime ]
    ++ lib.optionals with_cublas
      [ cudaPackages.cudatoolkit cudaPackages.cuda_cudart ]
    ++ lib.optionals with_openblas
      [ openblas.dev ]
    ++ lib.optionals with_clblas
      [ clblast ocl-icd opencl-headers ]
    ;

    # patching rpath with patchelf doens't work. The execuable
    # raises an segmentation fault
    postFixup = ''
      wrapProgram $out/bin/${pname} \
    '' + lib.optionalString with_cublas ''
      --prefix LD_LIBRARY_PATH : "${cudaPackages.libcublas}/lib:${cudaPackages.cuda_cudart}/lib:/run/opengl-driver/lib" \
    '' + lib.optionalString with_clblas ''
      --prefix LD_LIBRARY_PATH : "${clblast}/lib:${ocl-icd}/lib" \
    '' + lib.optionalString with_openblas ''
      --prefix LD_LIBRARY_PATH : "${openblas}/lib" \
    '' + ''
      --prefix PATH : "${ffmpeg}/bin"
    '';

    nativeBuildInputs = [
      ncurses
      cmake
      makeWrapper
    ]
    ++ lib.optional with_openblas pkg-config
    ++ lib.optional with_cublas cudaPackages.cuda_nvcc
    ;

    passthru.features = {
      inherit
        with_cublas with_openblas with_tts with_stablediffusion
        with_tinydream with_clblas;
    };

    passthru.tests = {
      version = testers.testVersion {
        package = self;
        version = "v" + version;
      };
      health =
        let
          port = "8080";
        in
        testers.runNixOSTest {
          name = pname + "-health";
          nodes.machine = {
            systemd.services.local-ai = {
              wantedBy = [ "multi-user.target" ];
              serviceConfig.ExecStart = "${self}/bin/local-ai --localai-config-dir . --address :${port}";
            };
          };
          testScript = ''
            machine.wait_for_open_port(${port})
            machine.succeed("curl -f http://localhost:${port}/readyz")
          '';
        };
    };

    meta = with lib; {
      description = "OpenAI alternative to run local LLMs, image and audio generation";
      homepage = "https://localai.io";
      license = licenses.mit;
      maintainers = with maintainers; [ onny ck3d ];
      platforms = platforms.linux;
      broken =
        # TODO: provide onnxruntime in the right way
        with_tts
        || (with_tinydream && (lib.lessThan self.stdenv.cc.version "13"));
    };
  };
in
self