Skip to content

Commit ee4ef1e

Browse files
committed
chore: reduced lines
Signed-off-by: ayushag <ayushag@nvidia.com>
1 parent 6243bcb commit ee4ef1e

File tree

1 file changed

+22
-65
lines changed
  • lib/llm/src/protocols/openai/chat_completions

1 file changed

+22
-65
lines changed

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

Lines changed: 22 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,23 @@ struct ChoiceJailState {
7272
partial_match_buffer: String,
7373
}
7474

75+
fn create_choice_stream(choice: &ChatChoiceStream, content: &str) -> ChatChoiceStream {
76+
#[allow(deprecated)]
77+
ChatChoiceStream {
78+
index: choice.index,
79+
delta: ChatCompletionStreamResponseDelta {
80+
role: choice.delta.role,
81+
content: Some(content.to_string()),
82+
tool_calls: None,
83+
function_call: None,
84+
refusal: None,
85+
reasoning_content: None,
86+
},
87+
finish_reason: choice.finish_reason,
88+
logprobs: choice.logprobs.clone(),
89+
}
90+
}
91+
7592
impl ChoiceJailState {
7693
/// Create a new jail state for a choice
7794
fn new(index: u32) -> Self {
@@ -120,19 +137,7 @@ impl ChoiceJailState {
120137
// Emit prefix if any
121138
if !prefix.is_empty() {
122139
#[allow(deprecated)]
123-
let prefix_choice = ChatChoiceStream {
124-
index: choice.index,
125-
delta: ChatCompletionStreamResponseDelta {
126-
role: choice.delta.role,
127-
content: Some(prefix),
128-
tool_calls: None,
129-
function_call: None,
130-
refusal: None,
131-
reasoning_content: None,
132-
},
133-
finish_reason: None,
134-
logprobs: choice.logprobs.clone(),
135-
};
140+
let prefix_choice = create_choice_stream(choice, &prefix);
136141
emissions.push(ChoiceEmission::PassThrough(prefix_choice));
137142
}
138143

@@ -165,19 +170,7 @@ impl ChoiceJailState {
165170
// Handle trailing content if any
166171
if !trailing_part.is_empty() {
167172
#[allow(deprecated)]
168-
let trailing_choice = ChatChoiceStream {
169-
index: choice.index,
170-
delta: ChatCompletionStreamResponseDelta {
171-
role: choice.delta.role,
172-
content: Some(trailing_part.to_string()),
173-
tool_calls: None,
174-
function_call: None,
175-
refusal: None,
176-
reasoning_content: None,
177-
},
178-
finish_reason: None,
179-
logprobs: choice.logprobs.clone(),
180-
};
173+
let trailing_choice = create_choice_stream(choice, trailing_part);
181174
emissions.push(ChoiceEmission::Trailing(trailing_choice));
182175
}
183176
} else {
@@ -202,19 +195,7 @@ impl ChoiceJailState {
202195
// Emit the safe prefix
203196
if !prefix.is_empty() {
204197
#[allow(deprecated)]
205-
let prefix_choice = ChatChoiceStream {
206-
index: choice.index,
207-
delta: ChatCompletionStreamResponseDelta {
208-
role: choice.delta.role,
209-
content: Some(prefix),
210-
tool_calls: None,
211-
function_call: None,
212-
refusal: None,
213-
reasoning_content: None,
214-
},
215-
finish_reason: None,
216-
logprobs: choice.logprobs.clone(),
217-
};
198+
let prefix_choice = create_choice_stream(choice, &prefix);
218199
emissions.push(ChoiceEmission::PassThrough(prefix_choice));
219200
}
220201

@@ -250,19 +231,7 @@ impl ChoiceJailState {
250231
// No markers - emit everything
251232
if !content.is_empty() {
252233
#[allow(deprecated)]
253-
let pass_through_choice = ChatChoiceStream {
254-
index: choice.index,
255-
delta: ChatCompletionStreamResponseDelta {
256-
role: choice.delta.role,
257-
content: Some(content),
258-
tool_calls: None,
259-
function_call: None,
260-
refusal: None,
261-
reasoning_content: None,
262-
},
263-
finish_reason: None,
264-
logprobs: choice.logprobs.clone(),
265-
};
234+
let pass_through_choice = create_choice_stream(choice, &content);
266235
emissions.push(ChoiceEmission::PassThrough(pass_through_choice));
267236
}
268237
self.partial_match_buffer.clear();
@@ -300,19 +269,7 @@ impl ChoiceJailState {
300269
// Handle trailing content if any
301270
if !trailing_part.is_empty() {
302271
#[allow(deprecated)]
303-
let trailing_choice = ChatChoiceStream {
304-
index: choice.index,
305-
delta: ChatCompletionStreamResponseDelta {
306-
role: choice.delta.role,
307-
content: Some(trailing_part.to_string()),
308-
tool_calls: None,
309-
function_call: None,
310-
refusal: None,
311-
reasoning_content: None,
312-
},
313-
finish_reason: None,
314-
logprobs: choice.logprobs.clone(),
315-
};
272+
let trailing_choice = create_choice_stream(choice, trailing_part);
316273
emissions.push(ChoiceEmission::Trailing(trailing_choice));
317274
}
318275

0 commit comments

Comments
 (0)