car-inference 0.13.0

Local model inference for CAR — Candle backend with Qwen3 models
Documentation
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
//! Inference service — exposes inference as built-in CAR tools.
//!
//! Provides `ToolSchema` definitions for `infer`, `embed`, and `classify`
//! so they can be registered as built-in tools with real implementations.

use car_ir::ToolSchema;
use serde_json::{json, Value};

use crate::{
    ClassifyRequest, EmbedRequest, GenerateImageRequest, GenerateRequest, GenerateVideoRequest,
    InferenceEngine, InferenceError, SynthesizeRequest, TranscribeRequest,
};

/// Execute an inference tool call. Returns the result as JSON.
///
/// This is the bridge between CAR's tool dispatch and the inference engine.
pub async fn execute_tool(
    engine: &InferenceEngine,
    tool_name: &str,
    params: &Value,
) -> Result<Value, InferenceError> {
    match tool_name {
        "infer" => {
            let req: GenerateRequest = serde_json::from_value(params.clone())
                .map_err(|e| InferenceError::InferenceFailed(format!("bad params: {e}")))?;
            let result = engine.generate_tracked(req).await?;
            let mut resp = json!({ "text": result.text });
            if !result.tool_calls.is_empty() {
                resp["tool_calls"] = serde_json::to_value(&result.tool_calls).unwrap_or_default();
            }
            if let Some(usage) = &result.usage {
                resp["usage"] = json!({
                    "prompt_tokens": usage.prompt_tokens,
                    "completion_tokens": usage.completion_tokens,
                });
            }
            resp["model_used"] = json!(result.model_used);
            resp["latency_ms"] = json!(result.latency_ms);
            Ok(resp)
        }
        "embed" => {
            let req: EmbedRequest = serde_json::from_value(params.clone())
                .map_err(|e| InferenceError::InferenceFailed(format!("bad params: {e}")))?;
            let result = engine.embed(req).await?;
            Ok(json!({ "embeddings": result }))
        }
        "classify" => {
            let req: ClassifyRequest = serde_json::from_value(params.clone())
                .map_err(|e| InferenceError::InferenceFailed(format!("bad params: {e}")))?;
            let result = engine.classify(req).await?;
            Ok(json!({ "classifications": result }))
        }
        "transcribe" => {
            let req: TranscribeRequest = serde_json::from_value(params.clone())
                .map_err(|e| InferenceError::InferenceFailed(format!("bad params: {e}")))?;
            let result = engine.transcribe(req).await?;
            Ok(serde_json::to_value(result)
                .map_err(|e| InferenceError::InferenceFailed(format!("serialize result: {e}")))?)
        }
        "synthesize" => {
            let req: SynthesizeRequest = serde_json::from_value(params.clone())
                .map_err(|e| InferenceError::InferenceFailed(format!("bad params: {e}")))?;
            let result = engine.synthesize(req).await?;
            Ok(serde_json::to_value(result)
                .map_err(|e| InferenceError::InferenceFailed(format!("serialize result: {e}")))?)
        }
        "generate_image" => {
            let req: GenerateImageRequest = serde_json::from_value(params.clone())
                .map_err(|e| InferenceError::InferenceFailed(format!("bad params: {e}")))?;
            let result = engine.generate_image(req).await?;
            Ok(serde_json::to_value(result)
                .map_err(|e| InferenceError::InferenceFailed(format!("serialize result: {e}")))?)
        }
        "generate_video" => {
            let req: GenerateVideoRequest = serde_json::from_value(params.clone())
                .map_err(|e| InferenceError::InferenceFailed(format!("bad params: {e}")))?;
            let result = engine.generate_video(req).await?;
            Ok(serde_json::to_value(result)
                .map_err(|e| InferenceError::InferenceFailed(format!("serialize result: {e}")))?)
        }
        _ => Err(InferenceError::InferenceFailed(format!(
            "unknown inference tool: {tool_name}"
        ))),
    }
}

