Skip to content

Commit a6821db

Browse files
committed
Refactoring chat completion stream
1 parent 54a84ca commit a6821db

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

src/v1/chat_completion/chat_completion_stream.rs

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -166,30 +166,29 @@ where
166166

167167
match serde_json::from_str::<Value>(&data_payload) {
168168
Ok(json) => {
169-
if let Some(choices) = json.get("choices") {
170-
if let Some(choice) = choices.get(0) {
171-
if let Some(delta) = choice.get("delta") {
172-
if let Some(tool_calls) = delta.get("tool_calls") {
173-
if let Some(tool_calls_array) = tool_calls.as_array() {
174-
let tool_calls_vec: Vec<ToolCall> = tool_calls_array
175-
.iter()
176-
.filter_map(|v| serde_json::from_value(v.clone()).ok())
177-
.collect();
178-
179-
if !tool_calls_vec.is_empty() {
180-
return Some(ChatCompletionStreamResponse::ToolCall(
181-
tool_calls_vec,
182-
));
183-
}
184-
}
185-
}
186-
187-
if let Some(content) = delta.get("content").and_then(|c| c.as_str())
188-
{
189-
let output = content.replace("\\n", "\n");
190-
return Some(ChatCompletionStreamResponse::Content(output));
191-
}
192-
}
169+
if let Some(delta) = json
170+
.get("choices")
171+
.and_then(|choices| choices.get(0))
172+
.and_then(|choice| choice.get("delta"))
173+
{
174+
if let Some(tool_call_response) = delta
175+
.get("tool_calls")
176+
.and_then(|tool_calls| tool_calls.as_array())
177+
.map(|tool_calls_array| {
178+
tool_calls_array
179+
.iter()
180+
.filter_map(|v| serde_json::from_value(v.clone()).ok())
181+
.collect::<Vec<ToolCall>>()
182+
})
183+
.filter(|tool_calls_vec| !tool_calls_vec.is_empty())
184+
.map(ChatCompletionStreamResponse::ToolCall)
185+
{
186+
return Some(tool_call_response);
187+
}
188+
189+
if let Some(content) = delta.get("content").and_then(|c| c.as_str()) {
190+
let output = content.replace("\\n", "\n");
191+
return Some(ChatCompletionStreamResponse::Content(output));
193192
}
194193
}
195194
}

0 commit comments

Comments
 (0)