feat(inference): 添加流式输出功能,支持实时解码文本

This commit is contained in:
2026-07-01 17:54:18 +08:00
parent 18a5ca103c
commit faeeaf7409
3 changed files with 118 additions and 3 deletions
+11 -3
View File
@@ -25,7 +25,7 @@ fn main() -> anyhow::Result<()> {
println!("模型加载完成,耗时: {:.2?}", start.elapsed());
let prompt = "你知道今天的天气不";
let prompt = "我怕人家一说要改前端文件,明天咔嚓甩给我了";
println!("\n用户: {}", prompt);
println!("Assistant: ");
@@ -36,10 +36,18 @@ fn main() -> anyhow::Result<()> {
};
let start = Instant::now();
let response = model.generate(prompt, false, &config)?;
let _response = model.generate_stream(
prompt,
false,
&config,
|token| {
print!("{}", token);
std::io::Write::flush(&mut std::io::stdout()).ok();
},
)?;
let elapsed = start.elapsed();
println!("{}", response);
println!();
println!("\n生成耗时: {:.2?}", elapsed);
Ok(())