/// ToolSchema for text generation.
pub fn infer_schema() -> ToolSchema {
    ToolSchema {
        name: "infer".to_string(),
        description: "Generate text using a local Qwen3 model.".to_string(),
        parameters: json!({
            "type": "object",
            "properties": {
                "prompt": {
                    "type": "string",
                    "description": "The prompt to complete"
                },
                "model": {
                    "type": "string",
                    "description": "Model name (default: Qwen3-1.7B)"
                },
                "context": {
                    "type": "string",
                    "description": "Optional memory context to ground the model's response"
                },
                "params": {
                    "type": "object",
                    "properties": {
                        "temperature": { "type": "number", "default": 0.7 },
                        "top_p": { "type": "number", "default": 0.9 },
                        "top_k": { "type": "integer", "default": 0 },
                        "max_tokens": { "type": "integer", "default": 4096 },
                        "workload": {
                            "type": "string",
                            "enum": ["interactive", "batch", "background"],
                            "default": "interactive",
                            "description": "Routing workload class. Interactive favors latency; batch/background tolerate slower high-quality local models."
                        },
                        "stop": {
                            "type": "array",
                            "items": { "type": "string" }
                        }
                    }
                }
            },
            "required": ["prompt"]
        }),
        returns: Some(json!({
            "type": "object",
            "properties": {
                "text": { "type": "string" }
            }
        })),
        idempotent: false,
        cache_ttl_secs: None,
        rate_limit: None,
    }
}

/// ToolSchema for embedding generation.
pub fn embed_schema() -> ToolSchema {
    ToolSchema {
        name: "embed".to_string(),
        description: "Generate vector embeddings for text using a local Qwen3 model.".to_string(),
        parameters: json!({
            "type": "object",
            "properties": {
                "texts": {
                    "type": "array",
                    "items": { "type": "string" },
                    "description": "Texts to embed"
                },
                "model": {
                    "type": "string",
                    "description": "Model name (default: Qwen3-0.6B)"
                }
            },
            "required": ["texts"]
        }),
        returns: Some(json!({
            "type": "object",
            "properties": {
                "embeddings": {
                    "type": "array",
                    "items": {
                        "type": "array",
                        "items": { "type": "number" }
                    }
                }
            }
        })),
        idempotent: true,
        cache_ttl_secs: Some(3600),
        rate_limit: None,
    }
}

/// ToolSchema for classification.
pub fn classify_schema() -> ToolSchema {
    ToolSchema {
        name: "classify".to_string(),
        description: "Classify text against candidate labels using a local Qwen3 model."
            .to_string(),
        parameters: json!({
            "type": "object",
            "properties": {
                "text": {
                    "type": "string",
                    "description": "Text to classify"
                },
                "labels": {
                    "type": "array",
                    "items": { "type": "string" },
                    "description": "Candidate labels"
                },
                "model": {
                    "type": "string",
                    "description": "Model name (default: Qwen3-0.6B)"
                }
            },
            "required": ["text", "labels"]
        }),
        returns: Some(json!({
            "type": "object",
            "properties": {
                "classifications": {
                    "type": "array",
                    "items": {
                        "type": "object",
                        "properties": {
                            "label": { "type": "string" },
                            "score": { "type": "number" }
                        }
                    }
                }
            }
        })),
        idempotent: true,
        cache_ttl_secs: Some(300),
        rate_limit: None,
    }
}

/// ToolSchema for memory-grounded text generation.
pub fn infer_grounded_schema() -> ToolSchema {
    ToolSchema {
        name: "infer.grounded".to_string(),
        description: "Generate text grounded with memory context. Automatically queries the memgine for relevant context before generating.".to_string(),
        parameters: json!({
            "type": "object",
            "properties": {
                "prompt": {
                    "type": "string",
                    "description": "The prompt to complete"
                },
                "model": {
                    "type": "string",
                    "description": "Model name (default: Qwen3-1.7B)"
                },
                "params": {
                    "type": "object",
                    "properties": {
                        "temperature": { "type": "number", "default": 0.7 },
                        "top_p": { "type": "number", "default": 0.9 },
                        "top_k": { "type": "integer", "default": 0 },
                        "max_tokens": { "type": "integer", "default": 4096 },
                        "stop": {
                            "type": "array",
                            "items": { "type": "string" }
                        }
                    }
                }
            },
            "required": ["prompt"]
        }),
        returns: Some(json!({
            "type": "object",
            "properties": {
                "text": { "type": "string" }
            }
        })),
        idempotent: false,
        cache_ttl_secs: None,
        rate_limit: None,
    }
}

pub fn generate_image_schema() -> ToolSchema {
    ToolSchema {
        name: "generate_image".to_string(),
        description: "Generate an image using a local MLX image model.".to_string(),
        parameters: json!({
            "type": "object",
            "properties": {
                "prompt": { "type": "string" },
                "model": { "type": "string" },
                "width": { "type": "integer" },
                "height": { "type": "integer" },
                "steps": { "type": "integer" },
                "guidance": { "type": "number" },
                "seed": { "type": "integer" },
                "output_path": { "type": "string" },
                "format": { "type": "string", "default": "png" }
            },
            "required": ["prompt"]
        }),
        returns: Some(json!({
            "type": "object",
            "properties": {
                "image_path": { "type": "string" },
                "media_type": { "type": "string" },
                "model_used": { "type": "string" }
            }
        })),
        idempotent: false,
        cache_ttl_secs: None,
        rate_limit: None,
    }
}

