Parsing fixes

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
This commit is contained in:
Kent Overstreet 2026-04-09 16:20:11 -04:00
parent b55230ce3f
commit 0af97774f4
2 changed files with 143 additions and 127 deletions

View file

@ -45,7 +45,7 @@ pub(crate) struct SamplingParams {
/// One token from the streaming completions API.
pub enum StreamToken {
Token { text: String, id: u32 },
Token(u32),
Done { usage: Option<Usage> },
Error(String),
}
@ -159,20 +159,19 @@ async fn stream_completions(
};
for choice in choices {
let text = choice["text"].as_str().unwrap_or("");
let token_ids = choice["token_ids"].as_array();
if let Some(ids) = token_ids {
for (i, id_val) in ids.iter().enumerate() {
if let Some(ids) = choice["token_ids"].as_array() {
for id_val in ids {
if let Some(id) = id_val.as_u64() {
let _ = tx.send(StreamToken::Token {
text: if i == 0 { text.to_string() } else { String::new() },
id: id as u32,
});
let _ = tx.send(StreamToken::Token(id as u32));
}
}
} else if let Some(text) = choice["text"].as_str() {
// Fallback: provider didn't return token_ids, encode locally
if !text.is_empty() {
for id in super::tokenizer::encode(text) {
let _ = tx.send(StreamToken::Token(id));
}
}
} else if !text.is_empty() {
let _ = tx.send(StreamToken::Token { text: text.to_string(), id: 0 });
}
}
}