mirror of
https://github.com/zeke-chin/cursor-api.git
synced 2025-09-29 12:52:48 +08:00
update: 流式返回
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -6,4 +6,5 @@ node_modules/
|
|||||||
.idea/
|
.idea/
|
||||||
__pycache__/
|
__pycache__/
|
||||||
rs-capi/target/
|
rs-capi/target/
|
||||||
.vscode/
|
.vscode/
|
||||||
|
cursor-api/
|
@@ -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;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user