pub fn generate_video_schema() -> ToolSchema {
    ToolSchema {
        name: "generate_video".to_string(),
        description: "Generate a video using a local MLX video model.".to_string(),
        parameters: json!({
            "type": "object",
            "properties": {
                "prompt": { "type": "string" },
                "model": { "type": "string" },
                "width": { "type": "integer" },
                "height": { "type": "integer" },
                "num_frames": { "type": "integer" },
                "steps": { "type": "integer" },
                "guidance": { "type": "number" },
                "seed": { "type": "integer" },
                "fps": { "type": "integer" },
                "output_path": { "type": "string" },
                "format": { "type": "string", "default": "mp4" },
                "image_path": { "type": "string", "description": "Reference image for image-to-video." },
                "audio_path": {
                    "type": "string",
                    "description": "Existing audio reference for audio_ref_video. The file is input conditioning for visual timing, rhythm, vocal cadence, intensity, and transitions; it is not generated output."
                },
                "video_path": {
                    "type": "string",
                    "description": "NOT YET IMPLEMENTED on any backend. Reference video for extension or retake modes; the request surface accepts this field but invoking it returns UnsupportedMode."
                },
                "extend_after_frame": {
                    "type": "integer",
                    "description": "NOT YET IMPLEMENTED on any backend. Extend mode: frame index in video_path to resume from."
                },
                "extend_context_frames": {
                    "type": "integer",
                    "description": "NOT YET IMPLEMENTED on any backend. Extend mode: how many trailing frames of video_path the model attends to when continuing the clip."
                },
                "retake_start_frame": {
                    "type": "integer",
                    "description": "NOT YET IMPLEMENTED on any backend. Retake mode: inclusive start frame of the span to regenerate."
                },
                "retake_end_frame": {
                    "type": "integer",
                    "description": "NOT YET IMPLEMENTED on any backend. Retake mode: exclusive end frame of the span to regenerate (half-open range)."
                },
                "mode": {
                    "type": "string",
                    "enum": ["t2v", "i2v", "audio_video", "audio_ref_video", "extend", "retake"],
                    "description": "Explicit mode. `t2v`, `i2v`, `audio_video`, and `audio_ref_video` are wired. `audio_ref_video` uses an existing audio file as the visual timing/intensity conditioning signal. `extend` and `retake` are NOT YET IMPLEMENTED — the request surface accepts them but calling a backend returns UnsupportedMode. Do not select `extend` or `retake` in production until this note is removed."
                }
            },
            "required": ["prompt"]
        }),
        returns: Some(json!({
            "type": "object",
            "properties": {
                "video_path": { "type": "string" },
                "media_type": { "type": "string" },
                "model_used": { "type": "string" }
            }
        })),
        idempotent: false,
        cache_ttl_secs: None,
        rate_limit: None,
    }
}

/// ToolSchema for model management — list models in the unified registry.
pub fn list_models_schema() -> ToolSchema {
    ToolSchema {
        name: "models.list".to_string(),
        description: "List all registered models (local and remote) with their capabilities, availability, and performance profiles.".to_string(),
        parameters: json!({
            "type": "object",
            "properties": {
                "capability": {
                    "type": "string",
                    "description": "Filter by capability (generate, embed, classify, code, reasoning, summarize, tool_use, vision, speech_to_text, text_to_speech, image_generation, video_generation)"
                },
                "local_only": {
                    "type": "boolean",
                    "description": "Only show local models"
                },
                "available_only": {
                    "type": "boolean",
                    "description": "Only show available models"
                }
            }
        }),
        returns: Some(json!({
            "type": "object",
            "properties": {
                "models": {
                    "type": "array",
                    "items": {
                        "type": "object",
                        "properties": {
                            "id": { "type": "string" },
                            "name": { "type": "string" },
                            "provider": { "type": "string" },
                            "capabilities": { "type": "array", "items": { "type": "string" } },
                            "available": { "type": "boolean" },
                            "is_local": { "type": "boolean" }
                        }
                    }
                }
            }
        })),
        idempotent: true,
        cache_ttl_secs: Some(60),
        rate_limit: None,
    }
}

