Views
No views yet
llama.cpp.llama.cpp and the llama_cpp_dart package.llama_cpp_dart package to run the model on a macOS machine with Metal GPU acceleration.radiology.png) was used as input.
ChatFormat.gemma, which correctly applies the <bos><start_of_turn>user... template required by the model.1import 'dart:io';
2import 'package:llama_cpp_dart/llama_cpp_dart.dart';
3
4Future<void> main() async {
5 Llama.libraryPath = "bin/MAC_ARM64/libmtmd.dylib";
6
7 final modelParams = ModelParams()..nGpuLayers = -1;
8
9 final contextParams = ContextParams()
10 ..nPredict = 512
11 ..nCtx = 8192
12 ..nBatch = 8192;
13
14 final samplerParams = SamplerParams()
15 ..temp = 0.0
16 ..topK = 64
17 ..topP = 0.95
18 ..penaltyRepeat = 1.1
19 ..addStopSequence("<end_of_turn>");
20
21 final llama = Llama(
22 "./model-radiology-Q4_K_M.gguf",
23 modelParams,
24 contextParams,
25 samplerParams,
26 false,
27 "./mmproj-radiology.gguf");
28
29 final image =
30 LlamaImage.fromFile(File("./radiology.png"));
31
32 final chat = ChatHistory();
33 chat.addMessage(role: Role.user, content: """<image>
34 You are an expert radiographer. Describe accurately what you see in this image.""");
35
36 // Use the correct chat format that matches the fine-tuning process
37 final prompt =
38 chat.exportFormat(ChatFormat.gemma, leaveLastAssistantOpen: true);
39
40 print("==== PROMPT SENT TO MODEL ====");
41 print(prompt);
42 print("==============================");
43
44 final sw = Stopwatch()..start();
45 try {
46 final stream = llama.generateWithMeda(prompt, inputs: [image]);
47
48 await for (final token in stream) {
49 stdout.write(token);
50 }
51 await stdout.flush();
52 stdout.writeln();
53 } on LlamaException catch (e) {
54 stderr.writeln("An error occurred: $e");
55 } finally {
56 sw.stop();
57 stdout.writeln('⏱️ Inference time: ${sw.elapsed}');
58 llama.dispose();
59 }
60}