update: 流式返回

This commit is contained in:
zeke
2024-11-27 10:03:19 +08:00
parent 4a6be2b1c0
commit 517e79f92d
2 changed files with 18 additions and 37 deletions

3
.gitignore vendored
View File

@@ -6,4 +6,5 @@ node_modules/
.idea/ .idea/
__pycache__/ __pycache__/
rs-capi/target/ rs-capi/target/
.vscode/ .vscode/
cursor-api/

View File

@@ -199,9 +199,9 @@ pub async fn chat_completions(
match chunk { match chunk {
Ok(chunk) => { Ok(chunk) => {
let res = chunk_to_utf8_string(&chunk); let res = chunk_to_utf8_string(&chunk);
if !res.is_empty() { // if !res.is_empty() {
text.push_str(&res); text.push_str(&res);
} // }
} }
Err(_) => return Err(StatusCode::INTERNAL_SERVER_ERROR), Err(_) => return Err(StatusCode::INTERNAL_SERVER_ERROR),
} }
@@ -249,45 +249,25 @@ async fn process_stream(
tokio::spawn(async move { tokio::spawn(async move {
for chunk in chunks { for chunk in chunks {
let text = chunk_to_utf8_string(&chunk); let text = chunk_to_utf8_string(&chunk);
// 只在文本非空时处理和发送
if !text.is_empty() { if !text.is_empty() {
let text = text.trim(); let response = models::chat::StreamResponse {
let text = if let Some(idx) = text.find("<|END_USER|>") { id: response_id.clone(),
text[idx + "<|END_USER|>".len()..].trim() object: "chat.completion.chunk".to_string(),
} else { created: chrono::Utc::now().timestamp(),
text choices: vec![models::chat::StreamChoice {
index: 0,
delta: models::chat::Delta { content: text },
}],
}; };
let text = if !text.is_empty() && text.chars().next().unwrap().is_alphabetic() { let json_data = serde_json::to_string(&response).unwrap();
text[1..].trim() let _ = tx.send(Ok(Event::default().data(json_data))).await;
} else {
text
};
let re = Regex::new(r"[\x00-\x1F\x7F]").unwrap();
let text = re.replace_all(text, "");
if !text.is_empty() {
let var_name = models::chat::StreamResponse {
id: response_id.clone(),
object: "chat.completion.chunk".to_string(),
created: chrono::Utc::now().timestamp(),
choices: vec![models::chat::StreamChoice {
index: 0,
delta: models::chat::Delta {
content: text.to_string(),
},
}],
};
let response = var_name;
let json_data = serde_json::to_string(&response).unwrap();
if !json_data.is_empty() {
let _ = tx.send(Ok(Event::default().data(json_data))).await;
}
}
} }
} }
// 发送完成标记
let _ = tx.send(Ok(Event::default().data("[DONE]"))).await; let _ = tx.send(Ok(Event::default().data("[DONE]"))).await;
}); });