@@ -10,7 +10,7 @@ use llama_cpp_2::context::params::LlamaContextParams;
10
10
use llama_cpp_2:: context:: LlamaContext ;
11
11
use llama_cpp_2:: llama_batch:: LlamaBatch ;
12
12
use llama_cpp_2:: model:: params:: LlamaModelParams ;
13
- use llama_cpp_2:: mtmd:: * ;
13
+ use llama_cpp_2:: mtmd:: { MtmdBitmap , MtmdBitmapError , MtmdContext , MtmdContextParams , MtmdInputText } ;
14
14
15
15
use llama_cpp_2:: llama_backend:: LlamaBackend ;
16
16
use llama_cpp_2:: model:: { LlamaChatMessage , LlamaChatTemplate , LlamaModel , Special } ;
@@ -83,6 +83,8 @@ pub struct MtmdCliContext {
83
83
84
84
impl MtmdCliContext {
85
85
/// Creates a new MTMD CLI context
86
+ ///
87
+ /// # Errors
86
88
pub fn new (
87
89
params : & MtmdCliParams ,
88
90
model : & LlamaModel ,
@@ -101,11 +103,11 @@ impl MtmdCliContext {
101
103
) ?,
102
104
} ;
103
105
104
- let mtmd_ctx = MtmdContext :: init_from_file ( & params. mmproj_path , model, mtmd_params) ?;
106
+ let mtmd_ctx = MtmdContext :: init_from_file ( & params. mmproj_path , model, & mtmd_params) ?;
105
107
106
108
let chat_template = model
107
109
. chat_template ( params. chat_template . as_deref ( ) )
108
- . map_err ( |e| format ! ( "Failed to get chat template: {}" , e ) ) ?;
110
+ . map_err ( |e| format ! ( "Failed to get chat template: {e}" ) ) ?;
109
111
110
112
let batch = LlamaBatch :: new ( params. n_tokens , 1 ) ;
111
113
@@ -120,13 +122,15 @@ impl MtmdCliContext {
120
122
}
121
123
122
124
/// Loads media (image or audio) from the specified file path
125
+ /// # Errors
123
126
pub fn load_media ( & mut self , path : & str ) -> Result < ( ) , MtmdBitmapError > {
124
127
let bitmap = MtmdBitmap :: from_file ( & self . mtmd_ctx , path) ?;
125
128
self . bitmaps . push ( bitmap) ;
126
129
Ok ( ( ) )
127
130
}
128
131
129
132
/// Evaluates a chat message, tokenizing and processing it through the model
133
+ /// # Errors
130
134
pub fn eval_message (
131
135
& mut self ,
132
136
model : & LlamaModel ,
@@ -161,11 +165,12 @@ impl MtmdCliContext {
161
165
// Clear bitmaps after tokenization
162
166
self . bitmaps . clear ( ) ;
163
167
164
- self . n_past = chunks. eval_chunks ( & self . mtmd_ctx , & context, 0 , 0 , 1 , true ) ?;
168
+ self . n_past = chunks. eval_chunks ( & self . mtmd_ctx , context, 0 , 0 , 1 , true ) ?;
165
169
Ok ( ( ) )
166
170
}
167
171
168
172
/// Generates a response by sampling tokens from the model
173
+ /// # Errors
169
174
pub fn generate_response (
170
175
& mut self ,
171
176
model : & LlamaModel ,
@@ -190,7 +195,7 @@ impl MtmdCliContext {
190
195
191
196
// Print token
192
197
let piece = model. token_to_str ( token, Special :: Tokenize ) ?;
193
- print ! ( "{}" , piece ) ;
198
+ print ! ( "{piece}" ) ;
194
199
io:: stdout ( ) . flush ( ) ?;
195
200
196
201
// Prepare next batch
@@ -223,7 +228,7 @@ fn run_single_turn(
223
228
224
229
// Load media files
225
230
for image_path in & params. images {
226
- println ! ( "Loading image: {}" , image_path ) ;
231
+ println ! ( "Loading image: {image_path}" ) ;
227
232
ctx. load_media ( image_path) ?;
228
233
}
229
234
for audio_path in & params. audio {
@@ -233,7 +238,7 @@ fn run_single_turn(
233
238
// Create user message
234
239
let msg = LlamaChatMessage :: new ( "user" . to_string ( ) , prompt) ?;
235
240
236
- println ! ( "Evaluating message: {:?}" , msg ) ;
241
+ println ! ( "Evaluating message: {msg :?}" ) ;
237
242
238
243
// Evaluate the message (prefill)
239
244
ctx. eval_message ( model, context, msg, true ) ?;
@@ -269,7 +274,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
269
274
// Setup model parameters
270
275
let mut model_params = LlamaModelParams :: default ( ) ;
271
276
if !params. no_gpu {
272
- model_params = model_params. with_n_gpu_layers ( 1000000 ) ; // Use all layers on GPU
277
+ model_params = model_params. with_n_gpu_layers ( 1_000_000 ) ; // Use all layers on GPU
273
278
}
274
279
275
280
// Load model
0 commit comments