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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
//! `lattice` CLI — interactive chat and HTTP serve subcommands.
//!
//! # Usage
//!
//! ```text
//! lattice chat --model /path/to/model [--max-tokens 256] [--temperature 0.7]
//! lattice serve --model /path/to/model [--host 127.0.0.1] [--port 8080] [--max-tokens 256]
//! ```
use clap::{Parser, Subcommand};
#[derive(Parser)]
#[command(name = "lattice", about = "Pure-Rust transformer inference engine")]
struct Cli {
#[command(subcommand)]
command: Command,
}
#[derive(Subcommand)]
enum Command {
/// Interactive chat with a model
Chat {
/// Path to model directory (SafeTensors + config.json)
#[arg(long)]
model: String,
/// Maximum tokens to generate per response
#[arg(long, default_value = "256")]
max_tokens: usize,
/// Sampling temperature
#[arg(long, default_value = "0.7")]
temperature: f32,
},
/// Start HTTP server with OpenAI-compatible API
Serve {
/// Path to model directory
#[arg(long)]
model: String,
/// Host address to bind (default: 127.0.0.1; use 0.0.0.0 for LAN)
#[arg(long, default_value = "127.0.0.1")]
host: String,
/// Port to listen on
#[arg(long, default_value = "8080")]
port: u16,
/// Maximum tokens to generate per request (default when request omits max_tokens)
#[arg(long, default_value = "256")]
max_tokens: usize,
/// Model identifier echoed in responses (defaults to the model path basename)
#[arg(long)]
model_id: Option<String>,
},
}
// ---------------------------------------------------------------------------
// chat subcommand
// ---------------------------------------------------------------------------
fn run_chat(model_path: &str, max_tokens: usize, temperature: f32) {
use std::io::{BufRead, Write};
use std::path::Path;
let path = Path::new(model_path);
eprintln!("Loading model from {model_path}...");
let model = match lattice_inference::model::qwen35::Qwen35Model::from_safetensors(path) {
Ok(m) => m,
Err(e) => {
eprintln!("Error: failed to load model: {e}");
std::process::exit(1);
}
};
eprintln!("Model loaded. Type 'exit' or 'quit' to stop.\n");
let gen_cfg = lattice_inference::model::qwen35_config::GenerateConfig {
max_new_tokens: max_tokens,
temperature,
..Default::default()
};
let stdin = std::io::stdin();
let mut stdout = std::io::stdout();
for line in stdin.lock().lines() {
let prompt = match line {
Ok(l) => l,
Err(e) => {
eprintln!("Error reading input: {e}");
break;
}
};
let trimmed = prompt.trim();
if trimmed.is_empty() {
continue;
}
if trimmed.eq_ignore_ascii_case("exit") || trimmed.eq_ignore_ascii_case("quit") {
break;
}
match model.generate(trimmed, &gen_cfg) {
Ok(output) => {
let _ = writeln!(stdout, "{}", output.text);
let _ = writeln!(
stdout,
"[{} prompt tokens, {} generated]",
output.prompt_tokens, output.generated_tokens
);
}
Err(e) => {
eprintln!("Generation error: {e}");
}
}
}
}
// ---------------------------------------------------------------------------
// serve subcommand: OpenAI-compatible HTTP API
// ---------------------------------------------------------------------------
mod serve {
use axum::{
Json, Router,
extract::{DefaultBodyLimit, State},
http::StatusCode,
response::{IntoResponse, Response},
routing::{get, post},
};
use lattice_inference::Tokenizer;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{SystemTime, UNIX_EPOCH};
/// Request body cap: 1 MiB. Requests above this return HTTP 413.
const REQUEST_BODY_LIMIT_BYTES: usize = 1_048_576;
// -----------------------------------------------------------------------
// Shared application state
// -----------------------------------------------------------------------
/// State shared across all request handlers via axum's `State` extractor.
#[derive(Clone)]
pub struct AppState {
/// The loaded model, wrapped in Arc so it can be cheaply cloned into
/// `spawn_blocking` closures without copying weights.
pub model: Arc<lattice_inference::model::qwen35::Qwen35Model>,
/// Default `max_tokens` value used when a request omits the field.
/// Set from the `--max-tokens` CLI flag passed to `lattice serve`.
pub default_max_tokens: usize,
/// Hard upper bound on `max_tokens` accepted from any request.
/// Prevents callers from requesting unbounded generation.
pub max_tokens_cap: usize,
/// Canonical model identifier echoed in every response.
/// Derived from the `--model-id` flag or the model path basename.
pub model_id: String,
/// Monotonically increasing counter used to make response IDs unique
/// across concurrent requests within the same second.
pub request_counter: Arc<AtomicU64>,
}
// -----------------------------------------------------------------------
// Error type
// -----------------------------------------------------------------------
/// Structured HTTP error that serialises to the OpenAI error envelope so
/// that clients can parse failure responses uniformly.
#[derive(Debug)]
pub enum ApiError {
/// Caller mistake — HTTP 400.
BadRequest { message: String, code: &'static str },
/// Request body exceeds size limit — HTTP 413.
PayloadTooLarge { message: String },
/// Server-side failure — HTTP 500.
Internal { message: String },
}
#[derive(Serialize)]
struct ErrorBody {
error: ErrorDetail,
}
#[derive(Serialize)]
struct ErrorDetail {
message: String,
r#type: &'static str,
code: String,
param: Option<String>,
}
impl IntoResponse for ApiError {
fn into_response(self) -> Response {
match self {
ApiError::BadRequest { message, code } => {
let body = Json(ErrorBody {
error: ErrorDetail {
message,
r#type: "invalid_request_error",
code: code.to_string(),
param: None,
},
});
(StatusCode::BAD_REQUEST, body).into_response()
}
ApiError::PayloadTooLarge { message } => {
let body = Json(ErrorBody {
error: ErrorDetail {
message,
r#type: "invalid_request_error",
code: "request_body_too_large".to_string(),
param: None,
},
});
(StatusCode::PAYLOAD_TOO_LARGE, body).into_response()
}
ApiError::Internal { message } => {
let body = Json(ErrorBody {
error: ErrorDetail {
message,
r#type: "server_error",
code: "internal_error".to_string(),
param: None,
},
});
(StatusCode::INTERNAL_SERVER_ERROR, body).into_response()
}
}
}
}
// -----------------------------------------------------------------------
// Request / response types
// -----------------------------------------------------------------------
/// OpenAI-compatible chat completions request.
///
/// Known-but-unsupported fields (`stream=true`, `tools`, `tool_choice`,
/// `logprobs=true`, `n > 1`, `response_format` other than `"text"`) are
/// parsed and explicitly rejected with HTTP 400 rather than silently dropped.
/// `stop` is accepted and parsed into string-level stop sequences.
/// Unknown fields are ignored by default (serde default).
#[derive(Deserialize)]
pub struct ChatCompletionRequest {
/// Required: must match the served model identifier.
pub model: String,
pub messages: Vec<Message>,
/// Generation token budget. Use at most one of `max_tokens` /
/// `max_completion_tokens`; if both are present they must agree.
pub max_tokens: Option<usize>,
/// Alias for `max_tokens` (current OpenAI naming).
pub max_completion_tokens: Option<usize>,
pub temperature: Option<f32>,
/// Nucleus sampling probability mass. Mapped into `GenerateConfig`.
pub top_p: Option<f32>,
/// SSE streaming — not yet supported; rejected with 400.
pub stream: Option<bool>,
/// Stop sequences — a JSON string or array of strings (up to 4, non-empty).
/// Parsed by `parse_stop_strings`; null/absent → empty vec (no stops).
pub stop: Option<Value>,
/// Deterministic sampling seed. Mapped into `GenerateConfig`.
pub seed: Option<u64>,
/// Response format constraint — only `"text"` is accepted.
pub response_format: Option<ResponseFormat>,
/// Tool definitions — not supported; rejected with 400.
pub tools: Option<Value>,
/// Tool choice — not supported; rejected with 400.
pub tool_choice: Option<Value>,
/// Log-probabilities — not supported; rejected with 400.
pub logprobs: Option<bool>,
/// Number of completions — only `1` is accepted.
pub n: Option<usize>,
}
#[derive(Deserialize)]
pub struct ResponseFormat {
pub r#type: String,
}
/// Message content: either a plain string or an array of content parts.
/// Non-text parts (image, audio, file) are rejected with HTTP 400.
#[derive(Deserialize)]
#[serde(untagged)]
pub enum MessageContent {
Text(String),
Parts(Vec<ContentPart>),
}
#[derive(Deserialize)]
pub struct ContentPart {
#[serde(rename = "type")]
pub kind: String,
pub text: Option<String>,
}
#[derive(Deserialize)]
pub struct Message {
pub role: String,
pub content: MessageContent,
}
#[derive(Serialize)]
pub struct ChatCompletionResponse {
pub id: String,
pub object: String,
pub created: u64,
pub model: String,
pub choices: Vec<Choice>,
pub usage: Usage,
}
#[derive(Serialize)]
pub struct Choice {
pub index: usize,
pub message: ResponseMessage,
pub finish_reason: String,
}
#[derive(Serialize)]
pub struct ResponseMessage {
pub role: String,
pub content: String,
}
#[derive(Serialize)]
pub struct Usage {
pub prompt_tokens: usize,
pub completion_tokens: usize,
pub total_tokens: usize,
}
#[derive(Serialize)]
pub struct HealthResponse {
pub status: &'static str,
}
// -----------------------------------------------------------------------
// Validation helpers — pure functions, no model required, easily tested
// -----------------------------------------------------------------------
/// Resolve the effective `max_tokens`, rejecting zero, values above the
/// server cap, and conflicting `max_tokens` / `max_completion_tokens`.
fn validate_max_tokens(
req_max: Option<usize>,
req_max_completion: Option<usize>,
default_max_tokens: usize,
max_tokens_cap: usize,
) -> Result<usize, ApiError> {
let effective = match (req_max, req_max_completion) {
(None, None) => default_max_tokens,
(Some(a), None) => a,
(None, Some(b)) => b,
(Some(a), Some(b)) if a == b => a,
(Some(a), Some(b)) => {
return Err(ApiError::BadRequest {
message: format!(
"max_tokens ({a}) and max_completion_tokens ({b}) differ; supply only one"
),
code: "invalid_request",
});
}
};
if effective == 0 {
return Err(ApiError::BadRequest {
message: "max_tokens must be at least 1".to_string(),
code: "invalid_max_tokens",
});
}
if effective > max_tokens_cap {
return Err(ApiError::BadRequest {
message: format!("max_tokens {effective} exceeds server limit {max_tokens_cap}"),
code: "max_tokens_exceeds_limit",
});
}
Ok(effective)
}
/// Validate `temperature` is in `[0.0, 2.0]`.
fn validate_temperature(value: Option<f32>) -> Result<f32, ApiError> {
let temperature = value.unwrap_or(0.7);
if !(0.0..=2.0).contains(&temperature) {
return Err(ApiError::BadRequest {
message: "temperature must be between 0 and 2".to_string(),
code: "invalid_temperature",
});
}
Ok(temperature)
}
/// Validate `top_p` is in `(0.0, 1.0]`.
fn validate_top_p(value: Option<f32>) -> Result<f32, ApiError> {
let top_p = value.unwrap_or(0.9);
if top_p <= 0.0 || top_p > 1.0 {
return Err(ApiError::BadRequest {
message: "top_p must be greater than 0 and at most 1".to_string(),
code: "invalid_top_p",
});
}
Ok(top_p)
}
/// Parse the OpenAI `stop` field into a `Vec<String>`.
///
/// Accepted forms:
/// - `null` / absent → empty vec (no string-level stops)
/// - a JSON string → `vec![s]`
/// - a JSON array of 1–4 non-empty strings → that vec
///
/// Returns `Err(BadRequest)` for:
/// - an empty array
/// - an array with more than 4 elements
/// - any array element that is not a string
/// - any stop string that is empty
fn parse_stop_strings(stop: &Option<Value>) -> Result<Vec<String>, ApiError> {
match stop {
None => Ok(vec![]),
Some(Value::Null) => Ok(vec![]),
Some(Value::String(s)) => {
if s.is_empty() {
return Err(ApiError::BadRequest {
message: "stop string must not be empty".to_string(),
code: "invalid_stop",
});
}
Ok(vec![s.clone()])
}
Some(Value::Array(arr)) => {
if arr.is_empty() {
return Err(ApiError::BadRequest {
message: "stop array must not be empty".to_string(),
code: "invalid_stop",
});
}
if arr.len() > 4 {
return Err(ApiError::BadRequest {
message: format!("stop array has {} elements; maximum is 4", arr.len()),
code: "invalid_stop",
});
}
let mut out = Vec::with_capacity(arr.len());
for item in arr {
match item {
Value::String(s) => {
if s.is_empty() {
return Err(ApiError::BadRequest {
message: "stop string must not be empty".to_string(),
code: "invalid_stop",
});
}
out.push(s.clone());
}
_ => {
return Err(ApiError::BadRequest {
message: "each element of stop must be a string".to_string(),
code: "invalid_stop",
});
}
}
}
Ok(out)
}
Some(_) => Err(ApiError::BadRequest {
message: "stop must be a string or array of strings".to_string(),
code: "invalid_stop",
}),
}
}
/// Reject OpenAI fields that are parsed but not yet implemented.
fn reject_unsupported(req: &ChatCompletionRequest) -> Result<(), ApiError> {
if req.stream.unwrap_or(false) {
return Err(ApiError::BadRequest {
message: "stream=true is not supported by this server".to_string(),
code: "unsupported_feature",
});
}
if req.tools.is_some() || req.tool_choice.is_some() {
return Err(ApiError::BadRequest {
message: "tools and tool_choice are not supported by this server".to_string(),
code: "unsupported_feature",
});
}
if req.logprobs.unwrap_or(false) {
return Err(ApiError::BadRequest {
message: "logprobs is not supported by this server".to_string(),
code: "unsupported_feature",
});
}
if req.n.unwrap_or(1) > 1 {
return Err(ApiError::BadRequest {
message: "n > 1 is not supported".to_string(),
code: "unsupported_feature",
});
}
if let Some(fmt) = &req.response_format {
if fmt.r#type != "text" {
return Err(ApiError::BadRequest {
message: format!(
"response_format.type '{}' is not supported; use 'text'",
fmt.r#type
),
code: "unsupported_feature",
});
}
}
Ok(())
}
// -----------------------------------------------------------------------
// Helpers
// -----------------------------------------------------------------------
/// Extract a plain text string from a message content value.
/// Returns `Err` for non-text content parts (image, audio, file).
fn message_text(content: &MessageContent) -> Result<String, ApiError> {
match content {
MessageContent::Text(text) => Ok(text.clone()),
MessageContent::Parts(parts) => {
let mut out = String::new();
for part in parts {
if part.kind != "text" {
return Err(ApiError::BadRequest {
message: format!(
"content part type '{}' is not supported; only 'text' parts are accepted",
part.kind
),
code: "unsupported_feature",
});
}
out.push_str(part.text.as_deref().unwrap_or(""));
}
Ok(out)
}
}
}
/// Build a single prompt string from the full message list using Qwen ChatML format.
///
/// Format (one block per message, in order):
/// ```text
/// <|im_start|>system
/// {content}<|im_end|>
/// <|im_start|>user
/// {content}<|im_end|>
/// <|im_start|>assistant
/// {content}<|im_end|>
/// ```
/// The final line is the open generation prompt `<|im_start|>assistant\n` — no closing
/// `<|im_end|>` — so the model generates from there.
///
/// Only the roles `system`, `user`, and `assistant` are supported. Any other role
/// returns `Err` so the handler can respond with HTTP 400.
fn render_prompt(messages: &[Message]) -> Result<String, ApiError> {
let mut buf = String::new();
for msg in messages {
let content = message_text(&msg.content)?;
match msg.role.as_str() {
"system" | "user" | "assistant" => {
buf.push_str(&format!(
"<|im_start|>{}\n{}<|im_end|>\n",
msg.role, content
));
}
"tool" | "developer" => {
return Err(ApiError::BadRequest {
message: format!("role '{}' is not supported by this server", msg.role),
code: "unsupported_feature",
});
}
other => {
return Err(ApiError::BadRequest {
message: format!(
"unsupported role '{other}'; must be 'system', 'user', or 'assistant'"
),
code: "invalid_role",
});
}
}
}
// Open generation turn — model generates from here.
buf.push_str("<|im_start|>assistant\n");
Ok(buf)
}
// -----------------------------------------------------------------------
// Helpers
// -----------------------------------------------------------------------
/// Maps a `GenerateOutput` to the OpenAI `finish_reason` string.
///
/// Returns `"stop"` when the library explicitly ended generation via a stop
/// condition (EOS token, stop-token-id, or stop-string match); `"length"` when
/// the token budget was exhausted without a stop condition.
pub(super) fn finish_reason_for(
output: &lattice_inference::model::qwen35_config::GenerateOutput,
) -> &'static str {
if output.stopped { "stop" } else { "length" }
}
// Handlers
// -----------------------------------------------------------------------
pub async fn health() -> Json<HealthResponse> {
Json(HealthResponse { status: "ok" })
}
pub async fn chat_completions(
State(state): State<AppState>,
result: Result<Json<ChatCompletionRequest>, axum::extract::rejection::JsonRejection>,
) -> Result<Json<ChatCompletionResponse>, ApiError> {
// Surface JSON extraction failures as structured 400 responses.
// Log the raw parser message server-side; never forward it to clients.
let Json(req) = result.map_err(|rejection| {
if rejection.status() == StatusCode::PAYLOAD_TOO_LARGE {
ApiError::PayloadTooLarge {
message: "request body exceeds 1 MiB limit".to_string(),
}
} else {
eprintln!("invalid request body: {}", rejection.body_text());
ApiError::BadRequest {
message: "invalid JSON request body".to_string(),
code: "invalid_request_body",
}
}
})?;
// Reject unsupported OpenAI features before any further processing.
reject_unsupported(&req)?;
// Validate that the caller targets the served model.
if req.model != state.model_id {
return Err(ApiError::BadRequest {
message: format!(
"model '{}' is not loaded; this server serves '{}'",
req.model, state.model_id
),
code: "model_not_found",
});
}
if req.messages.is_empty() {
return Err(ApiError::BadRequest {
message: "messages must not be empty".to_string(),
code: "invalid_messages",
});
}
// Require the conversation to end with a user turn (Qwen ChatML constraint).
let last_role = req.messages.last().map(|m| m.role.as_str()).unwrap_or("");
if last_role != "user" {
return Err(ApiError::BadRequest {
message: "the last message must have role 'user'".to_string(),
code: "invalid_messages",
});
}
// Validate and resolve sampling parameters.
let max_tokens = validate_max_tokens(
req.max_tokens,
req.max_completion_tokens,
state.default_max_tokens,
state.max_tokens_cap,
)?;
let temperature = validate_temperature(req.temperature)?;
let top_p = validate_top_p(req.top_p)?;
// Render the full conversation into a ChatML prompt. Returns 400 for
// any unsupported role or content-part type encountered.
let prompt = render_prompt(&req.messages)?;
// Preflight: reject prompts that would overflow the model's context window
// before entering the blocking generation path. This converts what would
// otherwise be a panic inside spawn_blocking into a clean 400 response.
let prompt_token_count = state.model.tokenizer().tokenize(&prompt).real_length;
let max_context = state.model.max_context();
if prompt_token_count == 0 || prompt_token_count.saturating_add(max_tokens) > max_context {
return Err(ApiError::BadRequest {
message: format!(
"prompt ({prompt_token_count} tokens) plus max_tokens ({max_tokens}) \
exceeds model context window ({max_context})"
),
code: "context_length_exceeded",
});
}
let stop_strings = parse_stop_strings(&req.stop)?;
let gen_cfg = lattice_inference::model::qwen35_config::GenerateConfig {
max_new_tokens: max_tokens,
temperature,
top_p,
seed: req.seed,
stop_strings,
..Default::default()
};
let model = Arc::clone(&state.model);
// `generate` is CPU-bound blocking work; run it on the blocking thread pool.
let output = tokio::task::spawn_blocking(move || model.generate(&prompt, &gen_cfg))
.await
.map_err(|e| {
eprintln!("task join error: {e}");
ApiError::Internal {
message: "inference failed".to_string(),
}
})?
.map_err(|e| {
eprintln!("generation error: {e}");
ApiError::Internal {
message: "inference failed".to_string(),
}
})?;
// Distinguish "hit token cap" from "natural stop" (EOS / stop token / stop string).
// `GenerateOutput.stopped` carries the explicit stop reason set by the library.
// Log and return 500 if the invariant is violated.
if output.generated_tokens > max_tokens {
eprintln!(
"generation invariant violation: generated_tokens={} max_tokens={}",
output.generated_tokens, max_tokens
);
return Err(ApiError::Internal {
message: "inference failed".to_string(),
});
}
let finish_reason = finish_reason_for(&output);
let created = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0);
let seq = state.request_counter.fetch_add(1, Ordering::Relaxed);
let response = ChatCompletionResponse {
id: format!("chatcmpl-{created}-{seq}"),
object: "chat.completion".to_string(),
created,
model: state.model_id.clone(),
choices: vec![Choice {
index: 0,
message: ResponseMessage {
role: "assistant".to_string(),
content: output.text.clone(),
},
finish_reason: finish_reason.to_string(),
}],
usage: Usage {
prompt_tokens: output.prompt_tokens,
completion_tokens: output.generated_tokens,
total_tokens: output.prompt_tokens + output.generated_tokens,
},
};
Ok(Json(response))
}
// -----------------------------------------------------------------------
// Router
// -----------------------------------------------------------------------
pub fn router(state: AppState) -> Router {
Router::new()
.route("/health", get(health))
.route("/v1/chat/completions", post(chat_completions))
.layer(DefaultBodyLimit::max(REQUEST_BODY_LIMIT_BYTES))
.with_state(state)
}
// -----------------------------------------------------------------------
// Tests — pure helper functions; no model construction needed
// -----------------------------------------------------------------------
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn validate_max_tokens_rejects_zero() {
let err = validate_max_tokens(Some(0), None, 256, 4096).unwrap_err();
assert!(matches!(
err,
ApiError::BadRequest {
code: "invalid_max_tokens",
..
}
));
}
#[test]
fn validate_max_tokens_rejects_above_cap() {
let err = validate_max_tokens(Some(9999), None, 256, 4096).unwrap_err();
assert!(matches!(
err,
ApiError::BadRequest {
code: "max_tokens_exceeds_limit",
..
}
));
}
#[test]
fn validate_max_tokens_uses_default_when_absent() {
assert_eq!(validate_max_tokens(None, None, 128, 4096).unwrap(), 128);
}
#[test]
fn validate_max_tokens_alias_agrees() {
assert_eq!(
validate_max_tokens(Some(512), Some(512), 256, 4096).unwrap(),
512
);
}
#[test]
fn validate_max_tokens_alias_conflict_rejected() {
let err = validate_max_tokens(Some(100), Some(200), 256, 4096).unwrap_err();
assert!(matches!(
err,
ApiError::BadRequest {
code: "invalid_request",
..
}
));
}
#[test]
fn validate_temperature_rejects_negative() {
let err = validate_temperature(Some(-0.1)).unwrap_err();
assert!(matches!(
err,
ApiError::BadRequest {
code: "invalid_temperature",
..
}
));
}
#[test]
fn validate_temperature_rejects_above_two() {
let err = validate_temperature(Some(2.1)).unwrap_err();
assert!(matches!(
err,
ApiError::BadRequest {
code: "invalid_temperature",
..
}
));
}
#[test]
fn validate_temperature_accepts_boundary() {
assert_eq!(validate_temperature(Some(0.0)).unwrap(), 0.0);
assert_eq!(validate_temperature(Some(2.0)).unwrap(), 2.0);
}
#[test]
fn validate_top_p_rejects_zero() {
let err = validate_top_p(Some(0.0)).unwrap_err();
assert!(matches!(
err,
ApiError::BadRequest {
code: "invalid_top_p",
..
}
));
}
#[test]
fn validate_top_p_rejects_above_one() {
let err = validate_top_p(Some(1.1)).unwrap_err();
assert!(matches!(
err,
ApiError::BadRequest {
code: "invalid_top_p",
..
}
));
}
#[test]
fn validate_top_p_accepts_one() {
assert_eq!(validate_top_p(Some(1.0)).unwrap(), 1.0);
}
#[test]
fn render_prompt_multi_message_chatml() {
let messages = vec![
Message {
role: "system".to_string(),
content: MessageContent::Text("Be helpful.".to_string()),
},
Message {
role: "user".to_string(),
content: MessageContent::Text("Hello".to_string()),
},
];
let prompt = render_prompt(&messages).unwrap();
assert!(prompt.contains("<|im_start|>system\nBe helpful.<|im_end|>"));
assert!(prompt.contains("<|im_start|>user\nHello<|im_end|>"));
assert!(prompt.ends_with("<|im_start|>assistant\n"));
}
#[test]
fn render_prompt_rejects_invalid_role() {
let messages = vec![Message {
role: "function".to_string(),
content: MessageContent::Text("data".to_string()),
}];
let err = render_prompt(&messages).unwrap_err();
assert!(matches!(
err,
ApiError::BadRequest {
code: "invalid_role",
..
}
));
}
#[test]
fn render_prompt_rejects_tool_role() {
let messages = vec![Message {
role: "tool".to_string(),
content: MessageContent::Text("result".to_string()),
}];
let err = render_prompt(&messages).unwrap_err();
assert!(matches!(
err,
ApiError::BadRequest {
code: "unsupported_feature",
..
}
));
}
#[test]
fn render_prompt_rejects_non_text_content_part() {
let messages = vec![Message {
role: "user".to_string(),
content: MessageContent::Parts(vec![ContentPart {
kind: "image_url".to_string(),
text: None,
}]),
}];
let err = render_prompt(&messages).unwrap_err();
assert!(matches!(
err,
ApiError::BadRequest {
code: "unsupported_feature",
..
}
));
}
// Exercises finish_reason_for via the real helper function used by the handler.
// A cap-reached output has stopped=false → "length".
// A stop-condition output has stopped=true → "stop".
#[test]
fn finish_reason_length_only_at_cap() {
use lattice_inference::model::qwen35_config::GenerateOutput;
let cap = GenerateOutput {
text: String::new(),
token_ids: vec![],
prompt_tokens: 10,
generated_tokens: 64,
stopped: false,
};
assert_eq!(super::finish_reason_for(&cap), "length");
let natural = GenerateOutput {
text: "hello".into(),
token_ids: vec![1, 2, 3],
prompt_tokens: 10,
generated_tokens: 3,
stopped: true,
};
assert_eq!(super::finish_reason_for(&natural), "stop");
}
// M1 regression: a stop-string hit at exactly max_new_tokens must yield "stop",
// not "length". The old token-count formula (generated == cap → "length") would
// mislabel this case because the stop-completing token is included in generated_ids
// before the stop is detected.
//
// This test calls the real finish_reason_for helper. It is RED when
// finish_reason_for reverts to the old `generated_tokens == max_tokens` formula.
#[test]
fn finish_reason_stop_string_at_cap_is_stop_not_length() {
use lattice_inference::model::qwen35_config::GenerateOutput;
let max_tokens: usize = 4;
// stop-string hit at exactly the token budget:
// stopped=true because a stop string matched; generated_tokens==max_tokens
// because the matching token is included in generated_ids before truncation.
let output = GenerateOutput {
text: "hi".into(),
token_ids: vec![1, 2, 3, 4],
prompt_tokens: 5,
generated_tokens: max_tokens,
stopped: true,
};
assert_eq!(
super::finish_reason_for(&output),
"stop",
"stop-string hit at cap must yield finish_reason=stop, not length"
);
}
// Natural length cap (no stop condition) must still yield "length".
#[test]
fn finish_reason_natural_length_cap_is_length() {
use lattice_inference::model::qwen35_config::GenerateOutput;
let output = GenerateOutput {
text: "hi".into(),
token_ids: vec![1, 2, 3, 4],
prompt_tokens: 5,
generated_tokens: 4,
stopped: false,
};
assert_eq!(super::finish_reason_for(&output), "length");
}
#[test]
fn reject_unsupported_stream_true() {
let req = ChatCompletionRequest {
model: "m".to_string(),
messages: vec![],
max_tokens: None,
max_completion_tokens: None,
temperature: None,
top_p: None,
stream: Some(true),
stop: None,
seed: None,
response_format: None,
tools: None,
tool_choice: None,
logprobs: None,
n: None,
};
let err = reject_unsupported(&req).unwrap_err();
assert!(matches!(
err,
ApiError::BadRequest {
code: "unsupported_feature",
..
}
));
}
#[test]
fn reject_unsupported_n_gt_1() {
let req = ChatCompletionRequest {
model: "m".to_string(),
messages: vec![],
max_tokens: None,
max_completion_tokens: None,
temperature: None,
top_p: None,
stream: None,
stop: None,
seed: None,
response_format: None,
tools: None,
tool_choice: None,
logprobs: None,
n: Some(3),
};
let err = reject_unsupported(&req).unwrap_err();
assert!(matches!(
err,
ApiError::BadRequest {
code: "unsupported_feature",
..
}
));
}
#[test]
fn reject_unsupported_response_format_json() {
let req = ChatCompletionRequest {
model: "m".to_string(),
messages: vec![],
max_tokens: None,
max_completion_tokens: None,
temperature: None,
top_p: None,
stream: None,
stop: None,
seed: None,
response_format: Some(ResponseFormat {
r#type: "json_object".to_string(),
}),
tools: None,
tool_choice: None,
logprobs: None,
n: None,
};
let err = reject_unsupported(&req).unwrap_err();
assert!(matches!(
err,
ApiError::BadRequest {
code: "unsupported_feature",
..
}
));
}
// -----------------------------------------------------------------------
// reject_unsupported — remaining fields
// -----------------------------------------------------------------------
fn bare_req() -> ChatCompletionRequest {
ChatCompletionRequest {
model: "m".to_string(),
messages: vec![],
max_tokens: None,
max_completion_tokens: None,
temperature: None,
top_p: None,
stream: None,
stop: None,
seed: None,
response_format: None,
tools: None,
tool_choice: None,
logprobs: None,
n: None,
}
}
#[test]
fn reject_unsupported_tools_rejected() {
let req = ChatCompletionRequest {
tools: Some(serde_json::json!([])),
..bare_req()
};
let err = reject_unsupported(&req).unwrap_err();
assert!(matches!(
err,
ApiError::BadRequest {
code: "unsupported_feature",
..
}
));
}
#[test]
fn reject_unsupported_tool_choice_rejected() {
let req = ChatCompletionRequest {
tool_choice: Some(serde_json::json!("auto")),
..bare_req()
};
let err = reject_unsupported(&req).unwrap_err();
assert!(matches!(
err,
ApiError::BadRequest {
code: "unsupported_feature",
..
}
));
}
#[test]
fn reject_unsupported_logprobs_rejected() {
let req = ChatCompletionRequest {
logprobs: Some(true),
..bare_req()
};
let err = reject_unsupported(&req).unwrap_err();
assert!(matches!(
err,
ApiError::BadRequest {
code: "unsupported_feature",
..
}
));
}
#[test]
fn reject_unsupported_stop_now_accepted() {
// stop is no longer rejected by reject_unsupported; it is parsed separately.
let req = ChatCompletionRequest {
stop: Some(serde_json::json!("</s>")),
..bare_req()
};
assert!(reject_unsupported(&req).is_ok());
}
// -----------------------------------------------------------------------
// parse_stop_strings
// -----------------------------------------------------------------------
#[test]
fn parse_stop_strings_null_gives_empty() {
assert_eq!(parse_stop_strings(&None).unwrap(), Vec::<String>::new());
assert_eq!(
parse_stop_strings(&Some(serde_json::Value::Null)).unwrap(),
Vec::<String>::new()
);
}
#[test]
fn parse_stop_strings_single_string_gives_vec_of_one() {
let v = parse_stop_strings(&Some(serde_json::json!("</s>"))).unwrap();
assert_eq!(v, vec!["</s>".to_string()]);
}
#[test]
fn parse_stop_strings_array_of_two_accepted() {
let v = parse_stop_strings(&Some(serde_json::json!(["</s>", "\nUser:"]))).unwrap();
assert_eq!(v, vec!["</s>".to_string(), "\nUser:".to_string()]);
}
#[test]
fn parse_stop_strings_empty_array_rejected() {
let err = parse_stop_strings(&Some(serde_json::json!([]))).unwrap_err();
assert!(matches!(
err,
ApiError::BadRequest {
code: "invalid_stop",
..
}
));
}
#[test]
fn parse_stop_strings_array_over_four_rejected() {
let err = parse_stop_strings(&Some(serde_json::json!(["a", "b", "c", "d", "e"])))
.unwrap_err();
assert!(matches!(
err,
ApiError::BadRequest {
code: "invalid_stop",
..
}
));
}
#[test]
fn parse_stop_strings_array_with_number_rejected() {
let err = parse_stop_strings(&Some(serde_json::json!(["ok", 42]))).unwrap_err();
assert!(matches!(
err,
ApiError::BadRequest {
code: "invalid_stop",
..
}
));
}
#[test]
fn parse_stop_strings_empty_string_element_rejected() {
let err = parse_stop_strings(&Some(serde_json::json!(["ok", ""]))).unwrap_err();
assert!(matches!(
err,
ApiError::BadRequest {
code: "invalid_stop",
..
}
));
}
#[test]
fn parse_stop_strings_empty_string_scalar_rejected() {
let err = parse_stop_strings(&Some(serde_json::json!(""))).unwrap_err();
assert!(matches!(
err,
ApiError::BadRequest {
code: "invalid_stop",
..
}
));
}
#[test]
fn parse_stop_strings_array_exactly_four_accepted() {
let v = parse_stop_strings(&Some(serde_json::json!(["a", "b", "c", "d"]))).unwrap();
assert_eq!(v.len(), 4);
}
#[test]
fn reject_unsupported_stream_false_ok() {
// stream=false must not trigger a rejection.
let req = ChatCompletionRequest {
stream: Some(false),
..bare_req()
};
assert!(reject_unsupported(&req).is_ok());
}
#[test]
fn reject_unsupported_n_1_ok() {
let req = ChatCompletionRequest {
n: Some(1),
..bare_req()
};
assert!(reject_unsupported(&req).is_ok());
}
#[test]
fn reject_unsupported_response_format_text_ok() {
let req = ChatCompletionRequest {
response_format: Some(ResponseFormat {
r#type: "text".to_string(),
}),
..bare_req()
};
assert!(reject_unsupported(&req).is_ok());
}
#[test]
fn reject_unsupported_logprobs_false_ok() {
let req = ChatCompletionRequest {
logprobs: Some(false),
..bare_req()
};
assert!(reject_unsupported(&req).is_ok());
}
// -----------------------------------------------------------------------
// validate_max_tokens — additional edge cases
// -----------------------------------------------------------------------
#[test]
fn validate_max_tokens_at_exactly_cap_ok() {
assert_eq!(
validate_max_tokens(Some(4096), None, 256, 4096).unwrap(),
4096
);
}
#[test]
fn validate_max_tokens_max_completion_only_ok() {
assert_eq!(
validate_max_tokens(None, Some(512), 256, 4096).unwrap(),
512
);
}
// -----------------------------------------------------------------------
// validate_temperature — default path
// -----------------------------------------------------------------------
#[test]
fn validate_temperature_none_uses_default() {
assert_eq!(validate_temperature(None).unwrap(), 0.7);
}
// -----------------------------------------------------------------------
// validate_top_p — default path
// -----------------------------------------------------------------------
#[test]
fn validate_top_p_none_uses_default() {
assert_eq!(validate_top_p(None).unwrap(), 0.9);
}
// -----------------------------------------------------------------------
// render_prompt — additional cases
// -----------------------------------------------------------------------
#[test]
fn render_prompt_user_only() {
let msgs = vec![Message {
role: "user".to_string(),
content: MessageContent::Text("hi".to_string()),
}];
let prompt = render_prompt(&msgs).unwrap();
assert_eq!(
prompt,
"<|im_start|>user\nhi<|im_end|>\n<|im_start|>assistant\n"
);
}
#[test]
fn render_prompt_multi_turn_assistant() {
let msgs = vec![
Message {
role: "user".to_string(),
content: MessageContent::Text("q1".to_string()),
},
Message {
role: "assistant".to_string(),
content: MessageContent::Text("a1".to_string()),
},
Message {
role: "user".to_string(),
content: MessageContent::Text("q2".to_string()),
},
];
let prompt = render_prompt(&msgs).unwrap();
assert!(prompt.contains("<|im_start|>user\nq1<|im_end|>"));
assert!(prompt.contains("<|im_start|>assistant\na1<|im_end|>"));
assert!(prompt.contains("<|im_start|>user\nq2<|im_end|>"));
assert!(prompt.ends_with("<|im_start|>assistant\n"));
}
#[test]
fn render_prompt_content_parts_text_ok() {
let msgs = vec![Message {
role: "user".to_string(),
content: MessageContent::Parts(vec![
ContentPart {
kind: "text".to_string(),
text: Some("hello".to_string()),
},
ContentPart {
kind: "text".to_string(),
text: Some(" world".to_string()),
},
]),
}];
let prompt = render_prompt(&msgs).unwrap();
assert!(prompt.contains("<|im_start|>user\nhello world<|im_end|>"));
}
#[test]
fn render_prompt_rejects_developer_role() {
let msgs = vec![Message {
role: "developer".to_string(),
content: MessageContent::Text("system prompt".to_string()),
}];
let err = render_prompt(&msgs).unwrap_err();
assert!(matches!(
err,
ApiError::BadRequest {
code: "unsupported_feature",
..
}
));
}
// -----------------------------------------------------------------------
// Error envelope JSON shape
// -----------------------------------------------------------------------
#[test]
fn error_envelope_bad_request_shape() {
let err = ApiError::BadRequest {
message: "test error".to_string(),
code: "invalid_request",
};
// Verify the error serialises to the OpenAI envelope shape:
// {"error":{"message":"...","type":"invalid_request_error","code":"...","param":null}}
let body = ErrorBody {
error: ErrorDetail {
message: "test error".to_string(),
r#type: "invalid_request_error",
code: "invalid_request".to_string(),
param: None,
},
};
let json = serde_json::to_string(&body).unwrap();
assert!(json.contains("\"error\""));
assert!(json.contains("\"message\":\"test error\""));
assert!(json.contains("\"type\":\"invalid_request_error\""));
assert!(json.contains("\"code\":\"invalid_request\""));
assert!(json.contains("\"param\":null"));
// Ensure it is NOT a bare message — must be nested under "error".
let v: serde_json::Value = serde_json::from_str(&json).unwrap();
assert!(v["error"].is_object(), "top-level key must be 'error'");
// Variant check kept separate so we know err itself was constructed correctly.
assert!(matches!(
err,
ApiError::BadRequest {
code: "invalid_request",
..
}
));
}
#[test]
fn error_envelope_payload_too_large_shape() {
let body = ErrorBody {
error: ErrorDetail {
message: "request body exceeds 1 MiB limit".to_string(),
r#type: "invalid_request_error",
code: "request_body_too_large".to_string(),
param: None,
},
};
let json = serde_json::to_string(&body).unwrap();
let v: serde_json::Value = serde_json::from_str(&json).unwrap();
assert_eq!(v["error"]["code"], "request_body_too_large");
}
#[test]
fn error_envelope_internal_shape() {
let body = ErrorBody {
error: ErrorDetail {
message: "inference failed".to_string(),
r#type: "server_error",
code: "internal_error".to_string(),
param: None,
},
};
let json = serde_json::to_string(&body).unwrap();
let v: serde_json::Value = serde_json::from_str(&json).unwrap();
assert_eq!(v["error"]["type"], "server_error");
assert_eq!(v["error"]["code"], "internal_error");
}
// -----------------------------------------------------------------------
// message_text helper
// -----------------------------------------------------------------------
#[test]
fn message_text_plain_string() {
let content = MessageContent::Text("hello".to_string());
assert_eq!(message_text(&content).unwrap(), "hello");
}
#[test]
fn message_text_parts_concatenates() {
let content = MessageContent::Parts(vec![
ContentPart {
kind: "text".to_string(),
text: Some("foo".to_string()),
},
ContentPart {
kind: "text".to_string(),
text: Some("bar".to_string()),
},
]);
assert_eq!(message_text(&content).unwrap(), "foobar");
}
#[test]
fn message_text_parts_rejects_image() {
let content = MessageContent::Parts(vec![ContentPart {
kind: "image_url".to_string(),
text: None,
}]);
let err = message_text(&content).unwrap_err();
assert!(matches!(
err,
ApiError::BadRequest {
code: "unsupported_feature",
..
}
));
}
}
}
// ---------------------------------------------------------------------------
// main
// ---------------------------------------------------------------------------
#[tokio::main]
async fn main() {
let cli = Cli::parse();
match cli.command {
Command::Chat {
model,
max_tokens,
temperature,
} => {
run_chat(&model, max_tokens, temperature);
}
Command::Serve {
model,
host,
port,
max_tokens,
model_id,
} => {
use std::path::Path;
use std::sync::Arc;
use std::sync::atomic::AtomicU64;
// Derive a model identifier from the path basename when --model-id
// is not provided.
let served_model_id = model_id.unwrap_or_else(|| {
Path::new(&model)
.file_name()
.and_then(|n| n.to_str())
.unwrap_or("lattice")
.to_string()
});
eprintln!("Loading model from {model}...");
let qwen_model = match lattice_inference::model::qwen35::Qwen35Model::from_safetensors(
Path::new(&model),
) {
Ok(m) => m,
Err(e) => {
eprintln!("Error: failed to load model: {e}");
std::process::exit(1);
}
};
eprintln!("Model loaded. Serving as '{served_model_id}'.");
let state = serve::AppState {
model: Arc::new(qwen_model),
default_max_tokens: max_tokens,
max_tokens_cap: 4096,
model_id: served_model_id.clone(),
request_counter: Arc::new(AtomicU64::new(0)),
};
let app = serve::router(state);
let addr = format!("{host}:{port}");
let listener = match tokio::net::TcpListener::bind(&addr).await {
Ok(l) => l,
Err(e) => {
eprintln!("Error: failed to bind to {addr}: {e}");
std::process::exit(1);
}
};
eprintln!(
"Listening on {addr} (model: {served_model_id}, max_tokens default: {max_tokens})"
);
eprintln!(" POST /v1/chat/completions");
eprintln!(" GET /health");
let shutdown = async {
if let Err(e) = tokio::signal::ctrl_c().await {
eprintln!("Error waiting for shutdown signal: {e}");
}
eprintln!("Shutdown signal received, draining connections...");
};
if let Err(e) = axum::serve(listener, app)
.with_graceful_shutdown(shutdown)
.await
{
eprintln!("Server error: {e}");
std::process::exit(1);
}
}
}
}