Skip to content

Commit 7336d41

Browse files
committed
chore: move more choicestream to method
Signed-off-by: ayushag <ayushag@nvidia.com>
1 parent bcdc447 commit 7336d41

File tree

1 file changed

+43
-48
lines changed
  • lib/llm/src/protocols/openai/chat_completions

1 file changed

+43
-48
lines changed

lib/llm/src/protocols/openai/chat_completions/jail.rs

Lines changed: 43 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,10 @@ struct ChoiceJailState {
7575
}
7676

7777
fn create_choice_stream(
78-
content: &str,
79-
role: Option<Role>,
8078
index: u32,
79+
role: Option<Role>,
80+
content: &str,
81+
tool_calls: Option<Vec<ChatCompletionMessageToolCallChunk>>,
8182
finish_reason: Option<FinishReason>,
8283
logprobs: Option<ChatChoiceLogprobs>,
8384
) -> ChatChoiceStream {
@@ -87,7 +88,7 @@ fn create_choice_stream(
8788
delta: ChatCompletionStreamResponseDelta {
8889
role,
8990
content: Some(content.to_string()),
90-
tool_calls: None,
91+
tool_calls,
9192
function_call: None,
9293
refusal: None,
9394
reasoning_content: None,
@@ -146,10 +147,11 @@ impl ChoiceJailState {
146147
if !prefix.is_empty() {
147148
#[allow(deprecated)]
148149
let prefix_choice = create_choice_stream(
149-
&prefix,
150-
choice.delta.role,
151150
choice.index,
152-
choice.finish_reason,
151+
choice.delta.role,
152+
&prefix,
153+
None,
154+
None,
153155
choice.logprobs.clone(),
154156
);
155157
emissions.push(ChoiceEmission::PassThrough(prefix_choice));
@@ -185,10 +187,11 @@ impl ChoiceJailState {
185187
if !trailing_part.is_empty() {
186188
#[allow(deprecated)]
187189
let trailing_choice = create_choice_stream(
188-
trailing_part,
189-
choice.delta.role,
190190
choice.index,
191-
choice.finish_reason,
191+
choice.delta.role,
192+
trailing_part,
193+
None,
194+
None,
192195
choice.logprobs.clone(),
193196
);
194197
emissions.push(ChoiceEmission::Trailing(trailing_choice));
@@ -216,10 +219,11 @@ impl ChoiceJailState {
216219
if !prefix.is_empty() {
217220
#[allow(deprecated)]
218221
let prefix_choice = create_choice_stream(
219-
&prefix,
220-
choice.delta.role,
221222
choice.index,
222-
choice.finish_reason,
223+
choice.delta.role,
224+
&prefix,
225+
None,
226+
None,
223227
choice.logprobs.clone(),
224228
);
225229
emissions.push(ChoiceEmission::PassThrough(prefix_choice));
@@ -258,10 +262,11 @@ impl ChoiceJailState {
258262
if !content.is_empty() {
259263
#[allow(deprecated)]
260264
let pass_through_choice = create_choice_stream(
261-
&content,
262-
choice.delta.role,
263265
choice.index,
264-
choice.finish_reason,
266+
choice.delta.role,
267+
&content,
268+
None,
269+
None,
265270
choice.logprobs.clone(),
266271
);
267272
emissions.push(ChoiceEmission::PassThrough(pass_through_choice));
@@ -302,10 +307,11 @@ impl ChoiceJailState {
302307
if !trailing_part.is_empty() {
303308
#[allow(deprecated)]
304309
let trailing_choice = create_choice_stream(
305-
trailing_part,
306-
choice.delta.role,
307310
choice.index,
308-
choice.finish_reason,
311+
choice.delta.role,
312+
trailing_part,
313+
None,
314+
None,
309315
choice.logprobs.clone(),
310316
);
311317
emissions.push(ChoiceEmission::Trailing(trailing_choice));
@@ -331,9 +337,10 @@ impl ChoiceJailState {
331337
// Create a dummy choice for the method call
332338
#[allow(deprecated)]
333339
let dummy_choice = create_choice_stream(
334-
&self.accumulated_content,
335-
Some(Role::Assistant),
336340
self.index,
341+
Some(Role::Assistant),
342+
&self.accumulated_content,
343+
None,
337344
None,
338345
None,
339346
);
@@ -693,37 +700,25 @@ impl JailedStream {
693700
.collect();
694701

695702
// Create choice with tool calls
696-
#[allow(deprecated)]
697-
return ChatChoiceStream {
698-
index: choice_index,
699-
delta: ChatCompletionStreamResponseDelta {
700-
role: Some(Role::Assistant),
701-
content: normal_text.filter(|t| !t.is_empty()),
702-
tool_calls: Some(tool_call_chunks),
703-
function_call: None,
704-
refusal: None,
705-
reasoning_content: None,
706-
},
707-
finish_reason: Some(FinishReason::ToolCalls),
708-
logprobs: None,
709-
};
703+
return create_choice_stream(
704+
choice_index,
705+
Some(Role::Assistant),
706+
normal_text.as_deref().unwrap_or(""),
707+
Some(tool_call_chunks),
708+
Some(FinishReason::ToolCalls),
709+
None,
710+
);
710711
}
711712

712713
// No tool calls found or parsing failed, return content choice
713-
#[allow(deprecated)]
714-
ChatChoiceStream {
715-
index: choice_index,
716-
delta: ChatCompletionStreamResponseDelta {
717-
role: Some(Role::Assistant),
718-
content: Some(accumulated_content.to_string()),
719-
tool_calls: None,
720-
function_call: None,
721-
refusal: None,
722-
reasoning_content: None,
723-
},
724-
finish_reason: None,
725-
logprobs: base_choice.logprobs.clone(),
726-
}
714+
create_choice_stream(
715+
choice_index,
716+
Some(Role::Assistant),
717+
accumulated_content,
718+
None,
719+
None,
720+
base_choice.logprobs.clone(),
721+
)
727722
}
728723

729724
/// Check if accumulated content contains complete tool calls that can be parsed

0 commit comments

Comments
 (0)