16 imports = [ ./module.nix ];
20 threads = config.virtualisation.cores;
25 inherit (self.lib) genModels;
28 version = testers.testVersion {
30 version = "v" + self.version;
31 command = "local-ai --help";
34 health = testers.runNixOSTest {
35 name = self.name + "-health";
36 nodes.machine = common-config;
42 machine.wait_for_open_port(${port})
43 machine.succeed("curl -f http://localhost:${port}/readyz")
45 machine.succeed("${prom2json}/bin/prom2json http://localhost:${port}/metrics > metrics.json")
46 machine.copy_from_vm("metrics.json")
50 # https://localai.io/features/embeddings/#bert-embeddings
54 model-configs.${model} = {
55 # Note: q4_0 and q4_1 models can not be loaded
56 parameters.model = fetchurl {
57 url = "https://huggingface.co/skeskinen/ggml/resolve/main/all-MiniLM-L6-v2/ggml-model-f16.bin";
58 hash = "sha256-nBlbJFOk/vYKT2vjqIo5IRNmIU32SYpP5IhcniIxT1A=";
60 backend = "bert-embeddings";
64 models = genModels model-configs;
68 input = "Your text string goes here";
71 testers.runNixOSTest {
72 name = self.name + "-bert";
74 imports = [ common-config ];
75 virtualisation.cores = 2;
76 virtualisation.memorySize = 2048;
77 services.local-ai.models = models;
80 inherit models requests;
87 machine.wait_for_open_port(${port})
88 machine.succeed("curl -f http://localhost:${port}/readyz")
89 machine.succeed("curl -f http://localhost:${port}/v1/models --output models.json")
90 machine.succeed("${jq}/bin/jq --exit-status 'debug | .data[].id == \"${model}\"' models.json")
92 machine.succeed("curl -f http://localhost:${port}/embeddings --json @${writers.writeJSON "request.json" requests.request} --output embeddings.json")
93 machine.copy_from_vm("embeddings.json")
94 machine.succeed("${jq}/bin/jq --exit-status 'debug | .model == \"${model}\"' embeddings.json")
96 machine.succeed("${prom2json}/bin/prom2json http://localhost:${port}/metrics > metrics.json")
97 machine.copy_from_vm("metrics.json")
102 // lib.optionalAttrs (!self.features.with_cublas && !self.features.with_clblas) {
103 # https://localai.io/docs/getting-started/manual/
106 model = "gpt-3.5-turbo";
108 # https://localai.io/advanced/#full-config-model-file-reference
109 model-configs.${model} = rec {
110 context_size = 16 * 1024; # 128kb is possible, but needs 16GB RAM
111 backend = "llama-cpp";
113 # https://ai.meta.com/blog/meta-llama-3-1/
115 url = "https://huggingface.co/lmstudio-community/Meta-Llama-3.1-8B-Instruct-GGUF/resolve/main/Meta-Llama-3.1-8B-Instruct-Q4_K_M.gguf";
116 hash = "sha256-8r4+GiOcEsnz8BqWKxH7KAf4Ay/bY7ClUC6kLd71XkQ=";
119 # https://deepinfra.com/meta-llama/Meta-Llama-3.1-8B-Instruct
123 # following parameter leads to outputs like: !!!!!!!!!!!!!!!!!!!
125 presence_penalty = 0;
126 frequency_penalty = 0;
129 stopwords = [ "<|eot_id|>" ];
131 # Templates implement following specifications
132 # https://github.com/meta-llama/llama3/tree/main?tab=readme-ov-file#instruction-tuned-models
133 # ... and are insprired by:
134 # https://github.com/mudler/LocalAI/blob/master/embedded/models/llama3-instruct.yaml
136 # The rules for template evaluateion are defined here:
137 # https://pkg.go.dev/text/template
139 <|start_header_id|>{{.RoleName}}<|end_header_id|>
141 {{.Content}}${builtins.head stopwords}'';
143 chat = "{{.Input}}<|start_header_id|>assistant<|end_header_id|>";
145 completion = "{{.Input}}";
149 models = genModels model-configs;
152 # https://localai.io/features/text-generation/#chat-completions
158 content = "1 + 2 = ?";
162 # https://localai.io/features/text-generation/#edit-completions
165 instruction = "rephrase";
166 input = "Black cat jumped out of the window";
169 # https://localai.io/features/text-generation/#completions
172 prompt = "A long time ago in a galaxy far, far away";
176 testers.runNixOSTest {
177 name = self.name + "-llama";
179 imports = [ common-config ];
180 virtualisation.cores = 4;
181 virtualisation.memorySize = 8192;
182 services.local-ai.models = models;
183 # TODO: Add test case parallel requests
184 services.local-ai.parallelRequests = 2;
187 inherit models requests;
194 machine.wait_for_open_port(${port})
195 machine.succeed("curl -f http://localhost:${port}/readyz")
196 machine.succeed("curl -f http://localhost:${port}/v1/models --output models.json")
197 machine.succeed("${jq}/bin/jq --exit-status 'debug | .data[].id == \"${model}\"' models.json")
199 machine.succeed("curl -f http://localhost:${port}/v1/chat/completions --json @${writers.writeJSON "request-chat-completions.json" requests.chat-completions} --output chat-completions.json")
200 machine.copy_from_vm("chat-completions.json")
201 machine.succeed("${jq}/bin/jq --exit-status 'debug | .object == \"chat.completion\"' chat-completions.json")
202 machine.succeed("${jq}/bin/jq --exit-status 'debug | .choices | first.message.content | split(\" \") | last | tonumber == 3' chat-completions.json")
204 machine.succeed("curl -f http://localhost:${port}/v1/edits --json @${writers.writeJSON "request-edit-completions.json" requests.edit-completions} --output edit-completions.json")
205 machine.copy_from_vm("edit-completions.json")
206 machine.succeed("${jq}/bin/jq --exit-status 'debug | .object == \"edit\"' edit-completions.json")
207 machine.succeed("${jq}/bin/jq --exit-status '.usage.completion_tokens | debug == ${toString requests.edit-completions.max_tokens}' edit-completions.json")
209 machine.succeed("curl -f http://localhost:${port}/v1/completions --json @${writers.writeJSON "request-completions.json" requests.completions} --output completions.json")
210 machine.copy_from_vm("completions.json")
211 machine.succeed("${jq}/bin/jq --exit-status 'debug | .object ==\"text_completion\"' completions.json")
212 machine.succeed("${jq}/bin/jq --exit-status '.usage.completion_tokens | debug == ${
213 toString model-configs.${model}.parameters.max_tokens
214 }' completions.json")
216 machine.succeed("${prom2json}/bin/prom2json http://localhost:${port}/metrics > metrics.json")
217 machine.copy_from_vm("metrics.json")
224 (self.features.with_tts && !self.features.with_cublas && !self.features.with_clblas)
226 # https://localai.io/features/text-to-audio/#piper
229 model-stt = "whisper-en";
230 model-configs.${model-stt} = {
232 parameters.model = fetchurl {
233 url = "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-tiny.en-q5_1.bin";
234 hash = "sha256-x3xXZvHO8JtrfUfyG1Rsvd1BV4hrO11tT3CekeZsfCs=";
238 model-tts = "piper-en";
239 model-configs.${model-tts} = {
241 parameters.model = "en-us-danny-low.onnx";
246 models = genModels model-configs;
249 inherit (models) name;
253 url = "https://github.com/rhasspy/piper/releases/download/v0.0.2/voice-en-us-danny-low.tar.gz";
254 hash = "sha256-5wf+6H5HeQY0qgdqnAG1vSqtjIFM9lXH53OgouuPm0M=";
262 input = "Hello, how are you?";
265 testers.runNixOSTest {
266 name = self.name + "-tts";
268 imports = [ common-config ];
269 virtualisation.cores = 2;
270 services.local-ai.models = models;
273 inherit models requests;
280 machine.wait_for_open_port(${port})
281 machine.succeed("curl -f http://localhost:${port}/readyz")
282 machine.succeed("curl -f http://localhost:${port}/v1/models --output models.json")
283 machine.succeed("${jq}/bin/jq --exit-status 'debug' models.json")
285 machine.succeed("curl -f http://localhost:${port}/tts --json @${writers.writeJSON "request.json" requests.request} --output out.wav")
286 machine.copy_from_vm("out.wav")
288 machine.succeed("curl -f http://localhost:${port}/v1/audio/transcriptions --header 'Content-Type: multipart/form-data' --form file=@out.wav --form model=${model-stt} --output transcription.json")
289 machine.copy_from_vm("transcription.json")
290 machine.succeed("${jq}/bin/jq --exit-status 'debug | .segments | first.text == \"${requests.request.input}\"' transcription.json")
292 machine.succeed("${prom2json}/bin/prom2json http://localhost:${port}/metrics > metrics.json")
293 machine.copy_from_vm("metrics.json")