/// ToolSchema for model routing — show which model would be selected for a prompt.
pub fn route_model_schema() -> ToolSchema {
    ToolSchema {
        name: "models.route".to_string(),
        description: "Route a prompt to the best model without executing. Shows the routing decision, strategy, and fallback chain.".to_string(),
        parameters: json!({
            "type": "object",
            "properties": {
                "prompt": {
                    "type": "string",
                    "description": "The prompt to route"
                }
            },
            "required": ["prompt"]
        }),
        returns: Some(json!({
            "type": "object",
            "properties": {
                "model_id": { "type": "string" },
                "model_name": { "type": "string" },
                "strategy": { "type": "string" },
                "complexity": { "type": "string" },
                "predicted_quality": { "type": "number" },
                "reason": { "type": "string" },
                "fallbacks": { "type": "array", "items": { "type": "string" } }
            }
        })),
        idempotent: true,
        cache_ttl_secs: None,
        rate_limit: None,
    }
}

/// ToolSchema for model performance stats.
pub fn model_stats_schema() -> ToolSchema {
    ToolSchema {
        name: "models.stats".to_string(),
        description: "Get performance statistics for models based on observed outcomes."
            .to_string(),
        parameters: json!({
            "type": "object",
            "properties": {
                "model_id": {
                    "type": "string",
                    "description": "Model ID to get stats for (omit for all models)"
                }
            }
        }),
        returns: Some(json!({
            "type": "object",
            "properties": {
                "profiles": {
                    "type": "array",
                    "items": {
                        "type": "object",
                        "properties": {
                            "model_id": { "type": "string" },
                            "total_calls": { "type": "integer" },
                            "success_rate": { "type": "number" },
                            "avg_latency_ms": { "type": "number" },
                            "ema_quality": { "type": "number" }
                        }
                    }
                }
            }
        })),
        idempotent: true,
        cache_ttl_secs: Some(30),
        rate_limit: None,
    }
}

/// ToolSchema for speech transcription.
pub fn transcribe_schema() -> ToolSchema {
    ToolSchema {
        name: "transcribe".to_string(),
        description: "Transcribe an audio file using the best available local or remote speech-to-text model.".to_string(),
        parameters: json!({
            "type": "object",
            "properties": {
                "audio_path": {
                    "type": "string",
                    "description": "Path to the audio file to transcribe"
                },
                "model": {
                    "type": "string",
                    "description": "Optional STT model override"
                },
                "language": {
                    "type": "string",
                    "description": "Optional language hint"
                },
                "prompt": {
                    "type": "string",
                    "description": "Optional context or hotword hint"
                },
                "timestamps": {
                    "type": "boolean",
                    "description": "Request verbose timestamp-oriented output when supported"
                }
            },
            "required": ["audio_path"]
        }),
        returns: Some(json!({
            "type": "object",
            "properties": {
                "text": { "type": "string" },
                "model_used": { "type": "string" },
                "language": { "type": "string" }
            }
        })),
        idempotent: true,
        cache_ttl_secs: None,
        rate_limit: None,
    }
}

/// ToolSchema for speech synthesis.
pub fn synthesize_schema() -> ToolSchema {
    ToolSchema {
        name: "synthesize".to_string(),
        description: "Synthesize speech to an audio file using the best available local or remote text-to-speech model.".to_string(),
        parameters: json!({
            "type": "object",
            "properties": {
                "text": {
                    "type": "string",
                    "description": "Text to synthesize"
                },
                "model": {
                    "type": "string",
                    "description": "Optional TTS model override"
                },
                "voice": {
                    "type": "string",
                    "description": "Optional voice preset or provider-specific voice ID"
                },
                "language": {
                    "type": "string",
                    "description": "Optional language or language code"
                },
                "speed": {
                    "type": "number",
                    "description": "Optional playback speed multiplier"
                },
                "output_path": {
                    "type": "string",
                    "description": "Optional destination path for the generated audio"
                },
                "format": {
                    "type": "string",
                    "description": "Audio format (wav, mp3, flac, pcm)",
                    "default": "wav"
                }
            },
            "required": ["text"]
        }),
        returns: Some(json!({
            "type": "object",
            "properties": {
                "audio_path": { "type": "string" },
                "media_type": { "type": "string" },
                "model_used": { "type": "string" },
                "voice_used": { "type": "string" }
            }
        })),
        idempotent: false,
        cache_ttl_secs: None,
        rate_limit: None,
    }
}

/// All inference tool schemas.
pub fn all_schemas() -> Vec<ToolSchema> {
    vec![
        infer_schema(),
        infer_grounded_schema(),
        embed_schema(),
        classify_schema(),
        generate_image_schema(),
        generate_video_schema(),
        transcribe_schema(),
        synthesize_schema(),
        list_models_schema(),
        route_model_schema(),
        model_stats_schema(),
    ]
}